0

我有查詢多個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的數據

+1

請提供預期的輸出。 – 2015-03-31 07:43:22

+0

@Aツ檢查原始問題 – Andrey 2015-03-31 07:48:30

+1

這一年會發生什麼? – 2015-03-31 07:59:14

回答

1

如果您想爲每個客戶和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 
+0

創建新記錄thanx很多!我只有一個問題,我需要在第一個CTE中選擇',max(c.Id)AS contractid'? – Andrey 2015-03-31 08:12:18

+1

你的倒數第二列是一些countractId,我不確定哪一個是我最後一個。 – 2015-03-31 08:16:44