August 26, 2018 at 12:07 pm
This statement will produce: Select EmpID, Yr, sum(Sales)as Sales from Sales Group by EmpId, Yr With Rollup
EmpID Yr Sales
1 2014 120
1 2015 180
1 2016 250
1 null 550
2 2014 150
2 2012 60
2 2013 210
2 null 420
3 2018 20
3 2017 440
3 2015 130
3 null 590
How can I change the statement above to produce only, I guess read it into a temp table and select from the temp table where the year is equal to 'null'
1 null 550
2 null 420
3 null 590
August 26, 2018 at 6:56 pm
kd11 - Sunday, August 26, 2018 12:07 PMThis statement will produce: Select EmpID, Yr, sum(Sales)as Sales from Sales Group by EmpId, Yr With Rollup
EmpID Yr Sales
1 2014 120
1 2015 180
1 2016 250
1 null 550
2 2014 150
2 2012 60
2 2013 210
2 null 420
3 2018 20
3 2017 440
3 2015 130
3 null 590How can I change the statement above to produce only, I guess read it into a temp table and select from the temp table where the year is equal to 'null'
1 null 550
2 null 420
3 null 590
WHERE GROUPING(EmpID) = 1
Haven't tested it though it should do the job.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 27, 2018 at 9:43 am
kd11 - Sunday, August 26, 2018 12:07 PMThis statement will produce: Select EmpID, Yr, sum(Sales)as Sales from Sales Group by EmpId, Yr With Rollup
EmpID Yr Sales
1 2014 120
1 2015 180
1 2016 250
1 null 550
2 2014 150
2 2012 60
2 2013 210
2 null 420
3 2018 20
3 2017 440
3 2015 130
3 null 590How can I change the statement above to produce only, I guess read it into a temp table and select from the temp table where the year is equal to 'null'
1 null 550
2 null 420
3 null 590
You should only use ROLLUP if you want multiple levels of a hierarchy. Here you only want it based on EmpID, so only use EmpID.Select EmpID, CAST(NULL AS INT) AS Yr, sum(Sales)as Sales from Sales Group by EmpId
Also, WITH ROLLUP has been deprecated in favor of GROUPING SETS(), which I believe were introduced in SQL 2008. You should be using GROUPING SETS() instead of WITH ROLLUP().
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 27, 2018 at 12:12 pm
drew.allen - Monday, August 27, 2018 9:43 AMkd11 - Sunday, August 26, 2018 12:07 PMThis statement will produce: Select EmpID, Yr, sum(Sales)as Sales from Sales Group by EmpId, Yr With Rollup
EmpID Yr Sales
1 2014 120
1 2015 180
1 2016 250
1 null 550
2 2014 150
2 2012 60
2 2013 210
2 null 420
3 2018 20
3 2017 440
3 2015 130
3 null 590How can I change the statement above to produce only, I guess read it into a temp table and select from the temp table where the year is equal to 'null'
1 null 550
2 null 420
3 null 590You should only use ROLLUP if you want multiple levels of a hierarchy. Here you only want it based on EmpID, so only use EmpID.
Select EmpID, CAST(NULL AS INT) AS Yr, sum(Sales)as Sales from Sales Group by EmpId
Also, WITH ROLLUP has been deprecated in favor of GROUPING SETS(), which I believe were introduced in SQL 2008. You should be using GROUPING SETS() instead of WITH ROLLUP().
Drew
While I absolutely agree with that notion, I can see people using common code in a view or function and then picking out the totals that they want.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 28, 2018 at 11:35 am
kd11 - Sunday, August 26, 2018 12:07 PMThis statement will produce: Select EmpID, Yr, sum(Sales)as Sales from Sales Group by EmpId, Yr With Rollup
EmpID Yr Sales
1 2014 120
1 2015 180
1 2016 250
1 null 550
2 2014 150
2 2012 60
2 2013 210
2 null 420
3 2018 20
3 2017 440
3 2015 130
3 null 590How can I change the statement above to produce only, I guess read it into a temp table and select from the temp table where the year is equal to 'null'
1 null 550
2 null 420
3 null 590
SELECT X.emp_id, X.sale_yr, X.sales_amt
FROM (SELECT emp_id, sale_yr, sales_amt
FROM Sales
GROUP BY ROLLUP( emp_id, sale_yr))
AS X (emp_id, sale_yr, sales_amt)
WHERE X.sale_yr IS NULL;
untested.
Please post DDL and follow ANSI/ISO standards when asking for help.
August 28, 2018 at 1:28 pm
Jeff Moden - Sunday, August 26, 2018 6:55 PMkd11 - Sunday, August 26, 2018 12:07 PMThis statement will produce: Select EmpID, Yr, sum(Sales)as Sales from Sales Group by EmpId, Yr With Rollup
EmpID Yr Sales
1 2014 120
1 2015 180
1 2016 250
1 null 550
2 2014 150
2 2012 60
2 2013 210
2 null 420
3 2018 20
3 2017 440
3 2015 130
3 null 590How can I change the statement above to produce only, I guess read it into a temp table and select from the temp table where the year is equal to 'null'
1 null 550
2 null 420
3 null 590WHERE GROUPING(EmpID) = 1
Haven't tested it though it should do the job.
This needs to be HAVING rather than WHERE.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
August 29, 2018 at 3:01 pm
Assuming you don't want the overall total that WITH ROLLUP would give you.
Select
EmpID,
Yr,
Sum(Sales)
From @Table
Group By Grouping Sets ((EmpID,Yr),(EmpID))
Having Grouping_ID(EmpId, Yr) = 1
Although I'm not sure why you do that over:
Select
EmpID,
null Yr,
Sum(Sales)
From @Table
Group By EmpID
August 29, 2018 at 3:51 pm
drew.allen - Tuesday, August 28, 2018 1:28 PMThis needs to be HAVING rather than WHERE.Drew
Correct. My apologies.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply