【问题标题】:Counting the occurrences of a distinct values depending on its date根据日期计算不同值的出现次数
【发布时间】:2016-05-20 05:42:22
【问题描述】:

我正在尝试找到一个 SQL Server 查询,该查询将在特定列中找到不同的值,并根据其日期计算该值的出现次数。

示例数据:

in_date     item_code       
---------   ---------
2015/11/25  item1
2015/11/25  item2
2015/12/10  item1
2015/12/10  item1
2015/12/10  item2
2016/01/01  item1

预期输出:

Date          Item1     Item2
----------    -----     -----
2015/11/25    1         1
2015/12/10    2         1
2016/01/01    1         0

【问题讨论】:

    标签: sql sql-server tsql select distinct


    【解决方案1】:

    试试;

    Select 
      in_date, 
      Sum (case when item_code = item1 then 1 else 0 end) item1,
      Sum (case when item_code = item2 then 1 else 0 end) item2
    From tbl
    Group by in_date
    

    【讨论】:

      猜你喜欢
      • 2016-04-23
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多