的我在SSRS,需要做出一個圖表2年的數據進行比較的圖表。由於ssrs中的圖表只能使用數據列來存儲值,而不能使用行,所以我不得不寫很長的包含很多聯合的查詢。我需要12個月的橫軸,我不能創建一個新的表格或任何用戶定義的功能。一切都必須在一個查詢中。 有沒有更簡單的方法來完成這項任務?SQL Server Reporting Services圖表查詢。如何擺脫工會
SELECT
'01' AS 'MONTH',
(SELECT COUNT(*) FROM Order JOIN Car ON Order.Car_ID = Car.ID JOIN CarType ON Car.CarType_ID = CarType.ID WHERE @Year = YEAR(Order.OrderDate) AND (@GPI IS NULL OR @GPI = CarType.GPI) AND (@CarType_ID IS NULL OR @CarType_ID = CarType.ID) AND '01' = MONTH(Order.OrderDate)) AS 'Orders1',
(SELECT COUNT(*) FROM Order JOIN Car ON Order.Car_ID = Car.ID JOIN CarType ON Car.CarType_ID = CarType.ID WHERE @Year -1 = YEAR(Order.OrderDate) AND (@GPI IS NULL OR @GPI = CarType.GPI) AND (@CarType_ID IS NULL OR @CarType_ID = CarType.ID) AND '01' = MONTH(Order.OrderDate)) AS 'Orders2'
UNION
SELECT
'02' AS 'MONTH',
(SELECT COUNT(*) FROM Order JOIN Car ON Order.Car_ID = Car.ID JOIN CarType ON Car.CarType_ID = CarType.ID WHERE @Year = YEAR(Order.OrderDate) AND (@GPI IS NULL OR @GPI = CarType.GPI) AND (@CarType_ID IS NULL OR @CarType_ID = CarType.ID) AND '02' = MONTH(Order.OrderDate)) AS 'Orders1',
(SELECT COUNT(*) FROM Order JOIN Car ON Order.Car_ID = Car.ID JOIN CarType ON Car.CarType_ID = CarType.ID WHERE @Year -1 = YEAR(Order.OrderDate) AND (@GPI IS NULL OR @GPI = CarType.GPI) AND (@CarType_ID IS NULL OR @CarType_ID = CarType.ID) AND '02' = MONTH(Order.OrderDate)) AS 'Orders2'
--It goes on for 12 months
謝謝!這很棒! – Beninja2 2012-03-07 16:06:54