sql 查询
T1表内有如下三下字段
字段 类型 备注
date datetime 时间
field Varchar 地区
number int 数量
-----------生成表
USE [Test]
GO
/****** 对象: Table [dbo].[T1] 脚本日期: 04/13/2010 17:54:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[T1](
[date] [datetime] NOT NULL,
[field] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[number] [int] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
---------测试数据
insert into t1 values('2008-9-10','江苏',12)
insert into t1 values('2008-9-10','山东',22)
insert into t1 values('2008-9-10','湖南',10)
insert into t1 values('2008-9-10','云南',12)
insert into t1 values('2008-10-1','江苏',32)
insert into t1 values('2008-10-1','山东',12)
insert into t1 values('2008-10-1','湖南',12)
insert into t1 values('2008-10-1','云南',24)
insert into t1 values('2008-12-1','江苏',8)
insert into t1 values('2008-12-1','山东',15)
insert into t1 values('2008-12-1','湖南',20)
insert into t1 values('2008-12-1','云南',24)
--------------------------------------
怎样写sql将如下显示出来
date field1 field2 field3 field4
------解决方案--------------------
IF OBJECT_ID('[T1]') IS NOT NULL
DROP TABLE [T1]
GO
CREATE TABLE [dbo].[T1](
[date] [datetime] NOT NULL,
[field] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
[number] [int] NOT NULL
) ON [PRIMARY]
insert into t1 values('2008-9-10','江苏',12)
insert into t1 values('2008-9-10','山东',22)
insert into t1 values('2008-9-10','湖南',10)
insert into t1 values('2008-9-10','云南',12)
insert into t1 values('2008-10-1','江苏',32)
insert into t1 values('2008-10-1','山东',12)
insert into t1 values('2008-10-1','湖南',12)
insert into t1 values('2008-10-1','云南',24)
insert into t1 values('2008-12-1','江苏',8)
insert into t1 values('2008-12-1','山东',15)
insert into t1 values('2008-12-1','湖南',20)
insert into t1 values('2008-12-1','云南',24)
--2005
declare @s varchar(8000)
select @s=ISNULL(@s+',','')+field from T1 group by field
exec('select * from (select convert(varchar,date,23) date,field,numbe