与上一年的百分比差异

时间:2019-02-05 13:06:08

标签: sql sql-server tsql

尝试在我的数据中添加一个百分比差异列,因为我在查询中显示了最近3年的销售和佣金,并希望列出的每个销售人员的差异百分比显示销售金额的差异以及同一个月的前一年的佣金。因此,对于2017年1月的佣金和销售金额,我想显示一下他们在2018年1月的收入与2017年1月的收入之间是否有所增加或减少。

SELECT TOP (100) PERCENT 
    'ABC' AS CompanyCode, 
    ABC.AR_Salesperson.SalespersonName, 
    ABC.AR_SalespersonCommission.SalespersonDivisionNo, 
    ABC.AR_SalespersonCommission.SalespersonNo, 
    SUM(ABC.AR_SalespersonCommission.InvoiceTotal) AS InvoiceTotalSum, 
    SUM(ABC.AR_SalespersonCommission.CommissionAmt) AS CommissionAmtSum, 
    DATENAME(month, ABC.AR_SalespersonCommission.InvoiceDate) AS Month, 
    DATENAME(year, ABC.AR_SalespersonCommission.InvoiceDate) AS Year, 
    DATEPART(m, ABC.AR_SalespersonCommission.InvoiceDate) AS MonthNumber
FROM
    ABC.AR_Customer INNER JOIN
    ABC.AR_SalespersonCommission ON 
        ABC.AR_Customer.ARDivisionNo = ABC.AR_SalespersonCommission.ARDivisionNo AND 
        ABC.AR_Customer.CustomerNo = ABC.AR_SalespersonCommission.CustomerNo INNER JOIN
    ABC.AR_Salesperson ON 
        ABC.AR_SalespersonCommission.SalespersonDivisionNo = ABC.AR_Salesperson.SalespersonDivisionNo AND 
        ABC.AR_SalespersonCommission.SalespersonNo = ABC.AR_Salesperson.SalespersonNo
GROUP BY 
    ABC.AR_Salesperson.SalespersonName, 
    ABC.AR_SalespersonCommission.SalespersonDivisionNo, 
    ABC.AR_SalespersonCommission.SalespersonNo, 
    DATENAME(month, ABC.AR_SalespersonCommission.InvoiceDate), 
    DATENAME(year, ABC.AR_SalespersonCommission.InvoiceDate), 
    DATEPART(m, ABC.AR_SalespersonCommission.InvoiceDate)
HAVING
    (DATENAME(year, ABC.AR_SalespersonCommission.InvoiceDate) > DATEADD(year, - 4, GETDATE()))
ORDER BY 
    SalespersonName, 
    Year, 
    MonthNumber

尝试了各种方法来获取数据来执行此操作,但是还无法做到,例如使用OVER PARTITION BY等。所需的结果和样品数据在下面的链接中。

Desired Results and Sample Data

1 个答案:

答案 0 :(得分:0)

您可以使用CTE并在前一年加入它:

; WITH L4Y
AS (SELECT TOP (100) PERCENT 
        'ABC' AS CompanyCode, 
        ABC.AR_Salesperson.SalespersonName, 
        ABC.AR_SalespersonCommission.SalespersonDivisionNo, 
        ABC.AR_SalespersonCommission.SalespersonNo, 
        SUM(ABC.AR_SalespersonCommission.InvoiceTotal) AS InvoiceTotalSum, 
        SUM(ABC.AR_SalespersonCommission.CommissionAmt) AS CommissionAmtSum, 
        DATENAME(month, ABC.AR_SalespersonCommission.InvoiceDate) AS Month, 
        DATENAME(year, ABC.AR_SalespersonCommission.InvoiceDate) AS Year, 
        DATEPART(m, ABC.AR_SalespersonCommission.InvoiceDate) AS MonthNumber
    FROM
        ABC.AR_Customer INNER JOIN
        ABC.AR_SalespersonCommission ON 
            ABC.AR_Customer.ARDivisionNo = ABC.AR_SalespersonCommission.ARDivisionNo AND 
            ABC.AR_Customer.CustomerNo = ABC.AR_SalespersonCommission.CustomerNo INNER JOIN
        ABC.AR_Salesperson ON 
            ABC.AR_SalespersonCommission.SalespersonDivisionNo = ABC.AR_Salesperson.SalespersonDivisionNo AND 
            ABC.AR_SalespersonCommission.SalespersonNo = ABC.AR_Salesperson.SalespersonNo
    GROUP BY 
        ABC.AR_Salesperson.SalespersonName, 
        ABC.AR_SalespersonCommission.SalespersonDivisionNo, 
        ABC.AR_SalespersonCommission.SalespersonNo, 
        DATENAME(month, ABC.AR_SalespersonCommission.InvoiceDate), 
        DATENAME(year, ABC.AR_SalespersonCommission.InvoiceDate), 
        DATEPART(m, ABC.AR_SalespersonCommission.InvoiceDate)
    HAVING
        (DATENAME(year, ABC.AR_SalespersonCommission.InvoiceDate) > DATEADD(year, - 4, GETDATE()))
    )
SELECT L4Y.*
    , InvoiceTotalPrevYear = COALESCE(LY.InvoiceTotalSum, 0)
    , CommissionAmtSumPrevYear = COALESCE(LY.CommissionAmtSum, 0)
FROM L4Y
    LEFT JOIN L4Y LY
        ON L4Y.CompanyCode = LY.CompanyCode
        AND L4Y.SalespersonName = LY.SalespersonName
        --...(join on ALL fields except YEAR)
        AND l4y.[Month] = LY.[Month]
        -- Here's where the magic happens:
        AND L4Y.[Year] = (LY.[Year]-1) ;