34,590
社区成员
发帖
与我相关
我的任务
分享
-- 转换一下,只留下日期,时间不要。
select convert(varchar(10),S_Time,121) as s_time ,
sum(case when YG_No='G001' then 1 else 0 end ) as 'G001',
sum(case when YG_No='G002' then 1 else 0 end ) as 'G002',
sum(case when YG_No='G003' then 1 else 0 end ) as 'G003'
from mytable
group by convert(varchar(10),S_Time,121)
declare @table table
(
ID INT identity(1,1),
YG_No varchar(10),
KD_No varchar(20),
S_Time datetime
)
insert into @table
select 'G003','3900324532432','20150706' union all
select 'G001','3900324532432','20150703' union all
select 'G002','8802324532432','20150703' union all
select 'G003','3900324532432','20150705' union all
select 'G001','8802324532432','20150705' union all
select 'G001','8802324532432','20150704' union all
select 'G002','8802324532432','20150703' union all
select 'G003','3211032453243','20150706' union all
select 'G002','8802324532432','20150706' union all
select 'G002','6544324532432','20150706' union all
select 'G003','6546324532432','20150706' union all
select 'G003','8802324532432','20150702' union all
select 'G001','3900324532432','20150706' union all
select 'G003','3211324532432','20150706' union all
select 'G003','3900324532432','20150703' union all
select 'G002','8802324532432','20150702' union all
select 'G002','8802324532432','20150703' union all
select 'G003','3900324532432','20150705'
select S_Time,
COUNT(
case when left(KD_No,4)='3900' then 1
end
) as '3900开头数据',
COUNT(
case when left(KD_No,4)='8802' then 1
end
) as '8802开头数据',
COUNT(
case when left(KD_No,4)!='8802' and left(KD_No,4)!='3900' then 1
end
) as '其他开头数据'
from @table
group by S_Time
select S_Time,
COUNT(
case when YG_No='G001' then 1
end
) as 'G001',
COUNT(
case when YG_No='G002' then 1
end
) as 'G002',
COUNT(
case when YG_No='G003' then 1
end
) as 'G003'
from @table
group by S_Time