表:TA1
keycode,type
1,营业税
2,营业税表:TA2
keycode,tname,money
1,商品房,1000
1,住宅144平方米以下,1200
1,公产,1800
2,住宅144平方米以上,1900
2,商品房,900按照以下类别进行统计,要求一个语句里要列出下面所有类别的查询结果
:住宅144平方米以下,住宅144平方米以上,赠与,继承,商品房,公产输入如下的结果
税种,住宅144平方米以下,住宅144平方米以上,赠与,继承,商品房,公产
营业税,1200,1900,0,0,1900,1800
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-17 11:18:00
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[TA1]
if object_id('[TA1]') is not null drop table [TA1]
go
create table [TA1]([keycode] int,[type] varchar(6))
insert [TA1]
select 1,'营业税' union all
select 2,'营业税'
--> 测试数据:[TA2]
if object_id('[TA2]') is not null drop table [TA2]
go
create table [TA2]([keycode] int,[tname] varchar(17),[money] int)
insert [TA2]
select 1,'商品房',1000 union all
select 1,'住宅144平方米以下',1200 union all
select 1,'公产',1800 union all
select 2,'住宅144平方米以上',1900 union all
select 2,'商品房',900
--------------开始查询--------------------------
select
a.[type],
max(case b.[tname] when '住宅144平方米以下' then b.[money]else 0 end) as '住宅144平方米以下',
max(case b.[tname] when '住宅144平方米以上' then b.[money] else 0 end) as '住宅144平方米以上',
max(case b.[tname] when '商品房' then b.[money] else 0 end) as '商品房',
max(case b.[tname] when '公产' then b.[money] else 0 end) as '公产'
from
[TA1] a
join
[TA2] b
on
a.[keycode]=b.[keycode]
group by
a.[type]
----------------结果----------------------------
/* type 住宅144平方米以下 住宅144平方米以上 商品房 公产
------ ----------- ----------- ----------- -----------
营业税 1200 1900 1000 1800(1 行受影响)
*/
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-11-17 11:18:00
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[TA1]
if object_id('[TA1]') is not null drop table [TA1]
go
create table [TA1]([keycode] int,[type] varchar(6))
insert [TA1]
select 1,'营业税' union all
select 2,'营业税'
--> 测试数据:[TA2]
if object_id('[TA2]') is not null drop table [TA2]
go
create table [TA2]([keycode] int,[tname] varchar(17),[money] int)
insert [TA2]
select 1,'商品房',1000 union all
select 1,'住宅144平方米以下',1200 union all
select 1,'公产',1800 union all
select 2,'住宅144平方米以上',1900 union all
select 2,'商品房',900
--------------开始查询--------------------------
select
a.[type],
max(case b.[tname] when '住宅144平方米以下' then b.[money]else 0 end) as '住宅144平方米以下',
max(case b.[tname] when '住宅144平方米以上' then b.[money] else 0 end) as '住宅144平方米以上',
max(case b.[tname] when '商品房' then b.[money] else 0 end) as '商品房',
max(case b.[tname] when '公产' then b.[money] else 0 end) as '公产',
max(case b.[tname] when '赠与' then b.[money] else 0 end) as '赠与',
max(case b.[tname] when '继承' then b.[money] else 0 end) as '继承'
from
[TA1] a
join
[TA2] b
on
a.[keycode]=b.[keycode]
group by
a.[type]
----------------结果----------------------------
/*type 住宅144平方米以下 住宅144平方米以上 商品房 公产 赠与 继承
------ ----------- ----------- ----------- ----------- ----------- -----------
营业税 1200 1900 1000 1800 0 0(1 行受影响)
*/
if object_id('[TA1]') is not null drop table [TA1]
go
create table [TA1]([keycode] int,[type] varchar(6))
insert [TA1]
select 1,'营业税' union all
select 2,'营业税'
if object_id('[TA2]') is not null drop table [TA2]
go
create table [TA2]([keycode] int,[tname] varchar(17),[money] int)
insert [TA2]
select 1,'商品房',1000 union all
select 1,'住宅144平方米以下',1200 union all
select 1,'公产',1800 union all
select 2,'住宅144平方米以上',1900 union all
select 2,'商品房',900
---查询---
select
a.[type] as 税种,
[住宅144平方米以下]=sum(case when b.tname='住宅144平方米以下' then [money] else 0 end),
[住宅144平方米以上]=sum(case when b.tname='住宅144平方米以上' then [money] else 0 end),
赠与=sum(case when b.tname='赠与' then [money] else 0 end),
继承=sum(case when b.tname='继承' then [money] else 0 end),
商品房=sum(case when b.tname='商品房' then [money] else 0 end),
公产=sum(case when b.tname='公产' then [money] else 0 end)
from
ta1 a
left join
ta2 b
on a.keycode=b.keycode
group by a.[type]---结果---
税种 住宅144平方米以下 住宅144平方米以上 赠与 继承 商品房 公产
------ ----------- ----------- ----------- ----------- ----------- -----------
营业税 1200 1900 0 0 1900 1800(所影响的行数为 1 行)
declare @TB1 table([keycode] int,[type] varchar(6))
insert @TB1
select 1,'营业税' union all
select 2,'营业税'
declare @TB2 table([keycode] int,[tname] varchar(17),[money] int)
insert @TB2
select 1,'商品房',1000 union all
select 1,'住宅144平方米以下',1200 union all
select 1,'公产',1800 union all
select 2,'住宅144平方米以上',1900 union all
select 2,'商品房',900
;WITH CTE AS
(
SELECT T1.[type] as [税种], T2.[tname], T2.[money]
FROM @TB1 T1 INNER JOIN @TB2 T2 ON T1.[keycode] = T2.[keycode]
)
SELECT *
FROM CTE
PIVOT
(
SUM([money])
FOR [tname]
IN([住宅144平方米以下],[住宅144平方米以上],[赠与],[继承],[商品房],[公产])
) AS P-----------------------
税种 住宅144平方米以下 住宅144平方米以上 赠与 继承 商品房 公产
------ ----------- ----------- ----------- ----------- ----------- -----------
营业税 1200 1900 NULL NULL 1900 1800(1 row(s) affected)