7

I am trying to figure out how to return the top 10 records for each group of Trans.TranSID.

SELECT a.ABID, a.ABName, t.TranSID, SUM(IIF(TranTypeID = 'CO', td.Qty * CAST(td.Price AS money) * - 1, 
                      td.Qty * CAST(td.Price AS money))) AS TotalSales
FROM         Trans t INNER JOIN
                      TransDetail td ON t.TranID = td.TranID INNER JOIN
                      ABook a ON t.TranABID = a.ABID
WHERE     (t.TranDate BETWEEN CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-01-01 00:00:00', 102)) AND 
           t.TranTypeID in ('SO','CA','CO') AND (t.TranStatus <> 'V')
GROUP BY a.ABID, a.ABName, t.TranSID
HAVING  (NOT (a.ABName LIKE '%cash%'))
ORDER BY t.TranSID, TotalSales Desc

I can add "TOP 10" to the select statement, but that gives me the top 10 accounts regardless of the group. There are 25 groups of Trans.TranSID and I'm trying to get the top 10 only for each group.

4

2 回答 2

10

我想你正在寻找ROW_NUMBER()一个PARTITION BY

SELECT * 
FROM (
    SELECT 
        ROW_NUMBER() OVER(PARTITION BY t.TranSID ORDER BY t.TranSID, SUM(IIF(TranTypeID = 'CO', td.Qty * CAST(td.Price AS money) * - 1, td.Qty * CAST(td.Price AS money))) DESC) as RowNum,            
        a.ABID, 
        a.ABName, 
        t.TranSID, 
        SUM(IIF(TranTypeID = 'CO', td.Qty * CAST(td.Price AS money) * - 1, td.Qty * CAST(td.Price AS money))) AS TotalSales
    FROM Trans t 
       INNER JOIN TransDetail td 
           ON t.TranID = td.TranID 
       INNER JOIN ABook a 
           ON t.TranABID = a.ABID
    WHERE (t.TranDate BETWEEN CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-01-01 00:00:00', 102)) 
       AND t.TranTypeID in ('SO','CA','CO')
       AND (t.TranStatus <> 'V')
    GROUP BY a.ABID, a.ABName, t.TranSID
    HAVING  (NOT (a.ABName LIKE '%cash%'))
) a
WHERE a.RowNum <=10

这将为分组中的每条记录分配一个行号(由 定义的列PARTITION,从 1 到 n。从那里,您可以在其上运行 aSELECT以获取每个组的任意数量的记录。

于 2013-04-30T15:50:06.123 回答
1

我对 t-sql 不是特别熟悉,不幸的是我无法访问 t-sql 数据库来测试这是否可以实现您的目标。

也就是说,我认为这是您可以使用子查询和 ROW_NUMBER 函数完成它的一种方法。

SELECT
    *
FROM (
    SELECT 
        a.ABID
        , a.ABName
        , t.TranSID
        , SUM(IFF(TranTypeID = 'CO'
            , td.Qty * CAST(td.Price AS MONEY) * -1
            , td.Qty * CAST(td.Price AS MONEY))) AS TotalSales
        , ROW_NUMBER() 
            OVER(PARTITION BY t.TranSID 
                ORDER BY SUM(IFF(TranTypeID = 'CO'
                    , td.Qty * CAST(td.Price AS MONEY) * -1
                    , td.Qty * CAST(td.Price AS MONEY))) DESC) AS row
    FROM
        Trans t 
        INNER JOIN TransDetail td 
        ON t.TranID = td.TranID 

        INNER JOIN ABook a 
        ON t.TranABID = a.ABID

    WHERE
        (t.TranDate BETWEEN CONVERT(DATETIME, '2012-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-01-01 00:00:00', 102))
        AND t.TranTypeID in ('SO','CA','CO') 
        AND (t.TranStatus <> 'V')

    GROUP BY 
        a.ABID
        , a.ABName
        , t.TranSID

    HAVING
        (NOT (a.ABName LIKE '%cash%'))
) q

WHERE
    q.row <= 10
于 2013-04-30T15:55:27.973 回答