\$\begingroup\$
\$\endgroup\$
1
I was wondering if you guys know how to reduce the amount of subselects in this SQL Server Query? I think I'm using bad practices in this:
WITH TotalizedRepairOrders AS
(SELECT SalesOrder,Items,DepartmentName, LastCompletedActivity,NextActivity,Plant,ParallelNextActivity FROM OrdersSynthesis)
SELECT Plant,
DepartmentName,
BlindsInQuestioning =( SELECT ISNULL(SUM(Items),0) FROM TotalizedRepairOrders WHERE NextActivity = 'NEW ORDER REVIEW' AND DepartmentName = RO.DepartmentName),
BlindsReadyToCross =( SELECT ISNULL( SUM(Items),0) FROM TotalizedRepairOrders WHERE (LastCompletedActivity = 'LBM INVOICE' OR NextActivity = 'LBM INVOICE' OR NextActivity ='PLANT ARRIVAL') AND DepartmentName = RO.DepartmentName),
BlindsPendingForShipmentCreation = ( SELECT ISNULL(SUM(Items),0) FROM TotalizedRepairOrders WHERE ( NextActivity = 'RETURN SHIPMENT CREATION') AND DepartmentName = RO.DepartmentName),
PendingForInstructions = ( SELECT ISNULL(SUM(Items),0) FROM TotalizedRepairOrders WHERE ( ParallelNextActivity = 'INSTRUCTIONS' ) AND DepartmentName = RO.DepartmentName),
PendingToBeRepaired = ( SELECT ISNULL( SUM(Items),0) FROM TotalizedRepairOrders WHERE ( NextActivity = 'PRODUCTION' ) AND DepartmentName = RO.DepartmentName),
PendingForQualityControl = ( SELECT ISNULL(SUM(Items),0) FROM TotalizedRepairOrders WHERE ( NextActivity = 'INSPECTION') AND DepartmentName = RO.DepartmentName),
PendingToBePacked = ( SELECT ISNULL(SUM(Items),0) FROM TotalizedRepairOrders WHERE ( NextActivity = 'PACKING LABEL') AND DepartmentName = RO.DepartmentName)
FROM TotalizedRepairOrders RO GROUP BY Plant ,DepartmentName
200_success
145k22 gold badges190 silver badges478 bronze badges
-
\$\begingroup\$ Have you tried to pivot the subqueries? sqlhints.com/2014/03/10/pivot-and-unpivot-in-sql-server \$\endgroup\$Tim LaVenice– Tim LaVenice2016年06月27日 18:52:25 +00:00Commented Jun 27, 2016 at 18:52
1 Answer 1
\$\begingroup\$
\$\endgroup\$
3
Here is a simpler method of your query, which doesn't use a CTE and a bunch of sub-selects.
SELECT
Plant
DepartmentName,
SUM(CASE WHEN NextActivity = 'NEW ORDER REVIEW' THEN ISNULL(Items,0) ELSE 0 END) AS BLindsInQuestion,
SUM(CASE WHEN LastCompletedActivity = 'LBM INVOICE' OR NextActivity = 'LBM INVOICE' OR NextActivity ='PLANT ARRIVAL' THEN ISNULL(Items,0) ELSE 0 END) as BlindsReadyToCross
/*
etc
*/
FROM
TotalizedRepairOrders
GROUP BY Plant ,DepartmentName
-
1\$\begingroup\$ Topsy-turvy a little.
NextActivity
is not inGROUP BY
list and will cause compile error.SUM(CASE WHEN...)
\$\endgroup\$IVNSTN– IVNSTN2016年06月27日 18:54:21 +00:00Commented Jun 27, 2016 at 18:54 -
\$\begingroup\$ This is not going to return the expected result set, you need to do the CASE within SUM, not the SUM within CASE:
SUM(CASE WHEN NextActivity = 'NEW ORDER REVIEW' THEN Items ELSE 0 END
AS BlindsInQuestioning \$\endgroup\$dnoeth– dnoeth2016年06月30日 16:21:53 +00:00Commented Jun 30, 2016 at 16:21 -
\$\begingroup\$ Yes, @IvanStarostin pointed that out I just haven't had time to fix it. This was migrated from StackOverFlow. I don't monitor CodeReview that often. \$\endgroup\$S3S– S3S2016年06月30日 16:33:38 +00:00Commented Jun 30, 2016 at 16:33
lang-sql