有点难度的sql语句,紧急求助

来源:百度知道 编辑:UC知道 时间:2024/05/18 04:53:50
表结构如下:

日期 姓名

2009-03-16 张三
2009-03-17 李四
2009-03-17 张三
2009-03-17 张三
2009-03-18 王五
2009-03-18 李四
2009-03-18 李四

想要的查询后的显示结构如下:

日期 张三 李四 王五

2009-03-16 1 0 0
2009-03-17 2 1 0
2009-03-18 0 2 1
有点棘手,请大家帮忙!
...

select 日期,
sum(case 姓名 when 姓名='张三' then 1 else 0 end) as 张三,
sum(case 姓名 when 姓名='李四' then 1 else 0 end) as 李四,
sum(case 姓名 when 姓名='王五' then 1 else 0 end) as 王五
from table group by 日期

脑袋打结 又得写代码。。。
declare @name_a varchar //张三(以下依次类推)
declare @name_b varchar
declare @name_c varchar
declare @datatime_16 //存储在日期表里的16号的日期对象名

if not exists(select * from 日期表 where @name_a = "张三")
begin
print
select @datatime_16 from 日期表
count(@name_a)
end
以上查出张三在16号的出现的次数 以下类推 在17号如还出现count(@name_a)+1然后输出

select d.日期,isnull(a.count) 张三,isnull(b.count) 李四,isnull(c.count) 王五 from
(select 姓名,日期,count(*) count from table_name where 姓名='张三' group by 姓名,日期) a,(select 姓名,日期,count(*) count from table_name where 姓名='李四' group by 姓名,日期) b,(select 姓名,日期,count(*) count from table_name where 姓名='王五' group by 姓名,日期) c,
(select distinct 日期 from t