I have an sql statement like so:
SELECT a.TDate, WeekStart=a.TDate-Cast(a.TDate - '11-Aug-2004' AS int) % 7,
Adjusted=Case WHEN a.TDate <> '11-Aug-2004' THEN
Case WHEN a.Total > 24000 THEN
a.Total/4 ELSE
Case WHEN a.Total > 0 THEN
a.Total ELSE
0 -- here is where the problem needs to be solved!!!
END END
ELSE Case WHEN a.Total > 24000 THEN
a.Total/4 ELSE
a.Total END END
FROM(SELECT TDate, Sum(Scores) AS Total
FROM Test
WHERE (TDate >= '11-Aug-2004' AND TDate <= '24-Aug-2004')
AND Code In ('ABC')
AND Scores>0
GROUP BY TDate
) AS a
It returns a result set like so:
2004-08-11 00:00:00 2004-08-11 00:00:00 13667.180
2004-08-12 00:00:00 2004-08-11 00:00:00 0
2004-08-13 00:00:00 2004-08-11 00:00:00 12620.891
2004-08-14 00:00:00 2004-08-11 00:00:00 7764.918
2004-08-15 00:00:00 2004-08-11 00:00:00 7551.753
2004-08-16 00:00:00 2004-08-11 00:00:00 13562.814
2004-08-17 00:00:00 2004-08-11 00:00:00 15161.205
2004-08-18 00:00:00 2004-08-18 00:00:00 14780.539
2004-08-19 00:00:00 2004-08-18 00:00:00 14295.927
2004-08-20 00:00:00 2004-08-18 00:00:00 12648.412
2004-08-21 00:00:00 2004-08-18 00:00:00 10163.869
2004-08-22 00:00:00 2004-08-18 00:00:00 7691.478
2004-08-23 00:00:00 2004-08-18 00:00:00 13482.235
2004-08-24 00:00:00 2004-08-18 00:00:00 15543.685
Here is the problem, at the moment one of the case statements says if Total is not greater than zero then return zero, what it is supposed to say is, if the Total is not greater than zero then return the value for the previous day.
In other words, looking at the result set above, if 2004-08-12 Total is not greater than zero, then the total should equal that of 2004-08-11, giving a result set like so:
2004-08-11 00:00:00 2004-08-11 00:00:00 13667.180
2004-08-12 00:00:00 2004-08-11 00:00:00 13667.180
2004-08-13 00:00:00 2004-08-11 00:00:00 12620.891
2004-08-14 00:00:00 2004-08-11 00:00:00 7764.918
.
.
.
Can anyone help?
SELECT a.TDate, WeekStart=a.TDate-Cast(a.TDate - '11-Aug-2004' AS int) % 7,
Adjusted=Case WHEN a.TDate <> '11-Aug-2004' THEN
Case WHEN a.Total > 24000 THEN
a.Total/4 ELSE
Case WHEN a.Total > 0 THEN
a.Total ELSE
0 -- here is where the problem needs to be solved!!!
END END
ELSE Case WHEN a.Total > 24000 THEN
a.Total/4 ELSE
a.Total END END
FROM(SELECT TDate, Sum(Scores) AS Total
FROM Test
WHERE (TDate >= '11-Aug-2004' AND TDate <= '24-Aug-2004')
AND Code In ('ABC')
AND Scores>0
GROUP BY TDate
) AS a
It returns a result set like so:
2004-08-11 00:00:00 2004-08-11 00:00:00 13667.180
2004-08-12 00:00:00 2004-08-11 00:00:00 0
2004-08-13 00:00:00 2004-08-11 00:00:00 12620.891
2004-08-14 00:00:00 2004-08-11 00:00:00 7764.918
2004-08-15 00:00:00 2004-08-11 00:00:00 7551.753
2004-08-16 00:00:00 2004-08-11 00:00:00 13562.814
2004-08-17 00:00:00 2004-08-11 00:00:00 15161.205
2004-08-18 00:00:00 2004-08-18 00:00:00 14780.539
2004-08-19 00:00:00 2004-08-18 00:00:00 14295.927
2004-08-20 00:00:00 2004-08-18 00:00:00 12648.412
2004-08-21 00:00:00 2004-08-18 00:00:00 10163.869
2004-08-22 00:00:00 2004-08-18 00:00:00 7691.478
2004-08-23 00:00:00 2004-08-18 00:00:00 13482.235
2004-08-24 00:00:00 2004-08-18 00:00:00 15543.685
Here is the problem, at the moment one of the case statements says if Total is not greater than zero then return zero, what it is supposed to say is, if the Total is not greater than zero then return the value for the previous day.
In other words, looking at the result set above, if 2004-08-12 Total is not greater than zero, then the total should equal that of 2004-08-11, giving a result set like so:
2004-08-11 00:00:00 2004-08-11 00:00:00 13667.180
2004-08-12 00:00:00 2004-08-11 00:00:00 13667.180
2004-08-13 00:00:00 2004-08-11 00:00:00 12620.891
2004-08-14 00:00:00 2004-08-11 00:00:00 7764.918
.
.
.
Can anyone help?