CTE用户定义的功能分组和行重复

时间:2015-03-31 07:33:50

标签: sql-server select group-by distinct common-table-expression

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

1 个答案:

答案 0 :(得分:1)

除了客户之外删除所有组,如果您想要每个客户和budgetyear记录,请按budgetyear添加组,否则也删除它们。

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