从每个客户的多个记录创建一行的每月列表

时间:2016-05-03 16:31:58

标签: sql-server tsql

我正在尝试编写每个月都会列出一个列表的代码。该列表为每个客户提取多条记录,我需要结果集为每位客户返回一行,并提供各种汇总信息。

因此,当我运行以下代码时,我得到Max日期的正确答案,为我提供该客户的最后一张发票,我得到正确的答案,总计所有发票仍然打开的金额。将有其他字段显示帐户的老化。但是我得到的每个客户的行数和我的发票一样多,每行显示两个聚合字段的“正确”答案。

我开始查找Distinct,并且可以看到大多数答案都会返回Windowing函数。我之前没有使用它们,我对它们的工作感到困惑。输出需要按特定的顺序排列,这就是为什么我按照我的方式列出的东西。在这一点上,我读了很多帮助论坛,我感觉很醉,所以我觉得我错过了一个明显的答案。

SELECT  cl.[document type] AS [RecordType], 
        c.[no_], 
        c.[name], 
        c.[name 2], 
        c.[address], 
        c.[address 2], 
        c.city, 
        c.[post code] AS [Zip], 
        c.[county] AS [State], 
        c.[country_region code] AS CountyTrim, 
        Month(Getdate()) AS [FigMonth], 
        Day(Getdate()) AS [FigDay], 
        Year(Getdate()) AS [FigYear], 
        --Next 2 lines should pull last invoice for customer to fill LastSaleDate field 
        (
        SELECT Max(cl2.[posting date]) 
        FROM [dbo].[detailed cust_ ledg_ entry] AS cl2 
        WHERE  cl2.[document type] = 2 AND cl2.[customer no_] = c.[no_]
        ) AS [LastSaleDate], 
        c.[payment terms code] AS [Terms 1], 
        --iif instead of case to fill Terms1Open field 
        Iif (c.[payment terms code] = 'CreditCard/Pre-Auth', 'N', 'Y') AS [Terms1Open], 
        --Now totaling invoices per customer 
        (
        SELECT Sum(cl3.[amount]) 
        FROM [dbo].[detailed cust_ ledg_ entry] AS cl3 
        WHERE cl3.[customer no_] = c.[no_] 
        GROUP BY cl3.[customer no_]
        ) AS [DollarsTotal] 
FROM   [dbo].[customer] AS c 
JOIN [dbo].[detailed cust_ ledg_ entry] AS cl 
    ON c.[no_] = cl.[customer no_] 
JOIN [dbo].[cust_ ledger entry] AS l 
    ON l.[document no_] = cl.[document no_] 
WHERE   l.[open] = 1 
        AND cl.[document type] = 2 
        AND c.[customer posting group] IN ( 'BIKECUST', 'OUTDRCUST' ) 
ORDER  BY c.[no_] 

1 个答案:

答案 0 :(得分:0)

我并不完全确定你要做的是什么,但根据你的SQL,我已经重写了它,以便我认为代码最好的描述是合乎逻辑的。

如果我可以提供建议,如果这是您自己的架构 - 我会认真考虑重命名所有以符合最佳做法。

SELECT   CL.[document type] AS [RecordType]
        ,C.[no_]
        ,C.[name]
        ,C.[name 2]
        ,C.[address] 
        ,C.[address 2] 
        ,C.city 
        ,C.[post code] AS [Zip]
        ,C.[county] AS [State] 
        ,C.[country_region code] AS CountyTrim
        ,Month(Getdate()) AS [FigMonth] 
        ,Day(Getdate()) AS [FigDay] 
        ,Year(Getdate()) AS [FigYear] 
        ,MAX(CL.[posting date]) AS [LastSaleDate]
        ,C.[payment terms code] AS [Terms 1]
        ,CASE  WHEN C.[payment terms code] = 'CreditCard/Pre-Auth' 
               THEN 'N' 
               ELSE 'Y' END AS [Terms1Open]
        ,SUM(CL.[amount]) AS [DollarsTotal] 
  FROM   dbo.Customer C
  JOIN   dbo.[detailed cust_ ledg_ entry] CL
    ON   C.[no_] = CL.[customer no_]
  JOIN   dbo.[cust_ ledger entry]  L 
    ON   L.[document no_] = CL.[document no_]
 WHERE   L.[open] = 1 
   AND   CL.[document type] = 2 
   AND   C.[customer posting group] IN ( 'BIKECUST', 'OUTDRCUST' ) 
 GROUP
    BY   CL.[document type]
        ,C.[no_]
        ,C.[name]
        ,C.[name 2]
        ,C.[address] 
        ,C.[address 2] 
        ,C.city 
        ,C.[post code]
        ,C.[county]   
        ,C.[country_region code]
        ,Month(Getdate())                                              
        ,Day(Getdate())                                                
        ,Year(Getdate())     
        ,CASE  WHEN C.[payment terms code] = 'CreditCard/Pre-Auth' 
               THEN 'N' 
               ELSE 'Y' END
  ORDER  
     BY  C.[no_]