CTE 用户定义函数分组依据和行重复

CTE user defined functions group by and row duplications

我有多个 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 的数据的记录

删除除客户以外的所有分组,如果您想要每个客户和预算年的记录,请按预算年添加一个分组,否则也将其删除。

WITH cte AS 
( SELECT ISNULL(g.last_id, 1)
       + ROW_NUMBER() OVER ( ORDER BY CustomerId ) AS RowId
       ,c.[CustomerId] AS CustomerId
       ,b.Id
       ,YEAR(c.CreateDate) bYear
       ,4 AS EntityState
       ,max(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 , b.Id,YEAR(c.CreateDate)
), 
cte1 AS 
( SELECT SUM(dbo.GetContractCost(Id)) AS ProcedureCost
       , c.[CustomerId] AS CustomerId -- int
       , YEAR(c.CreateDate) bYear
  FROM     dbo.Contract c
  WHERE    c.PurchaseMethodId <> 15
  GROUP BY c.[CustomerId], YEAR(c.CreateDate)
),
cte2 AS 
( SELECT SUM(dbo.GetContractCost(c.Id)) AS SingleVendorCost
       , c.[CustomerId] AS CustomerId -- int
       , YEAR(c.CreateDate) bYear  
  FROM dbo.Contract c
  JOIN PurchaseSingleVendor p ON c.PurchaseSingleVendorId = p.Id
  WHERE    c.PurchaseMethodId = 15
    AND c.PurchaseSingleVendorId = 16
    AND c.PurchaseSingleVendorId = 17
  GROUP BY [c].[CustomerId], YEAR(c.CreateDate)
),
cte3 AS 
( SELECT SUM(dbo.GetContractCost(Id)) AS [CanceledProcedureCost]
       , c.[CustomerId] AS CustomerId -- int
       , YEAR(c.CreateDate) bYear
  FROM     dbo.Contract c
  WHERE    c.PurchaseMethodId = 15
  GROUP BY [c].[CustomerId], YEAR(c.CreateDate)
),
cte4 AS 
( SELECT SUM(dbo.GetContractCost(Id)) AS SingleVendorCost
       , c.[CustomerId] AS CustomerId -- int
       , YEAR(c.CreateDate) bYear
  FROM     dbo.Contract c
  WHERE    c.PurchaseMethodId = 15
  GROUP BY [c].[CustomerId], YEAR(c.CreateDate)
)
SELECT      cte.RowId
        -- ,cte1.CanceledProcedureCost
           ,cte1.ProcedureCost
           ,cte2.SingleVendorCost
           ,cte3.CanceledProcedureCost
           ,cte4.SingleVendorCost
           ,cte.Id AS YearId
           ,cte.bYear
           ,cte.CustomerId
           ,cte.contractid
           ,4
     FROM   cte
            LEFT JOIN cte1 ON cte.CustomerId= cte1.Customerid
                              and cte.bÝear = cte1.bYear
            LEFT  JOIN cte2 ON cte.CustomerId= cte2.Customerid
                              and cte.bÝear = cte2.bYear
            LEFT JOIN cte3 ON cte.CustomerId= cte3.Customerid
                              and cte.bÝear = cte3.bYear
            LEFT JOIN cte4 ON cte.CustomerId= cte4.Customerid
                              and cte.bÝear = cte4.bYear