Solved

How to combine two select querys

Posted on 2014-03-28
5
183 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
triggered use of sp_send_dbmail failure 2 22
grouping logic 6 46
Can someone plz fix this..getting an error 3 17
SQL Server stored proc 2 12
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now