sql server 按照用户类型查询统计不同类型的用户数据出现的次数

xiaoweifeifei 2020-06-28 11:23:32
id Account Type Time
1 张三 0 2020-05-22 10:22:33
2 李四 20 2020-05-22 11:22:33
3 王二 1 2020-05-23 10:22:33
4 张三 0 2020-05-24 10:22:33
5 张三 2 2020-05-25 10:42:33
6 张三 0 2020-05-26 10:25:31
7 张三 3 2020-05-26 10:25:33
8 张三 10 2020-05-27 10:25:33
....
以上数据按照日期统计每个type出现的次数(type的类型有0,1,2,3,4,11,20)
希望出现的结果是:
日期 类型0 类型1 类型2 类型3 类型4 类型11 类型20
2020-05-22 1 0 0 0 0 0 1
2020-05-23 0 1 0 0 0 0 0
2020-05-24 1 0 0 0 0 0 0
...全文
336 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
JC34172425 2020-06-28
  • 打赏
  • 举报
回复

create table test(id int ,Account varchar(50),type varchar(50),time datetime)
insert into test(id,Account,Type,[Time])
select 1,'张三',0,'2020-05-22 10:22:33'
union select 2,'李四',20,'2020-05-22 11:22:33'
union select 3,'王二',1,'2020-05-23 10:22:33'
union select 4,'张三',0,'2020-05-24 10:22:33'
union select 5,'张三',2,'2020-05-25 10:42:33'
union select 6,'张三',0,'2020-05-26 10:25:31'
union select 7,'张三',3,'2020-05-26 10:25:33'
union select 8,'张三',10,'2020-05-27 10:25:33'

select * from 
(
	select CONVERT(varchar(10),time,120) as time,type from test
) a
pivot (count(type) for type in ([0],[1],[2],[3],[4],[11],[20]) ) as t

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧