有一个表,
字段:PID,pcount,pname,pdate
1 2 产品1 2005-04-04
2 5 产品2 2006-05-04
3 1 产品3 2006-07-04
4 1 产品1 2007-07-04根据以上表如何汇总才能得到下面的报表,谢谢!
-------------------------------
|名称\年份 | 2005 | 2006 | 2007|
--------------------------------
| 产品1 | 2 | 0 | 1 |
________________________________| 产品2 | 0 | 5 | 0 |
________________________________| 产品3 | 0 | 1 | 0 |
字段:PID,pcount,pname,pdate
1 2 产品1 2005-04-04
2 5 产品2 2006-05-04
3 1 产品3 2006-07-04
4 1 产品1 2007-07-04根据以上表如何汇总才能得到下面的报表,谢谢!
-------------------------------
|名称\年份 | 2005 | 2006 | 2007|
--------------------------------
| 产品1 | 2 | 0 | 1 |
________________________________| 产品2 | 0 | 5 | 0 |
________________________________| 产品3 | 0 | 1 | 0 |
====================================================================
SQL交叉表实例很简单的一个东西,见网上好多朋友问“怎么实现交叉表?”,以下是我写的一个例子,数据库基于SQL SERVER 2000。-- ======================================================--交叉表实例-- ======================================================建表:在查询分析器里运行:CREATE TABLE [Test] ( [id] [int] IDENTITY (1, 1) NOT NULL , [name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [subject] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [Source] [numeric](18, 0) NULL ) ON [PRIMARY]GOINSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'语文',60)INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'数学',70)INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'英语',80)INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'数学',75)INSERT INTO [test] ([name],[subject],[Source]) values (N'王五',N'语文',57)INSERT INTO [test] ([name],[subject],[Source]) values (N'李四',N'语文',80)INSERT INTO [test] ([name],[subject],[Source]) values (N'张三',N'英语',100)Go
交叉表语句的实现:--用于:交叉表的列数是确定的select name,sum(case subject when '数学' then source else 0 end) as '数学', sum(case subject when '英语' then source else 0 end) as '英语', sum(case subject when '语文' then source else 0 end) as '语文' from test group by name --用于:交叉表的列数是不确定的declare @sql varchar(8000)set @sql = 'select name,' select @sql = @sql + 'sum(case subject when '''+subject+''' then source else 0 end) as '''+subject+''',' from (select distinct subject from test) as a select @sql = left(@sql,len(@sql)-1) + ' from test group by name'exec(@sql)go
drop table tb
go
----创建测试数据
create table tb(pid int,pcount int,pname varchar(50),pdate datetime)
insert tb
select 1,2,'产品1','2005-04-04' union all
select 2,5,'产品2','2006-05-04' union all
select 3,1,'产品3','2006-07-04' union all
select 4,1,'产品1','2007-07-04'
----生成动态汇总SQL字符串
declare @str varchar(8000)
declare @sql varchar(8000)
set @str = ''
select @str = @str + ',' +
'[' + cast(pyear as varchar(4)) + '] = sum(case when year(pdate) = ' + cast(pyear as varchar(4)) + ' then pcount else 0 end )'
from (select distinct year(pdate) as pyear from tb ) a
set @sql = 'select pname as [名称\年份]' + @str + ' from tb group by pname'
print @sql
----执行汇总
exec (@sql)
--drop table tb
if object_id('tb') is not null
drop table tb
go
----创建测试数据
create table tb(pid int,pcount int,pname varchar(50),pdate datetime)
insert tb
select 1,2,'产品1','2005-04-04' union all
select 2,5,'产品2','2006-05-04' union all
select 3,1,'产品3','2006-07-04' union all
select 4,1,'产品1','2007-07-04'
go----生成动态汇总SQL字符串
declare @sql nvarchar(max)
set @sql = ''
select @sql = @sql + ',' + QUOTENAME(pyear)
from (select distinct year(pdate) as pyear from tb ) a
set @sql = stuff(@sql, 1, 1, '')
exec('
select *
from(
select pname, year(pdate) as pyear, pcount from tb
)DATA
PIVOT(
SUM(pcount)
FOR pyear IN(' + @sql + ')
)P')