Solved

How to combine two select querys

Posted on 2014-03-28
5
184 Views
Last Modified: 2014-03-30
Hi!

Have this query 1:

SELECT
    TRTABLE.TransactionID,
    TRTABLE.Question,
    TRTABLE.UserID,
    TRTABLE.Organisation,
	TRTABLE.CreatedDate,
	TRTABLE.CLID,
	TRTABLE.ControlTransID,
	TRTABLE.LQID,
    TRTABLE.CategoryID_1,
	TRTABLE.Category_1,
	TRTABLE.Note,
	TRTABLE.ControlName,
	CONTR.ControlID,
	LOCOBJQUEST.LocationID,
	TRTABLE.Location,
	TRTABLE.CategoryID_2,
    TRTABLE.Category_2,
	TRTABLE.Category_1_Note,
	TRTABLE.Category_2_Note,
	TRTABLE.Answer,
	LOCOBJQUEST.ObjectID,
	QESTTABLE.IsBool,
	QESTTABLE.IsScale,
	QESTTABLE.Mandatory_QR,
	QESTTABLE.Mandatory_Image,
	QESTTABLE.Question_Type,
	QESTTABLE.ImageYN,
	QESTTABLE.NoteYN,
	USERT.Name,
	OBJT.Object,
	(select distinct(Organisationname) from dbo.SHOW_UserControls where dbo.SHOW_UserControls.Organisation=TRTABLE.Organisation) as 'Organisationname'
	
FROM
    dbo.TransactionTable TRTABLE 
INNER JOIN LocationObjectQuestion LOCOBJQUEST
ON TRTABLE.LQID = LOCOBJQUEST.LOQID
INNER JOIN QuestionsTable QESTTABLE
ON LOCOBJQUEST.QuestionsID = QESTTABLE.QuestionsID
INNER JOIN dbo.ControlTrans CONTR
ON CONTR.ControlTransID=TRTABLE.ControlTransID
INNER JOIN SHOW_User USERT
ON TRTABLE.UserID=USERT.UserID
INNER JOIN dbo.ObjectTable OBJT
ON LOCOBJQUEST.ObjectID=OBJT.ObjectID
AND (TRTABLE.Organisation=OBJT.Organisation)

Open in new window


This query gives me 47 records

But need to add 2 more colums(fields) to this query

Query2 :

SELECT
    "Felt1" =
    Case 
        when dbo.CategoryTable.Category = '%Bil eller Container nr%' then dbo.CategoryTable.Category 
        else dbo.SHOW_Category_2.Category_2
    END,     
    "Felt2" =
    Case 
        when dbo.CategoryTable.Category = 'Bil eller Container nr' then dbo.SHOW_Category_2.Category_2 
        else dbo.CategoryTable.Category
    END        
FROM
    dbo.TransactionTable
RIGHT OUTER JOIN
    dbo.CategoryTable
ON
    (
        dbo.TransactionTable.Category_1 = dbo.CategoryTable.CategoryID)
INNER JOIN
    dbo.SHOW_Category_2
ON
    (
        dbo.TransactionTable.Category_2 = dbo.SHOW_Category_2.CategoryID)

Open in new window


How can i combine this two querys ?
0
Comment
Question by:team2005
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 39961592
E.g.

WITH FirstQuery AS ( <yourFirstSql>),
      SecondQuery AS ( <yourSecondQuery> )
  SELECT *
  FROM  FirstQuery FQ
    [INNER|LEFT] JOIN SecondQuery SQ ON FQ.<primaryKeyColumns> = SQ.<primaryKeyColumns>;

Open in new window


You need to include the primary key columns from dbo.TransactionTable in both queries.
0
 
LVL 9

Expert Comment

by:edtechdba
ID: 39962579
This should do the trick, using a CTE to join the columns together.
WITH qry1 AS
(
SELECT
    TRTABLE.TransactionID,
    TRTABLE.Question,
    TRTABLE.UserID,
    TRTABLE.Organisation,
	TRTABLE.CreatedDate,
	TRTABLE.CLID,
	TRTABLE.ControlTransID,
	TRTABLE.LQID,
    TRTABLE.CategoryID_1,
	TRTABLE.Category_1,
	TRTABLE.Note,
	TRTABLE.ControlName,
	CONTR.ControlID,
	LOCOBJQUEST.LocationID,
	TRTABLE.Location,
	TRTABLE.CategoryID_2,
    TRTABLE.Category_2,
	TRTABLE.Category_1_Note,
	TRTABLE.Category_2_Note,
	TRTABLE.Answer,
	LOCOBJQUEST.ObjectID,
	QESTTABLE.IsBool,
	QESTTABLE.IsScale,
	QESTTABLE.Mandatory_QR,
	QESTTABLE.Mandatory_Image,
	QESTTABLE.Question_Type,
	QESTTABLE.ImageYN,
	QESTTABLE.NoteYN,
	USERT.Name,
	OBJT.Object,
	(select distinct(Organisationname) from dbo.SHOW_UserControls where dbo.SHOW_UserControls.Organisation=TRTABLE.Organisation) as 'Organisationname'
	
FROM
    dbo.TransactionTable TRTABLE 
INNER JOIN LocationObjectQuestion LOCOBJQUEST
ON TRTABLE.LQID = LOCOBJQUEST.LOQID
INNER JOIN QuestionsTable QESTTABLE
ON LOCOBJQUEST.QuestionsID = QESTTABLE.QuestionsID
INNER JOIN dbo.ControlTrans CONTR
ON CONTR.ControlTransID=TRTABLE.ControlTransID
INNER JOIN SHOW_User USERT
ON TRTABLE.UserID=USERT.UserID
INNER JOIN dbo.ObjectTable OBJT
ON LOCOBJQUEST.ObjectID=OBJT.ObjectID
AND (TRTABLE.Organisation=OBJT.Organisation)
),
qry2 AS
(
SELECT
    "Felt1" =
    Case 
        when dbo.CategoryTable.Category = '%Bil eller Container nr%' then dbo.CategoryTable.Category 
        else dbo.SHOW_Category_2.Category_2
    END,     
    "Felt2" =
    Case 
        when dbo.CategoryTable.Category = 'Bil eller Container nr' then dbo.SHOW_Category_2.Category_2 
        else dbo.CategoryTable.Category
    END        
FROM
    dbo.TransactionTable
RIGHT OUTER JOIN
    dbo.CategoryTable
ON
    (
        dbo.TransactionTable.Category_1 = dbo.CategoryTable.CategoryID)
INNER JOIN
    dbo.SHOW_Category_2
ON
    (
        dbo.TransactionTable.Category_2 = dbo.SHOW_Category_2.CategoryID)
)

SELECT qry1.*, qry2.*
FROM qry1
INNER JOIN qry2
  ON qry1.id = qry2.id -- this is the primary key that joins the queries together

Open in new window

0
 
LVL 2

Author Comment

by:team2005
ID: 39964851
Hi!

Tryed to use:

ON qry1.TransactionID = qry2.TransactionID

But gives me error:
Invalid column name 'TransactionID'.

??
0
 
LVL 9

Accepted Solution

by:
edtechdba earned 500 total points
ID: 39964882
The TransactionID needs to be included in the SELECT statement for qry2, see below how I added it.
WITH qry1 AS
(
SELECT
    TRTABLE.TransactionID,
    TRTABLE.Question,
    TRTABLE.UserID,
    TRTABLE.Organisation,
	TRTABLE.CreatedDate,
	TRTABLE.CLID,
	TRTABLE.ControlTransID,
	TRTABLE.LQID,
    TRTABLE.CategoryID_1,
	TRTABLE.Category_1,
	TRTABLE.Note,
	TRTABLE.ControlName,
	CONTR.ControlID,
	LOCOBJQUEST.LocationID,
	TRTABLE.Location,
	TRTABLE.CategoryID_2,
    TRTABLE.Category_2,
	TRTABLE.Category_1_Note,
	TRTABLE.Category_2_Note,
	TRTABLE.Answer,
	LOCOBJQUEST.ObjectID,
	QESTTABLE.IsBool,
	QESTTABLE.IsScale,
	QESTTABLE.Mandatory_QR,
	QESTTABLE.Mandatory_Image,
	QESTTABLE.Question_Type,
	QESTTABLE.ImageYN,
	QESTTABLE.NoteYN,
	USERT.Name,
	OBJT.Object,
	(select distinct(Organisationname) from dbo.SHOW_UserControls where dbo.SHOW_UserControls.Organisation=TRTABLE.Organisation) as 'Organisationname'
	
FROM
    dbo.TransactionTable TRTABLE 
INNER JOIN LocationObjectQuestion LOCOBJQUEST
ON TRTABLE.LQID = LOCOBJQUEST.LOQID
INNER JOIN QuestionsTable QESTTABLE
ON LOCOBJQUEST.QuestionsID = QESTTABLE.QuestionsID
INNER JOIN dbo.ControlTrans CONTR
ON CONTR.ControlTransID=TRTABLE.ControlTransID
INNER JOIN SHOW_User USERT
ON TRTABLE.UserID=USERT.UserID
INNER JOIN dbo.ObjectTable OBJT
ON LOCOBJQUEST.ObjectID=OBJT.ObjectID
AND (TRTABLE.Organisation=OBJT.Organisation)
),
qry2 AS
(
SELECT
    TransactionID,
    "Felt1" =
    Case 
        when dbo.CategoryTable.Category = '%Bil eller Container nr%' then dbo.CategoryTable.Category 
        else dbo.SHOW_Category_2.Category_2
    END,     
    "Felt2" =
    Case 
        when dbo.CategoryTable.Category = 'Bil eller Container nr' then dbo.SHOW_Category_2.Category_2 
        else dbo.CategoryTable.Category
    END        
FROM
    dbo.TransactionTable
RIGHT OUTER JOIN
    dbo.CategoryTable
ON
    (
        dbo.TransactionTable.Category_1 = dbo.CategoryTable.CategoryID)
INNER JOIN
    dbo.SHOW_Category_2
ON
    (
        dbo.TransactionTable.Category_2 = dbo.SHOW_Category_2.CategoryID)
)

SELECT qry1.*, qry2.*
FROM qry1
INNER JOIN qry2
  ON qry1.id = qry2.id -- this is the primary key that joins the queries together

Open in new window

0
 
LVL 2

Author Closing Comment

by:team2005
ID: 39964892
thanks
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Count with a subquery showing details 10 44
MS SQL with ODBC 5 36
CPU high usage when update statistics 2 30
Passing Parameter to Stored Procedure 4 24
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question