0

私は以下のCTEを持っており、CTEクエリ定義は私が問題を抱えている部分です。私がやりたいことは、CTE クエリ定義の結果を出力して 1 つのテーブルを生成し、各フィールド (例 'GreaterThan350') がフィールド ヘッダーとして出力され、カウントがレコードとして表示されるようにすることです。以下のスクリプトは、石で設定されていません (CTE を使用する IE)。CTE の結果で動作するソリューションに開かれています。

このような:

ここに画像の説明を入力

このようではなく:

ここに画像の説明を入力

WITH AgedCircGrouped_CTE(PWNum, NumberOfDaysSinceOrderDate, OrderDate, CurrentStage, CircuitID) AS 
(
SELECT 
    cir.[PW Number] AS PWNum
    --Get the correct number of working dayts since the order date by using the fn_WorkDays function
    ,CONVERT(VARCHAR(10),SharedInfo.dbo.fn_WorkDays(cir.[Install Date], GETDATE()),103) AS NumberOfDaysSinceOrderDate 
    ,CONVERT(VARCHAR(10), ISNULL(cir.[Install Date],'01/01/1900'),103) AS  OrderDate--Get the order dates
    ,ISNULL(cirRep.CurrentStage, 'Not Set') AS CurrentStage
    ,cir.[ID] as CircuitID
    FROM Quotebase.dbo.Circuits cir
        LEFT JOIN Quotebase.dbo.CircuitReports cirRep ON Cir.[PW Number] = CirRep.PWNumber
    WHERE Cir.Status='New Circuit Order' 
    --ORDER BY Cir.[PW Number]
)


SELECT 
    COUNT ([NumberOfDaysSinceOrderDate] ) AS GreaterThan350
FROM [AgedCircGrouped_CTE]
WHERE [NumberOfDaysSinceOrderDate] > '350'
UNION ALL
SELECT 
    COUNT ([NumberOfDaysSinceOrderDate] ) AS Between250AND350
FROM [AgedCircGrouped_CTE]
WHERE [NumberOfDaysSinceOrderDate] BETWEEN '250' and '350'
UNION ALL
SELECT 
    COUNT ([NumberOfDaysSinceOrderDate] ) AS Between180AND250
FROM [AgedCircGrouped_CTE]
WHERE [NumberOfDaysSinceOrderDate] BETWEEN '180' and '250'
UNION ALL
SELECT 
    COUNT ([NumberOfDaysSinceOrderDate] ) AS Between120AND180
FROM [AgedCircGrouped_CTE]
WHERE [NumberOfDaysSinceOrderDate] BETWEEN '120' and '180'
UNION ALL
SELECT 
    COUNT ([NumberOfDaysSinceOrderDate] ) AS Between70AND120
FROM [AgedCircGrouped_CTE]
WHERE [NumberOfDaysSinceOrderDate] BETWEEN '70' and '120'
UNION ALL
SELECT 
    COUNT ([NumberOfDaysSinceOrderDate] ) AS Between40AND70
FROM [AgedCircGrouped_CTE]
WHERE [NumberOfDaysSinceOrderDate] BETWEEN '40' and '70'
UNION ALL
SELECT 
    COUNT ([NumberOfDaysSinceOrderDate] ) AS LessThan40
FROM [AgedCircGrouped_CTE]
WHERE [NumberOfDaysSinceOrderDate] < '40'

GO
4

1 に答える 1