Solved

how do I join grouped query results?

Posted on 2010-11-29
3
254 Views
Last Modified: 2012-05-10
I am trying to join 2 query results so the each row shows the branchID, sales total and stock level by size. These 2 query's work on the own.

query 1:
SELECT     ph.BRANCH_ID ,SUM(Case when pd.SKU_CODE='01bu0906006514q001' then ph.STOCK_UNITS_FREE else 0 end) as [S],
SUM(Case when pd.SKU_CODE='01bu0906006514q002' then ph.STOCK_UNITS_FREE else 0 end) as [M],
SUM(Case when pd.SKU_CODE='01bu0906006514q003' then ph.STOCK_UNITS_FREE else 0 end) as [L],
SUM(Case when pd.SKU_CODE='01bu0906006514q004' then ph.STOCK_UNITS_FREE else 0 end) as [XL],
SUM(Case when pd.SKU_CODE='01bu0906006514q005' then ph.STOCK_UNITS_FREE else 0 end) as [XXL]  
FROM         RMIS.dbo.product_detail AS pd INNER JOIN RMIS.dbo.PRODUCT_HISTORY AS ph ON ph.SKU_ID = pd.SKU_ID
WHERE     (ph.WEEK_SELECTOR = 1100230) GROUP BY ph.BRANCH_ID

query2
SELECT SUM(CASE WHEN pmh.TXN_DATE_TIME > (GETDATE() - 360) THEN units ELSE 0 END) AS Last360,  
SUM(CASE WHEN pmh.TXN_DATE_TIME > (GETDATE() - 90) THEN units ELSE 0 END) AS Last90,
SUM(CASE WHEN pmh.TXN_DATE_TIME > (GETDATE() - 30)  THEN units ELSE 0 END) AS Last30,
SUM(CASE WHEN pmh.TXN_DATE_TIME > (GETDATE() - 7) THEN units ELSE 0 END) AS Last7, pmh.BRANCH_ID
FROM         RMIS.dbo.PM_TRANS_HEADER AS pmh INNER JOIN RMIS.dbo.PM_TRANS_SKU_DETAILS AS pmskd ON pmh.RECEIPT_ID = pmskd.RECEIPT_ID
INNER JOIN RMIS.dbo.product_detail AS pd ON pd.SKU_ID = pmskd.SKU_ID
WHERE     (pmh.FLAG = 1) AND (pmh.TRANSACTION_VOID = 0) AND (pmskd.FLAG = 1) AND (pd.SKU_CODE LIKE '01bu0906006514q%')
GROUP BY pmh.BRANCH_ID

0
Comment
Question by:tosh2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 34234166
Might be a good use for a CTE,  try something like this....

with Query1 as
(
      SELECT     ph.BRANCH_ID ,SUM(Case when pd.SKU_CODE='01bu0906006514q001' then ph.STOCK_UNITS_FREE else 0 end) as [S],
      SUM(Case when pd.SKU_CODE='01bu0906006514q002' then ph.STOCK_UNITS_FREE else 0 end) as [M],
      SUM(Case when pd.SKU_CODE='01bu0906006514q003' then ph.STOCK_UNITS_FREE else 0 end) as [L],
      SUM(Case when pd.SKU_CODE='01bu0906006514q004' then ph.STOCK_UNITS_FREE else 0 end) as [XL],
      SUM(Case when pd.SKU_CODE='01bu0906006514q005' then ph.STOCK_UNITS_FREE else 0 end) as [XXL]  
      FROM         RMIS.dbo.product_detail AS pd INNER JOIN RMIS.dbo.PRODUCT_HISTORY AS ph ON ph.SKU_ID = pd.SKU_ID
      WHERE     (ph.WEEK_SELECTOR = 1100230) GROUP BY ph.BRANCH_ID
)

, Query2 as
(
      SELECT SUM(CASE WHEN pmh.TXN_DATE_TIME > (GETDATE() - 360) THEN units ELSE 0 END) AS Last360,  
      SUM(CASE WHEN pmh.TXN_DATE_TIME > (GETDATE() - 90) THEN units ELSE 0 END) AS Last90,
      SUM(CASE WHEN pmh.TXN_DATE_TIME > (GETDATE() - 30)  THEN units ELSE 0 END) AS Last30,
      SUM(CASE WHEN pmh.TXN_DATE_TIME > (GETDATE() - 7) THEN units ELSE 0 END) AS Last7, pmh.BRANCH_ID
      FROM         RMIS.dbo.PM_TRANS_HEADER AS pmh INNER JOIN RMIS.dbo.PM_TRANS_SKU_DETAILS AS pmskd ON pmh.RECEIPT_ID = pmskd.RECEIPT_ID
      INNER JOIN RMIS.dbo.product_detail AS pd ON pd.SKU_ID = pmskd.SKU_ID
      WHERE     (pmh.FLAG = 1) AND (pmh.TRANSACTION_VOID = 0) AND (pmskd.FLAG = 1) AND (pd.SKU_CODE LIKE '01bu0906006514q%')
      GROUP BY pmh.BRANCH_ID
)

select      *
from      Query1
inner join Query2 on
      Query1.Branch_Id = Query2.Branch_Id
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 34234171
But if you decide to go this route, name the CTE's something better than Query1 and Query 2 :)
0
 

Author Comment

by:tosh2000
ID: 34234233
Thanks
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

626 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