日期:2014-05-17 浏览次数:20523 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (TYPE nvarchar(6),MONEY int,CLASS nvarchar(4))
insert into [TB]
select '电费',100,'支出' union all
select '押金',100,'收入' union all
select '电费',50,'支出' union all
select '电话费',200,'支出' union all
select '押金',500,'收入' union all
select '押金',400,'支出' union all
select '烟',1000,'收入' union all
select '酒',200,'收入' union all
select '酒',100,'收入'
select * from [TB]
select CLASS,TYPE,sum(MONEY) as money from TB group by CLASS,TYPE
ORDER BY CLASS,TYPE
/*
收入 酒 300
收入 押金 600
收入 烟 1000
支出 电费 150
支出 电话费 200
支出 押金 400*/
SELECT CLASS ,
CASE WHEN TYPE LIKE '%烟%' THEN '烟'
WHEN TYPE LIKE '%酒%' THEN '酒'
ELSE type
END TYPE ,
SUM(MONEY) MONEY
FROM TB
GROUP BY CLASS ,
CASE WHEN TYPE LIKE '%烟%' THEN '烟'
WHEN TYPE LIKE '%酒%' THEN '酒'
ELSE type
END
ORDER BY CLASS ,
type