User表
UserId UserName
1 test
2 myName
3 hello
Type表
TypeId TypeName
1 三年制大专
2 五年制大专
3 中专
User_Type表
ID UserId TypeId Nums
1 1 1 5
2 1 2 3
3 2 1 2
4 2 3 6
5 3 1 10
6 3 2 1
7 3 3 8我想得出的查询结果是UserId 三年制大专 五年制大专 中专
1 5 3 0
2 2 0 6
3 10 1 8是个交叉表的查询.用的数据库是Access其实就是把行换成列...没思路!
UserId UserName
1 test
2 myName
3 hello
Type表
TypeId TypeName
1 三年制大专
2 五年制大专
3 中专
User_Type表
ID UserId TypeId Nums
1 1 1 5
2 1 2 3
3 2 1 2
4 2 3 6
5 3 1 10
6 3 2 1
7 3 3 8我想得出的查询结果是UserId 三年制大专 五年制大专 中专
1 5 3 0
2 2 0 6
3 10 1 8是个交叉表的查询.用的数据库是Access其实就是把行换成列...没思路!
一个例子:参考SQL Server 交叉查询
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
不是sql 里面的好多东西不支持.
但是Access有iif