Join 2 result sets, grouped by date and by hour

Hi,  

We are comparing sales by hour for our store, and I am currently using 2 separate queries.  

I'd like to combine these results into one result set, by making the 2 queries derived tables, and joining them.   So the second result set in the attached screenshot would be additional columns added to the first result set.   I was having a hard time getting this to work correctly.  

Thanks in advance for a solution on this. Screen Shot

USE STORESQL


SELECT 
		
		--I.F22,
		--R.F1041 AS ITEM_DESCR,
		--R.F01 AS ITEM_NO,
		 DATEPART(HOUR, R.F253) as SalesHour,
		 CONVERT(DATE,R.F253) AS Sales_Day,
         SUM(R.F65) AS TotalSales,
		 SUM(R.F64) AS UnitSales
FROM 

	SAL_REG_SAV R
	JOIN SAL_HDR_SAV H ON 
		R.F1505 = H.F1505
		AND R.F1056 = H.F1056
		AND R.F1057 = H.F1057
		AND R.F1032 = H.F1032
	JOIN POS_TAB P ON R.F01 = P.F01
	JOIN OBJ_TAB I ON R.F01 = I.F01


WHERE
R.F1505 = '12-14-2018'
AND R.F1056 = '047'

GROUP BY  DATEPART(HOUR, R.F253), CONVERT(DATE,R.F253)
ORDER BY CONVERT(DATE,R.F253), DATEPART(HOUR, R.F253)


SELECT 
		
		--I.F22,
		--R.F1041 AS ITEM_DESCR,
		--R.F01 AS ITEM_NO,
		 DATEPART(HOUR, R.F253) as SalesHour,
		 CONVERT(DATE,R.F253) AS Sales_Day,
         SUM(R.F65) AS TotalSales,
		 SUM(R.F64) AS UnitSales
FROM 

	SAL_REG_SAV R
	JOIN SAL_HDR_SAV H ON 
		R.F1505 = H.F1505
		AND R.F1056 = H.F1056
		AND R.F1057 = H.F1057
		AND R.F1032 = H.F1032
	JOIN POS_TAB P ON R.F01 = P.F01
	JOIN OBJ_TAB I ON R.F01 = I.F01


WHERE
R.F1505 = '12-15-2018'
AND R.F1056 = '047'

GROUP BY  DATEPART(HOUR, R.F253), CONVERT(DATE,R.F253)
ORDER BY CONVERT(DATE,R.F253), DATEPART(HOUR, R.F253) 

Open in new window

AD1080Asked:
Who is Participating?
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.

SharathData EngineerCommented:
try this.
USE STORESQL

;WITH CTE1 AS (
SELECT 
		
		--I.F22,
		--R.F1041 AS ITEM_DESCR,
		--R.F01 AS ITEM_NO,
		 DATEPART(HOUR, R.F253) as SalesHour,
		 CONVERT(DATE,R.F253) AS Sales_Day,
         SUM(R.F65) AS TotalSales,
		 SUM(R.F64) AS UnitSales
FROM 

	SAL_REG_SAV R
	JOIN SAL_HDR_SAV H ON 
		R.F1505 = H.F1505
		AND R.F1056 = H.F1056
		AND R.F1057 = H.F1057
		AND R.F1032 = H.F1032
	JOIN POS_TAB P ON R.F01 = P.F01
	JOIN OBJ_TAB I ON R.F01 = I.F01


WHERE
R.F1505 = '12-14-2018'
AND R.F1056 = '047'

GROUP BY  DATEPART(HOUR, R.F253), CONVERT(DATE,R.F253)
),
CTE2 AS (
SELECT 
		
		--I.F22,
		--R.F1041 AS ITEM_DESCR,
		--R.F01 AS ITEM_NO,
		 DATEPART(HOUR, R.F253) as SalesHour,
		 CONVERT(DATE,R.F253) AS Sales_Day,
         SUM(R.F65) AS TotalSales,
		 SUM(R.F64) AS UnitSales
FROM 

	SAL_REG_SAV R
	JOIN SAL_HDR_SAV H ON 
		R.F1505 = H.F1505
		AND R.F1056 = H.F1056
		AND R.F1057 = H.F1057
		AND R.F1032 = H.F1032
	JOIN POS_TAB P ON R.F01 = P.F01
	JOIN OBJ_TAB I ON R.F01 = I.F01


WHERE
R.F1505 = '12-15-2018'
AND R.F1056 = '047'

GROUP BY  DATEPART(HOUR, R.F253), CONVERT(DATE,R.F253)
)
SELECT c1.*, c2.Sales_Day, c2.TotalSales,c2.UnitSales 
  FROM CTE1 c1
  JOIN CTE2 c2 ON c1.SalesHour = c2.SalesHour
ORDER BY SalesHour

Open in new window

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
AD1080Author Commented:
Thanks that worked.
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

From novice to tech pro — start learning today.