求一个SQL查询 - MS-SQL Server / 应用实例
表A
字段a 字段b
01 a
02 b
03 c
04 d
表B
字段a 字段c 字段X
01 a001 1
01 a002 2
01 a003 3
02 b001 1
02 b002 2
02 b003 3
03 c001 1
03 c002 2
03 c003 3
04 d001 1
04 d002 2
04 d003 3
求查询结果为
01 a a001 a002 a003
02 b b001 b002 b003
03 c c001 c002 c003
04 d d001 d002 d003
sql 查询代码如何写 谢谢~
字段X 固定是3个吗?
select a.*,
max(case b.x when 1 then b.c else '' end) [1],
max(case b.x when 2 then b.c else '' end) [2],
max(case b.x when 3 then b.c else '' end) [3]
from a , b
where a.a = b.a
group by a.a a.b
select a.*,
max(case b.x when 1 then b.c else '' end) [1],
max(case b.x when 2 then b.c else '' end) [2],
max(case b.x when 3 then b.c else '' end) [3]
from a , b
where a.a = b.a
group by a.a ,a.b
SQL code:
----------------------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-05-19 15:50:56
-- Version: Microsoft SQL Server 2008 (RTM) -
相关问答:
今天做了一个存储过程 环境是SQL2000数据库
大致如下
建立临时表
定义员工游标
循环员工(属于1个公司)
......
从数据库中查询一张表的数据
select 部门,姓名 from tb
如何才能生成下面的xml格式
XML code:
<folder state="unchecked" label="全部">
<folder state="unchecked&qu ......
如何在SQL2005中设定定时作业,比如说定时清理某些表的数据,
或者是定时的将某些表的数据导出excel!
在线等待,急急急,最好是详细步骤!
之前我做的作业有点问题!
帮UP
参考:http://hi.baidu.com/toiota ......