T-SQL组由2个表组成

时间:2015-12-01 20:43:39

标签: sql tsql group-by

我的表格如下:

enter image description here

对于每个TimeOfDay,我想获得最频繁的类别。例如,如果有3个具有唯一ClosedTime的拍卖,但是每个时间都有TimeOfDay = 1,并且这些拍卖中的2个具有CategoryId = 1并且一个拍卖CategoryId = 2我想得到:

TimeOfDay | CategoryId
1         | 1

我已经尝试过TimeOfDay和CategoryId组,但我仍然不知道如何获得每个TimeOfDay组的顶级类别。我有这个:

select t.TimeOfDay, a.CategoryId, count(a.CategoryId)
numberOfSalesInCategory 
from Auction a
join Time t on t.Id = a.ClosedTime
where IsSuccess = 1
group by t.TimeOfDay, a.CategoryId

和一些样本数据的结果:

TimeOfDay | CategoryId |    numberOfSalesInCategory
0           1               1
1           1               1
1           2               3
2           2               1
0           3               1
3           3               1
3           4               2

所以对于这些数据我想得到:

TimeOfDay | CategoryId
0         | 1 or 3      numberOfSalesInCategory for both is 1
1         | 2           numberOfSalesInCategory is 3
2         | 2           only one category
3         | 4           numberOfSalesInCategory is 2

2 个答案:

答案 0 :(得分:1)

从技术上讲,您正在寻找模式。如果多个值都具有相同的频率,则可以有多种模式。如果您乐意随意选择一个,那么使用row_number()的条件聚合就是解决方案:

select TimeOfDay,
       max(case when seqnum = 1 then CategoryId end) as ModeCategory
from (select t.TimeOfDay, a.CategoryId, count(*) as numberOfSalesInCategory,
             row_number() over (partition by t.TimeOfDay order by count(*) ) as seqnum
      from Auction a join
           Time t
           on t.id = a.ClosedTime
      where a.isSuccess = 1
      group by t.TimeOfDay, a.CategoryId
     ) ta
group by TimeOfDay;

答案 1 :(得分:0)

您可以将当前语句放在CTE中,使用RANK()对它们进行排名,然后执行填充语句。

e.g。

; WITH T AS (SELECT t.TimeOfDay, a.CategoryId, COUNT(a.CategoryId)
    numberOfSalesInCategory 
    FROM Auction a
    JOIN Time t ON t.Id = a.ClosedTime
    WHERE IsSuccess = 1
    GROUP BY t.TimeOfDay, a.CategoryId)
, S AS (SELECT T.*
    , RANK() OVER (PARTITION BY TimeOfDay ORDER BY numberOfSalesInCategory DESC) RankOrder
    FROM T)
SELECT DISTINCT TimeOfDay 
    , STUFF(((SELECT ' or ' + CONVERT(NVARCHAR, CategoryId) 
        FROM S 
        WHERE RankOrder = 1 
        AND TimeOfDay = BloobleBlah.TimeOfDay 
        FOR XML PATH('')), 1, 4, '') CategoryId
FROM S BloobleBlah