我有查詢多個CTE在它每個cte做計算,比我從這個cte檢索需要我的奶牛,它的工作,但我的問題是我來的客戶誰可以有多個合同和他們在做重複我試圖通過使用不同的或組來照亮它,但它並沒有在這裏工作 是查詢其回答CTE用戶定義的功能組和行重複
WITH cte
AS (SELECT ISNULL(g.last_id
+ ROW_NUMBER() OVER (ORDER BY CustomerId), 1) AS RowId
,c.[CustomerId] AS CustomerId
,b.Id
,4 AS EntityState
,c.Id AS contractid
FROM dbo.Contract c
JOIN dbo.BudgetYear AS b ON YEAR(c.CreateDate) = b.Year
CROSS JOIN (SELECT MAX(Id) AS last_id
FROM [ContractConclusionStatistic]
) g
GROUP BY c.[CustomerId]
,last_id
,c.Id
,b.Id
),
cte1
AS (SELECT SUM(dbo.GetContractCost(Id)) OVER (PARTITION BY [CustomerId]) AS ProcedureCost
,c.[CustomerId] AS CustomerId -- int
,c.Id AS contractid
FROM dbo.Contract c
WHERE c.PurchaseMethodId <> 15
GROUP BY c.[CustomerId]
,Id
),
cte2
AS (SELECT SUM(dbo.GetContractCost(c.Id)) OVER (PARTITION BY [CustomerId]) AS SingleVendorCost
,c.[CustomerId] AS CustomerId -- int
,c.Id AS contractid
FROM dbo.Contract c
JOIN PurchaseSingleVendor p ON c.PurchaseSingleVendorId = p.Id
WHERE c.PurchaseMethodId = 15
AND c.PurchaseSingleVendorId = 16
AND c.PurchaseSingleVendorId = 17
--and 2 more
GROUP BY [c].[CustomerId]
,c.Id
),
cte3
AS (SELECT SUM(dbo.GetContractCost(Id)) OVER (PARTITION BY [CustomerId]) AS [CanceledProcedureCost]
,c.[CustomerId] AS CustomerId -- int
,c.Id AS contractid
FROM dbo.Contract c
WHERE c.PurchaseMethodId = 15
GROUP BY [c].[CustomerId]
,c.Id
),
cte4
AS (SELECT SUM(dbo.GetContractCost(Id)) OVER (PARTITION BY [CustomerId]) AS SingleVendorCost
,c.[CustomerId] AS CustomerId -- int
,c.Id AS contractid
FROM dbo.Contract c
WHERE c.PurchaseMethodId = 15
GROUP BY [c].[CustomerId]
,c.Id
)
SELECT DISTINCT
cte.RowId
-- ,cte1.CanceledProcedureCost
,cte1.ProcedureCost
,cte2.SingleVendorCost
,cte3.CanceledProcedureCost
,cte4.SingleVendorCost
,cte.Id AS YearId
,cte.CustomerId
,cte.contractid
,4
FROM cte
LEFT JOIN cte1 ON cte.contractid = cte1.contractid
LEFT JOIN cte2 ON cte2.CustomerId = cte.CustomerId
AND cte.contractid = cte2.contractid
LEFT JOIN cte3 ON cte3.CustomerId = cte.CustomerId
AND cte.contractid = cte3.contractid
LEFT JOIN cte4 ON cte4.CustomerId = cte.CustomerId
AND cte.contractid = cte4.contractid
GROUP BY cte.CustomerId
,cte.RowId
,cte1.ProcedureCost
,cte2.SingleVendorCost
,cte.contractid
,cte3.CanceledProcedureCost
,cte4.SingleVendorCost
,cte.Id
54 NULL NULL 174000.00 174000.00 4 18000 1100253 4
55 NULL NULL 174000.00 174000.00 4 18000 1100254 4
56 345191000.00 NULL NULL NULL 4 18000 1100261 4
57 345191000.00 NULL NULL NULL 4 18000 1100262 4
58 345191000.00 NULL NULL NULL 4 18000 1100276 4
59 345191000.00 NULL NULL NULL 4 18000 1100286 4
60 345191000.00 NULL NULL NULL 4 18000 1100297 4
61 NULL NULL 180000.00 180000.00 4 21065 1100188 4
62 NULL NULL NULL NULL 4 21065 1100232 4
63 NULL NULL 180000.00 180000.00 4 21065 1100255 4
64 NULL NULL 180000.00 180000.00 4 21065 1100256 4
65 NULL NULL 180000.00 180000.00 4 21065 1100257 4
任何想法如何擺脫掉重複? 這裏我選擇cte.Contractid
剛剛從那裏複製從原來的查詢來表明我不選擇此列,但仍收到重複
這裏就是我想實現
55 345191000.00 NULL 174000.00 174000.00 4 18000 1100254 4
56 NULL NULL 180000.00 180000.00 4 21065 1100257 4
每個客戶ID我需要有一條記錄來自多個CTE的數據
請提供預期的輸出。 – 2015-03-31 07:43:22
@Aツ檢查原始問題 – Andrey 2015-03-31 07:48:30
這一年會發生什麼? – 2015-03-31 07:59:14