COALESCE(DATEPART)未验证

时间:2018-07-02 19:36:51

标签: sql

我正在验证以下查询,在该查询中,我的预期结果是没有SUM收入的任何一周的收入值为0的行。我得到的只是一个0收入记录,我知道有很多记录。有人可以看一下我的代码,看看是否有明显的错误吗?

SELECT dbo.LMCustomer.Name, 
       SUM(dbo.LMDelivery.LdryCensChrg + dbo.LMDelivery.LdryWghtChrg + dbo.LMDelivery.LdryPiecChrg - dbo.LMDelivery.RetnWghtCred - dbo.LMDelivery.RetnPiecCred - dbo.LMDelivery.VrncChrg + dbo.LMDelivery.LdryDelvChrg +
                          dbo.LMDelivery.PrchChrg + dbo.LMDelivery.LdryPcntChrg + dbo.LMDelivery.AuxpChrg01 + dbo.LMDelivery.AuxpChrg02 + dbo.LMDelivery.AuxpChrg03 + dbo.LMDelivery.AuxpChrg04 + dbo.LMDelivery.AuxpChrg05 + dbo.LMDelivery.AuxpChrg06
                          + dbo.LMDelivery.AuxpChrg07 + dbo.LMDelivery.AuxpChrg08 + dbo.LMDelivery.AuxpChrg09 + dbo.LMDelivery.AuxpChrg10 + dbo.LMDelivery.AuxpChrg11 + dbo.LMDelivery.AuxpChrg12 - dbo.LMDelivery.AuxpCred01 - dbo.LMDelivery.AuxpCred02
                          - dbo.LMDelivery.AuxpCred03 - dbo.LMDelivery.AuxpCred04 - dbo.LMDelivery.AuxpCred05 - dbo.LMDelivery.AuxpCred06 - dbo.LMDelivery.AuxpCred07 - dbo.LMDelivery.AuxpCred08 - dbo.LMDelivery.AuxpCred09 - dbo.LMDelivery.AuxpCred10
                          - dbo.LMDelivery.AuxpCred11 - dbo.LMDelivery.AuxpCred12 + dbo.LMDelivery.AuxmChrg01 + dbo.LMDelivery.AuxmChrg02 + dbo.LMDelivery.AuxmChrg03 + dbo.LMDelivery.AuxmChrg04 + dbo.LMDelivery.AuxmChrg05 + dbo.LMDelivery.AuxmChrg06
                          + dbo.LMDelivery.AuxmChrg07 + dbo.LMDelivery.AuxmChrg08 - dbo.LMDelivery.AuxmCred01 - dbo.LMDelivery.AuxmCred02 - dbo.LMDelivery.AuxmCred03 - dbo.LMDelivery.AuxmCred04 - dbo.LMDelivery.AuxmCred05 - dbo.LMDelivery.AuxmCred06
                          - dbo.LMDelivery.AuxmCred07 - dbo.LMDelivery.AuxmCred08) AS Revenue
FROM dbo.LMDelivery INNER JOIN
                         dbo.LMCustomer ON dbo.LMDelivery.ShipCustRcID = dbo.LMCustomer.RcID INNER JOIN
                         dbo.LMContract ON dbo.LMDelivery.ContRcID = dbo.LMContract.RcID
WHERE        (dbo.LMDelivery.UsefCanc = 0) AND (dbo.LMContract.StrtDate >= '2018-01-01') AND (dbo.LMDelivery.LdryDelvDate >= '2018-01-01')
GROUP BY dbo.LMCustomer.RcID, dbo.LMCustomer.Name, COALESCE (DATEPART(week, dbo.LMDelivery.LdryDelvDate), 0)

1 个答案:

答案 0 :(得分:0)

通过使用FROM子句中的LMDelivery表,您的查询仅限于显示有交货(并因此大概是收入)的星期。

如果您希望查看所有客户,所有星期的清单以及任何必要的交货信息,那么您将需要从所有客户和所有星期的清单开始。

假设使用SQL Server,则可以使用CTE列出到目前为止一年中的几周,将其与客户列表联系起来,然后查找任何相关的交货及其相关合同。类似于以下内容:

;WITH 
e1(n) AS
    (
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL 
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
    ), -- 10
e2(n) AS (SELECT ROW_NUMBER() OVER (ORDER BY e1.n) AS n FROM e1 CROSS JOIN e1 AS b), -- 10*10
num(WeekOfYear) AS 
    (
    SELECT  
        n - 1
    FROM e2
    WHERE n <= DATEPART(WEEK, GETDATE()) + 1
    )



SELECT dbo.LMCustomer.Name, 
       SUM(dbo.LMDelivery.LdryCensChrg + dbo.LMDelivery.LdryWghtChrg + dbo.LMDelivery.LdryPiecChrg - dbo.LMDelivery.RetnWghtCred - dbo.LMDelivery.RetnPiecCred - dbo.LMDelivery.VrncChrg + dbo.LMDelivery.LdryDelvChrg +
                          dbo.LMDelivery.PrchChrg + dbo.LMDelivery.LdryPcntChrg + dbo.LMDelivery.AuxpChrg01 + dbo.LMDelivery.AuxpChrg02 + dbo.LMDelivery.AuxpChrg03 + dbo.LMDelivery.AuxpChrg04 + dbo.LMDelivery.AuxpChrg05 + dbo.LMDelivery.AuxpChrg06
                          + dbo.LMDelivery.AuxpChrg07 + dbo.LMDelivery.AuxpChrg08 + dbo.LMDelivery.AuxpChrg09 + dbo.LMDelivery.AuxpChrg10 + dbo.LMDelivery.AuxpChrg11 + dbo.LMDelivery.AuxpChrg12 - dbo.LMDelivery.AuxpCred01 - dbo.LMDelivery.AuxpCred02
                          - dbo.LMDelivery.AuxpCred03 - dbo.LMDelivery.AuxpCred04 - dbo.LMDelivery.AuxpCred05 - dbo.LMDelivery.AuxpCred06 - dbo.LMDelivery.AuxpCred07 - dbo.LMDelivery.AuxpCred08 - dbo.LMDelivery.AuxpCred09 - dbo.LMDelivery.AuxpCred10
                          - dbo.LMDelivery.AuxpCred11 - dbo.LMDelivery.AuxpCred12 + dbo.LMDelivery.AuxmChrg01 + dbo.LMDelivery.AuxmChrg02 + dbo.LMDelivery.AuxmChrg03 + dbo.LMDelivery.AuxmChrg04 + dbo.LMDelivery.AuxmChrg05 + dbo.LMDelivery.AuxmChrg06
                          + dbo.LMDelivery.AuxmChrg07 + dbo.LMDelivery.AuxmChrg08 - dbo.LMDelivery.AuxmCred01 - dbo.LMDelivery.AuxmCred02 - dbo.LMDelivery.AuxmCred03 - dbo.LMDelivery.AuxmCred04 - dbo.LMDelivery.AuxmCred05 - dbo.LMDelivery.AuxmCred06
                          - dbo.LMDelivery.AuxmCred07 - dbo.LMDelivery.AuxmCred08) AS Revenue
FROM 
    dbo.LMCustomer INNER JOIN 
    num ON 1=1 LEFT JOIN 
    dbo.LMDelivery ON dbo.LMDelivery.ShipCustRcID = dbo.LMCustomer.RcID LEFT JOIN
    dbo.LMContract ON dbo.LMDelivery.ContRcID = dbo.LMContract.RcID
WHERE        LMDelivery.ShipCustRcID IS NULL OR (
    (dbo.LMDelivery.UsefCanc = 0) AND (dbo.LMContract.StrtDate >= '2018-01-01') AND (dbo.LMDelivery.LdryDelvDate >= '2018-01-01')
    AND COALESCE (DATEPART(week, LMDelivery.LdryDelvDate), 0) =  num.WeekOfYear
    )
GROUP BY dbo.LMCustomer.RcID, dbo.LMCustomer.NAME, num.WeekOfYear