【发布时间】:2015-06-04 11:07:09
【问题描述】:
我有多个 CTE 的查询,每个 cte 都在进行计算,然后我从这个 cte 中检索需要我的奶牛,它的工作,但我的问题是我来的客户可以拥有多个合同,他们正在做重复我试图阐明通过使用 distinct 或 group by 但它不起作用 这是查询及其答案
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 的数据
【问题讨论】:
-
请提供预期的输出。
-
@Aツ查原题
-
这一年会发生什么?
-
@Aツ 没什么,我只是从不同的表中选择年份 ID,但是如果年份 ID 例如将是 5 insde of 4,例如它应该为此 CustomerID 创建新记录
标签: sql-server select group-by distinct common-table-expression