?
Solved

how do I join grouped query results?

Posted on 2010-11-29
3
Medium Priority
?
255 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 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

718 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