SQL - 在DateDiff上取消周末的平均值

时间:2017-02-02 17:00:58

标签: sql sql-server tsql

我有一个SQL语句,用于计算我们的供应商为发票付款的天数。它只包括工作日,它完美地运作。我的客户现在问我是否可以平均每个供应商延迟那些日子。这是我的代码:

DATEDIFF(dd, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE))
-(DATEDIFF(wk, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) * 2)
-(CASE WHEN DATENAME(dw, PURCHTABLE.CONFIRMEDDLV) = 'Sunday'
       THEN 1
       ELSE 0
       END)
-(CASE WHEN DATENAME(dw, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) = 'Saturday'
       THEN 1
       ELSE 0
       END) AS 'DAYS LATE'

如果我尝试将其放在AVG()中,但它告诉我无法对聚合函数执行聚合函数。

更新:

这是我原来的SQL:

SELECT
    PURCHTABLE.PURCHNAME AS 'VENDOR NAME', 
    PURCHTABLE.ORDERACCOUNT AS 'VENDOR NUMBER',
    COUNT(DISTINCT PURCHTABLE.PURCHID) AS 'PURCHASE ORDER', 
    COUNT(PURCHLINE.LINENUMBER) AS 'NUMBER OF LINES', 
    SUM(PURCHLINE.LINEAMOUNT) AS 'PO PRICE TOTAL',
    DATEDIFF(dd, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE))-(DATEDIFF(wk, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) * 2)-(CASE WHEN DATENAME(dw, PURCHTABLE.CONFIRMEDDLV) = 'Sunday' THEN 1 ELSE 0 END)-(CASE WHEN DATENAME(dw, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) = 'Saturday' THEN 1 ELSE 0 END) AS 'DAYS LATE'
FROM
PURCHTABLE
JOIN 
PURCHLINE ON PURCHLINE.PURCHID = PURCHTABLE.PURCHID
JOIN
VENDPACKINGSLIPJOUR ON VENDPACKINGSLIPJOUR.PURCHID = PURCHTABLE.PURCHID
WHERE
PURCHTABLE.DELIVERYDATE >= '2017-01-01'
AND
PURCHTABLE.DELIVERYDATE <= '2017-01-20'
AND
PURCHTABLE.ORDERACCOUNT = 'VN03526'
GROUP BY  
PURCHTABLE.PURCHNAME, 
PURCHTABLE.ORDERACCOUNT,
PURCHTABLE.DELIVERYDATE,
PURCHTABLE.CONFIRMEDDLV

所以我不太明白我是如何改变你所说的.....我试过这个但它没有用。

SELECT 
    PURCHTABLE.PURCHNAME AS 'VENDOR NAME', 
    PURCHTABLE.ORDERACCOUNT AS 'VENDOR NUMBER'
FROM
    (SELECT
        PURCHTABLE.PURCHNAME AS 'VENDOR NAME', 
        PURCHTABLE.ORDERACCOUNT AS 'VENDOR NUMBER',
        COUNT(DISTINCT PURCHTABLE.PURCHID) AS 'PURCHASE ORDER', 
        COUNT(PURCHLINE.LINENUMBER) AS 'NUMBER OF LINES', 
        SUM(PURCHLINE.LINEAMOUNT) AS 'PO PRICE TOTAL',
        DATEDIFF(dd, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE))-(DATEDIFF(wk, PURCHTABLE.CONFIRMEDDLV, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) * 2)-(CASE WHEN DATENAME(dw, PURCHTABLE.CONFIRMEDDLV) = 'Sunday' THEN 1 ELSE 0 END)-(CASE WHEN DATENAME(dw, MAX(VENDPACKINGSLIPJOUR.DELIVERYDATE)) = 'Saturday' THEN 1 ELSE 0 END) AS 'DAYS LATE'
    FROM
    PURCHTABLE
    JOIN 
    PURCHLINE ON PURCHLINE.PURCHID = PURCHTABLE.PURCHID
    JOIN
    VENDPACKINGSLIPJOUR ON VENDPACKINGSLIPJOUR.PURCHID = PURCHTABLE.PURCHID
    WHERE
    PURCHTABLE.DELIVERYDATE >= '2017-01-01'
    AND
    PURCHTABLE.DELIVERYDATE <= '2017-01-20'
    AND
    PURCHTABLE.ORDERACCOUNT = 'VN03526') A
GROUP BY  
PURCHTABLE.PURCHNAME, 
PURCHTABLE.ORDERACCOUNT,
PURCHTABLE.DELIVERYDATE,
PURCHTABLE.CONFIRMEDDLV

1 个答案:

答案 0 :(得分:1)

您目前拥有的代码将成为更大查询的一部分,例如:

select VendorID
      ,VendorName
      ...
      ...
      ,<Your Days Late Code> as DaysLate
      ,InvoiceAmount
      ...
from tables
group by VendorID
      ,VendorName

所有你需要做的就是平均所有这些是选择你想要做的平均值的其他列,并将整个事物包装在另一个select语句中:

select VendorID
      ,VendorName
      ,avg(DaysLate) as AverageDaysLate
      ,sum(InvoiceAmount) as TotalInvoiceAmount
from(
    select VendorID
          ,VendorName
          ...
          ...
          ,<Your Days Late Code> as DaysLate
          ,InvoiceAmount
          ...
    from tables
    group by VendorID
            ,VendorName
) a
group by VendorID
        ,VendorName