How to bucket a SQL server query

后端 未结 2 721
-上瘾入骨i
-上瘾入骨i 2021-01-26 12:00

I have the following table in SQL Server:

Date            Browser         Country         Time(ms)
----------------------------------------------------------
201         


        
2条回答
  •  没有蜡笔的小新
    2021-01-26 12:04

    With conditional aggregation you already were on the right track for one possible solution. But have the condition on the country. In a WHERE filter for the durations and stick the different durations together with UNION ALL.

    SELECT '0 - 1s' [Time(ms)],
           count(CASE
                   WHEN [Country] = 'US' THEN
                     1
                 END) [US],
           count(CASE
                   WHEN [Country] = 'JP' THEN
                     1
                 END) [JP]
           FROM [dbo].[mytable]
           WHERE [Time] >= 0
                 AND [Time] < 1000
    UNION ALL
    ...
    UNION ALL
    SELECT '+3s' [Time(ms)],
           count(CASE
                   WHEN [Country] = 'US' THEN
                     1
                 END) [US],
           count(CASE
                   WHEN [Country] = 'JP' THEN
                     1
                 END) [JP]
           FROM [dbo].[mytable]
           WHERE [Time] >= 3000;
    

提交回复
热议问题