学习

chouer

  博客中心 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 登录 ::
  2 随笔 :: 948 文章 :: 8 评论 :: 0 Trackbacks
Cached @ 2025/4/28 15:30:53Control ASP.skins_cogitation_controls_blogstats_ascx
<2025年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

News

留言簿(0)

随笔档案

文章分类

文章档案

搜索

最新评论

  • 1. SAP资料下载
  • SAP下载网,
    SAP资料共享网站,完全免费
    资料全由网友共享,大家共享,大家下载
  • --SAP99

阅读排行榜

评论排行榜

Cached @ 2025/4/28 15:30:53Control ASP.skins_cogitation_controls_singlecolumn_ascx

--=====================================================

  --交叉表实例

--=====================================================

在查询分析器里运行:

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]

GO

INSERT 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

运行结果:

分享按钮发布于: 2006-09-22 12:41 chouer 阅读(310) 评论(0)  编辑 收藏

评论

标题
姓名
主页
内容 
  登录  使用高级评论  Top 订阅回复  取消订阅
[使用Ctrl+Enter键可以直接提交]