I have a field customerid in the orders table that i want to display with some aggregate derived columns , and i want to count the instances a value shows up per customerid. One derived column [SHIPPING TODAY] will have a join into another table via the customerid
TABLE dbo.orders --o
(
OrderID (pk, int, NOT NULL),
CustomerID varchar(5) NOT NULL,
sourceid (pk uniqueidentifier NOT NULL),
Status varchar(50) null,
ordershipped datetime NULL,
orderarrived datetime NULL
CONSTRAINT PK_OrderID PRIMARY KEY CLUSTERED (OrderID ASC)
);
TABLE dbo.shipments --sh
(
evID (pk,varchar(5), NOT NULL),
shipdate datetime NULL,
CustomerID varchar(5) NOT NULL,
location nvarchar(50) not null
CONSTRAINT PK_customerID PRIMARY KEY CLUSTERED (evID ASC)
);
SELECT coalesce (cu.customerid,'Total') AS CUSTID
declare @now datetime
set @now = getdate()
[READY] = sum (case when o.status = 'Confirmed' then 1 else 0 end)
,[SHIPPING TODAY] = (select isnull(sum(case when sh.shipdate = @now then 1 else 0 end),0) from shipments sh where o.customerid = sh.customerid)
FROM Orders o
GROUP BY ROLLUP(cu.customerid);
....I get an accurate total for the READY column,the total for the SHIPPING TODAY column adds up to zero? I have tried Grouping Set but to no avail.
Thanks in advance