March 7, 2017 at 7:21 pm
TABLE dbo.codes --cod
( codeID (pk uniqueidentifier NOT NULL),
trackid (varchar(50) NOT NULL),
confirmed datetime NULL,
code (varchar(2) NULL),
CONSTRAINT PK_codeID PRIMARY KEY CLUSTERED (codeID ASC) );
select cu.customerid, ,[CONFIRMED_LAST_WK] = sum(case when (select convert(date,cod.confirmed) from Codes cod where o.trackid = cod.trackid and cod.confirmed <>'') >= DATEADD(DAY, -8 + CASE DATEDIFF(DAY, 0, GETDATE()) % 7 WHEN 0 THEN 1 WHEN 5 THEN 2 WHEN 6 THEN 1 ELSE 0 END, GETDATE()) then 1 else 0 end) from orders o group by customerid
But this is getting "Cannot perform an aggregate function on an expression containing an aggregate or a subquery." error, i cant find a good way to subquery this
??? Thanks in advance
March 7, 2017 at 7:51 pm
Post DDL statement and sample data insert statements to ensure get responses.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle
March 7, 2017 at 11:17 pm
You can try something like this. NOTE: The lack of data or expected results means that this code is untested.WITH cteDateCalc AS (
SELECT
trackid
, confirmed
, CalcDate = DATEADD(DAY, -8 + CASE DATEDIFF(DAY, 0, GETDATE()) % 7
WHEN 0 THEN 1
WHEN 5 THEN 2
WHEN 6 THEN 1
ELSE 0
END, GETDATE())
FROM #Codes
WHERE confirmed IS NOT NULL
)
SELECT
o.CustomerID
, CONFIRMED_LAST_WK = SUM(CASE WHEN cte.confirmed >= cte.CalcDate THEN 1
ELSE 0
END)
FROM #orders AS o
INNER JOIN cteDateCalc AS cte
ON o.trackid = cte.trackid
GROUP BY o.CustomerID
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply