Solved

How to combine two select querys

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

Expert Comment

by:Stefan Hoffmann
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

744 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

21 Experts available now in Live!

Get 1:1 Help Now