Hello,
I'd like to report on Weekly and YTD measures in the same report, preferable using one query, instead of several (and then filter) also have an ability to group by some other dimensions, e.g. depots, dates or periods.
Adding date to the queries breaks the report aggregations if that field is not included as a column, eg. this query:
(
SELECT
'PP' KPI,
dbo.view_BO_Perf8.Date,
Sum(dbo.view_BO_Perf8.DailyRouteTargetPunctuality*dbo.view_BO_Perf8.CntPunc)/Sum(dbo.view_BO_Perf8.CntPunc) Target,
100*Sum(dbo.view_BO_Perf8.FndPunc)/Sum(dbo.view_BO_Perf8.CntPunc) Actual
FROM
dbo.view_BO_Perf8
WHERE
( dbo.view_BO_Perf8.ParentRoute <> 'City Shuttle' )
AND ( dbo.view_BO_Perf8.ParentRoute <> 'City Shuttle' )
AND
(
rtrim(convert(char,dbo.view_BO_Perf8.Date,111)) >= rtrim(convert(char,dateadd(d, -6, dateadd(d, 6, DATEADD(wk, DATEDIFF(wk,0, getdate() ), -7))) ,111))
AND
rtrim(convert(char,dbo.view_BO_Perf8.Date,111)) <= rtrim(convert(char,dateadd(d, 6, DATEADD(wk, DATEDIFF(wk,0, getdate() ), -7)) ,111))
AND
dbo.view_BO_Perf8.ParentRoute <> 'City Shuttle'
)
GROUP BY
dbo.view_BO_Perf8.Date
UNION
SELECT
'SD',
dbo.view_BO_Perf8.Date,
Sum(dbo.view_BO_Perf8.DailyRouteTargetServiceDelivery*dbo.view_BO_Perf8.SchTrips)/Sum(dbo.view_BO_Perf8.SchTrips),
100*(Sum(dbo.view_BO_Perf8.ActTrips)-0.125*Sum(dbo.view_BO_Perf8.FailShorts))/Sum(dbo.view_BO_Perf8.SchTrips)
FROM
dbo.view_BO_Perf8
WHERE
(
rtrim(convert(char,dbo.view_BO_Perf8.Date,111)) >= rtrim(convert(char,dateadd(d, -6, dateadd(d, 6, DATEADD(wk, DATEDIFF(wk,0, getdate() ), -7))) ,111))
AND
rtrim(convert(char,dbo.view_BO_Perf8.Date,111)) <= rtrim(convert(char,dateadd(d, 6, DATEADD(wk, DATEDIFF(wk,0, getdate() ), -7)) ,111))
)
GROUP BY
dbo.view_BO_Perf8.Date
)
produces correct results and groups data correctly, regardless of what's included in the select/group by clauses.
Removing fields from the WEBI table produces incorrect results because the date column is not used in the WEBI table.
What's the best way to handle this? Should I use aggregate aware function? I need to report on weekly and YTD figures. Is it possible to achieve it using one query?