Solved

how do I join grouped query results?

Posted on 2010-11-29
3
245 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now