T-SQL:我正在尝试获取一列的最新行以及另一列的总和

时间:2019-02-28 10:39:02

标签: sql-server tsql reporting-services

我有下表,它显示SalesQtyStockQtyArticleSupplierBranch分组的Month

╔════════╦════════╦══════════╦═════════╦══════════╦══════════╗
║ Month  ║ Branch ║ Supplier ║ Article ║ SalesQty ║ StockQty ║
╠════════╬════════╬══════════╬═════════╬══════════╬══════════╣
║ 201811 ║    333 ║        2 ║    3122 ║        4 ║ 11       ║
║ 201811 ║    345 ║        1 ║    1234 ║        2 ║ 10       ║
║ 201811 ║    345 ║        1 ║    4321 ║        3 ║ 11       ║
║ 201812 ║    333 ║        2 ║    3122 ║        2 ║ 4        ║
║ 201812 ║    345 ║        1 ║    1234 ║        3 ║ 12       ║
║ 201812 ║    345 ║        1 ║    4321 ║        4 ║ 5        ║
║ 201901 ║    333 ║        2 ║    3122 ║        1 ║ 8        ║
║ 201901 ║    345 ║        1 ║    1234 ║        6 ║ 9        ║
║ 201901 ║    345 ║        1 ║    4321 ║        2 ║ 8        ║
║ 201902 ║    333 ║        2 ║    3122 ║        7 ║ NULL     ║
║ 201902 ║    345 ║        1 ║    1234 ║        4 ║ 13       ║
║ 201902 ║    345 ║        1 ║    4321 ║        1 ║ 10       ║
╚════════╩════════╩══════════╩═════════╩══════════╩══════════╝

现在,我想对SalesQty求和并得到最新 StockQty并按Article, Supplier, Branch进行分组。

最终结果应如下所示:

╔════════╦══════════╦═════════╦═════════════╦════════════════╗
║ Branch ║ Supplier ║ Article ║ SumSalesQty ║ LatestStockQty ║
╠════════╬══════════╬═════════╬═════════════╬════════════════╣
║    333 ║        2 ║    3122 ║          14 ║           NULL ║
║    345 ║        1 ║    1234 ║          15 ║             13 ║
║    345 ║        1 ║    4321 ║          10 ║             10 ║
╚════════╩══════════╩═════════╩═════════════╩════════════════╝

我已经尝试过了,但是它给了我一个错误,在这种情况下我不知道该做什么。

我已经制作了这个示例,因此您可以自己尝试。 db<>fiddle

SELECT      
    Branch, 
    Supplier,
    Article,
    SumSalesQty = SUM(SalesQty),
    -- my attempt
    LatestStockQty = (SELECT StockQty FROM TestTable i 
                      WHERE MAX(Month) = Month
                        AND TT.Branch = i. Branch
                        AND TT.Supplier = i.Branch
                        AND TT.Article = i.Branch)
FROM        
    TestTable TT
GROUP BY    
    Branch, Supplier, Article

谢谢您的帮助!

2 个答案:

答案 0 :(得分:3)

我们可以在此处尝试使用ROW_NUMBER来隔离每个组的最新记录:

WITH cte AS (
    SELECT t.*, ROW_NUMBER() OVER (PARTITION BY Branch, Supplier, Article
                                   ORDER BY Month DESC) rn,
        SUM(SalesQty) OVER (PARTITION BY Branch, Supplier, Article) SumSalesQty
    FROM TestTable t
)

SELECT
    Month,
    Branch,
    Supplier,
    Article,
    SumSalesQty,
    StockQty
FROM cte
WHERE rn = 1;

在CTE中,我们为每个Branch / Supplier / Article组计算一个行号值,从最近一个月的1开始。我们还计算同一分区上的销售数量总和。然后,我们只需从该CTE中选择行号等于1的所有行。

enter image description here

Demo

答案 1 :(得分:3)

一种类似的方法,但没有CTE

    SELECT top 1 with ties  
              Branch 
            , Supplier
            , Article
            , SUM(SalesQty) OVER (PARTITION BY Branch, Supplier, Article) SumSalesQty
            , tt.StockQty  as LatestStockQty
FROM        TestTable TT
order by    ROW_NUMBER() OVER (PARTITION BY Branch, Supplier, Article ORDER BY Month DESC)