Hi
I have a problem with the following query.
I need to use a case select in a sub query that has a group by, but i can't seem to correctly link it to the main query, so that the amount display correctly.
Please have a look:
Select SiteID,SiteName,Score as CurrentScore,
(select case @datrange
WHEN 'Daily'
THEN (select ss.score from sites ss where Date between
dateadd(dd,@Datefrom,-1) and
DateAdd(dd,@dateTo,-1)
Group By ss.SiteID)
WHEN 'Monthly'
THEN (select ss.score from sites ss where datefrom between
dateadd(mm,@Datefrom,-1) and
DateAdd(mm,@dateTo,-1)
Group By ss.SiteID)
) As previous1,
(select case @datrange
WHEN 'Daily'
THEN (select ss.score
from sites ss where Date between
dateadd(dd,@Datefrom,-2) and
DateAdd(dd,@dateTo,-2)
Group By ss.SiteID)
WHEN 'Monthly'
THEN (select ss.score
from sites ss where datefrom between
dateadd(mm,@Datefrom,-2) and
DateAdd(mm,@dateTo,-2)
Group By ss.SiteID)
) As previous2
From Site
Where Date Between @Datefrom and @DateTo
Group by
SiteID
So when I do this I obviously get the error of the subquery returns more than one value, I just need to join the Sub query correctly to the main query.
so the values display correctly as follows:
SiteID | Sitename | CurrentScore | Previous1 | Previous2
1 | Jerico | 3.62 | 2.27 | 2.31
2 | Hut | 4.26 | 5.21 | 5.22
Thank you in advance for any help and efforts.