34,872
社区成员
发帖
与我相关
我的任务
分享----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-07-24 18:27:27
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([voucher_no] varchar(3),[date] datetime,[bank_type] varchar(4),[voucher_stat] int)
insert [tb]
select '001','2009-04-03','icbc',0 union all
select '002','2009-04-03','abc',1 union all
select '003','2009-04-03','boc',1 union all
select '004','2009-04-04','icbc',1 union all
select '005','2009-04-04','icbc',1 union all
select '006','2009-04-05','icbc',1 union all
select '007','2009-04-05','abc',0 union all
select '008','2009-04-05','ccb',0 union all
select '009','2009-04-05','boc',0 union all
select '010','2009-04-05','icbc',1 union all
select '011','2009-04-05','abc',1 union all
select '012','2009-04-06','icbc',1 union all
select '013','2009-04-06','boc',1 union all
select '014','2009-04-07','icbc',1 union all
select '015','2009-04-08','icbc',1 union all
select '016','2009-04-08','icbc',1
--------------开始查询--------------------------
select
bank_type as 银行类型,convert(varchar(10),[date],120) as 交易时间,count(1) as 总支付数,
成功数=sum(case when voucher_stat=1 then 1 else 0 end),
失败数=sum(case when voucher_stat=0 then 1 else 0 end)
from [tb]
group by bank_type,[date]
----------------结果----------------------------
/*
(所影响的行数为 16 行)
银行类型 交易时间 总支付数 成功数 失败数
---- ---------- ----------- ----------- -----------
abc 2009-04-03 1 1 0
boc 2009-04-03 1 1 0
icbc 2009-04-03 1 0 1
icbc 2009-04-04 2 2 0
abc 2009-04-05 2 1 1
boc 2009-04-05 1 0 1
ccb 2009-04-05 1 0 1
icbc 2009-04-05 2 2 0
boc 2009-04-06 1 1 0
icbc 2009-04-06 1 1 0
icbc 2009-04-07 1 1 0
icbc 2009-04-08 2 2 0
(所影响的行数为 12 行)
*/
select
bank_type 银行类型,
date=convert(varchar(10),date,120) 交易时间,
总支付数=count(*),
成功数=sum(voucher_stat),
失败数=sum(1-voucher_stat)
from table1
group by convert(varchar(10),date,120),bank_type----------------------------------------------------------------
-- Author :fredrickhu(小F 向高手学习)
-- Date :2009-07-24 18:27:27
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([voucher_no] varchar(3),[date] datetime,[bank_type] varchar(4),[voucher_stat] int)
insert [tb]
select '001','2009-04-03','icbc',0 union all
select '002','2009-04-03','abc',1 union all
select '003','2009-04-03','boc',1 union all
select '004','2009-04-04','icbc',1 union all
select '005','2009-04-04','icbc',1 union all
select '006','2009-04-05','icbc',1 union all
select '007','2009-04-05','abc',0 union all
select '008','2009-04-05','ccb',0 union all
select '009','2009-04-05','boc',0 union all
select '010','2009-04-05','icbc',1 union all
select '011','2009-04-05','abc',1 union all
select '012','2009-04-06','icbc',1 union all
select '013','2009-04-06','boc',1 union all
select '014','2009-04-07','icbc',1 union all
select '015','2009-04-08','icbc',1 union all
select '016','2009-04-08','icbc',1
--------------开始查询--------------------------
select
bank_type as 银行类型,[date] as 交易时间,count(1) as 总支付数,
成功数=sum(case when voucher_stat=1 then 1 else 0 end),
失败数=sum(case when voucher_stat=0 then 1 else 0 end)
from [tb]
group by bank_type,[date]
----------------结果----------------------------
/*银行类型 交易时间 总支付数 成功数 失败数
---- ------------------------------------------------------ ----------- ----------- -----------
abc 2009-04-03 00:00:00.000 1 1 0
boc 2009-04-03 00:00:00.000 1 1 0
icbc 2009-04-03 00:00:00.000 1 0 1
icbc 2009-04-04 00:00:00.000 2 2 0
abc 2009-04-05 00:00:00.000 2 1 1
boc 2009-04-05 00:00:00.000 1 0 1
ccb 2009-04-05 00:00:00.000 1 0 1
icbc 2009-04-05 00:00:00.000 2 2 0
boc 2009-04-06 00:00:00.000 1 1 0
icbc 2009-04-06 00:00:00.000 1 1 0
icbc 2009-04-07 00:00:00.000 1 1 0
icbc 2009-04-08 00:00:00.000 2 2 0
(所影响的行数为 12 行)
*/
select
date=convert(varchar(10),date,120),
bank_type,
總數=count(*),
[成功数]=sum(case when voucher_stat=1 then 1 else 0 end),
失敗數=sum(case when voucher_stat=0 then 1 else 0 end)
from table1
group by convert(varchar(10),date,120),bank_type
select
date=convert(varchar(10),date,120),
bank_type,
總數=count(*),
[成功数]=sum(cast(voucher_stat as int)),
失敗數=sum(1-cast(voucher_stat as int))
from table1
group by convert(varchar(10),date,120),bank_type