You will have to include DG in your grouping...
Select Consignment_id
,Orders_boxsize
,Orders_boxweight
,Orders_boxlength
,Orders_boxwidth
,Orders_boxdepth
,Order_dispatchment_id
,SUM(Component_weight) as Component_weight
,SUM(_ic) as _ic
,DG
from
(
SELECT dbo.Articles_in_Consignment.Consignment_id,
dbo.ORDER_D.Orders_boxsize,
dbo.ORDER_D.Orders_boxweight,
dbo.ORDER_D.Orders_boxlength,
dbo.ORDER_D.Orders_boxwidth,
dbo.ORDER_D.Orders_boxdepth,
dbo.ORDER_D.Order_dispatchment_id,
SUM(
ISNULL(dbo.COMPONENT.Component_weight, 0) * ISNULL(dbo.ORDER_ALLOCATE.Order_allocate_qty, 0)
) AS Component_weight,
COUNT_BIG(*) AS _ic
,(
SELECT CASE
WHEN EXISTS (
SELECT *
FROM dbo.ORDER_ITEMS
WHERE Component_id IN (1111, 1111)
AND Orders_id = dbo.ORDERS.Orders_id
) THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT)
END
) AS DG
FROM dbo.Articles_in_Consignment
INNER JOIN dbo.ORDER_D
ON dbo.Articles_in_Consignment.Order_dispatchment_id = dbo.ORDER_D.Order_dispatchment_id
INNER JOIN dbo.ORDERS
ON dbo.ORDER_D.Order_dispatchment_id = dbo.ORDERS.Order_dispatchment_id
INNER JOIN dbo.ORDER_ITEMS
ON dbo.ORDERS.Orders_id = dbo.ORDER_ITEMS.Orders_id
INNER JOIN dbo.ORDER_ALLOCATE
ON dbo.ORDER_ITEMS.Order_items_id = dbo.ORDER_ALLOCATE.Order_items_id
INNER JOIN dbo.STOCKIN
ON dbo.ORDER_ALLOCATE.Stockin_id = dbo.STOCKIN.Stockin_id
INNER JOIN dbo.COMPONENT
ON dbo.STOCKIN.Component_id = dbo.COMPONENT.Component_id
WHERE (dbo.ORDERS.Cold = 'No')
GROUP BY
dbo.Articles_in_Consignment.Consignment_id,
dbo.ORDER_D.Orders_boxsize,
dbo.ORDER_D.Orders_boxweight,
dbo.ORDER_D.Orders_boxlength,
dbo.ORDER_D.Orders_boxwidth,
dbo.ORDER_D.Orders_boxdepth,
dbo.ORDER_D.Order_dispatchment_id
) a
Group by
Consignment_id
,Orders_boxsize
,Orders_boxweight
,Orders_boxlength
,Orders_boxwidth
,Orders_boxdepth
,Order_dispatchment_id
,DG
Many of your NULLs may be excluded by the fact that you're using an INNER JOIN between the tables. Try flipping it around:
UPDATE C
SET PriceClassID = 'A'
FROM Customer AS C
LEFT JOIN
(
SELECT CustId, SUM(curybaldue) AS last_sum
FROM SOShipHeader
WHERE OrdDate > @year_ago
AND Status = 'C'
GROUP BY CustID
) AS A ON C.CustId = A.CustId
WHERE
(A.last_sum < 3000 OR A.last_sum IS NULL)
AND C.PriceClassID IN ('CLUB', 'CLUB-E')
AND C.Customer.User7 <= @year_ago
AND C.Customer.User7 > @year_and_month_ago
Incidentally, relatively simple queries like this are usually better on StackOverflow; DBA.SE is meant more for server administration, database design, performance tuning, and such.
Best Answer
If you don't have
NULL
values in theID
column then either allDM
's for one type ofID != 1
and as a result allID
's that have the same value will be updated or none.But as said previously, depending on nullability of the
ID
column you can have suprising results and performance implications.More on
NOT IN
andNOT EXISTS
here.An example of spools being added & multiple table accesses as a result of using
NOT IN
A cleaner way to write your query:
Do note that
NULL
values will be updated as well, you would have to add extra logic such as addingAND a.id IS NOT NULL;
.Resulting cleaner join path:
Example of null values resulting in 0 updates happening:
--> 0 rows affected
--> (500 rows affected)