Join two query to make it one

Hi,

I have two query

query 1)

select
d5.NO,
d.TYPE_CD_ENU,
d5.BUS_NM,
SUM(f.QTY),
D5.BUSINESS_ID
 from F_QTY f
LEFT OUTER JOIN D_JUNK d
on f.FK_JUNK_ID=d.SK_JUNK_ID
LEFT OUTER JOIN dbo.D_BUSINESS d5
on f.FK_ACCNT_ID=d5.SK_BUSINESS_ID
WHERE d5.NO ='102161'
group by
d5.EICB_NO,
d.TYPE_CD_ENU,
d5.BUS_NM,
d5.BUSINESS_ID

query 2)

select
e.Shift,
e.Return,
e.BUS_NM,
e.BUSINESS_ID
 from F_ACCT f
LEFT OUTER JOIN D_ACC_JUNK e
on f.FK_JUNK_ID=d.SK_JUNK_ID
LEFT OUTER JOIN dbo.D_BUSINESS d5
on f.FK_ACCNT_ID=d5.SK_BUSINESS_ID
WHERE d5.NO ='102161'

Now join query 1) and query 2)

to get below we can use BUSINESS_ID to join them

d5.NO,
d.TYPE_CD_ENU,
d5.BUS_NM,
SUM(f.QTY),
D5.BUSINESS_ID,
e.Shift,
e.Return,
e.BUS_NM,
e.BUSINESS_ID
sam2929Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
SELECT
 NO,
 TYPE_CD_ENU,
 BUS_NM,
 QTY,
 query1.BUSINESS_ID,
 Shift,
 [Return],
 BUS_NM,
 query2.BUSINESS_ID
FROM (
    select
     d5.NO,
     d.TYPE_CD_ENU,
     d5.BUS_NM,
     SUM(f.QTY) AS QTY,
     D5.BUSINESS_ID
      from F_QTY f
     LEFT OUTER JOIN D_JUNK d
     on f.FK_JUNK_ID=d.SK_JUNK_ID
     LEFT OUTER JOIN dbo.D_BUSINESS d5
     on f.FK_ACCNT_ID=d5.SK_BUSINESS_ID
     WHERE d5.NO ='102161'
     group by
     d5.EICB_NO,
     d.TYPE_CD_ENU,
     d5.BUS_NM,
     d5.BUSINESS_ID
) AS query1
FULL OUTER JOIN (
     select
     e.Shift,
     e.[Return],
     e.BUS_NM,
     e.BUSINESS_ID
      from F_ACCT f
     LEFT OUTER JOIN D_ACC_JUNK e
     on f.FK_JUNK_ID=d.SK_JUNK_ID
     LEFT OUTER JOIN dbo.D_BUSINESS d5
     on f.FK_ACCNT_ID=d5.SK_BUSINESS_ID
     WHERE d5.NO ='102161'
) AS query2 ON
    query2.BUSINESS_ID = query1.BUSINESS_ID
--ORDER BY ...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulEE Topic AdvisorCommented:
both Q1 & Q2

LEFT OUTER JOIN dbo.D_BUSINESS D5 ON F.FK_ACCNT_ID = D5.SK_BUSINESS_ID
WHERE D5.NO = '102161'
There is no benefit to you in using that LEFT OUTER JOIN because every row MUST have D5.NO = '102161'

So just use INNER JOIN instead
Vitor MontalvãoMSSQL Senior EngineerCommented:
sam2929, do you still need help with this question?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.