Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

How to combine two select querys

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
team2005
Asked:
team2005
  • 2
  • 2
1 Solution
 
ste5anSenior DeveloperCommented:
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
 
edtechdbaCommented:
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
 
team2005Author Commented:
Hi!

Tryed to use:

ON qry1.TransactionID = qry2.TransactionID

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

??
0
 
edtechdbaCommented:
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
 
team2005Author Commented:
thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now