日期:2014-05-17 浏览次数:20458 次
---TableA
if OBJECT_ID('TableA','u') is not null
drop table TableA
create table TableA
(
QNUM int,
QSDES nvarchar(10)
)
insert into TableA
select 100101,'大小' union all
select 100102,'重量'
---TableB
if OBJECT_ID('TableB','u') is not null
drop table TableB
create table TableB
(
QYM nvarchar(10),
REF int,
QNUM int,
QAW int
)
insert into TableB
select 'QY01',331,100101,1 union all
select 'QY02',332,100102,2
---TableC
if OBJECT_ID('TableC','u') is not null
drop table TableC
create table TableC
(
QYM nvarchar(10),
PNUM nvarchar(10),
REF int
)
insert into TableC
select 'QY01','P001',331
---TableD
if OBJECT_ID('TableD','u') is not null
drop table TableD
create table TableD
(
PNUM nvarchar(10),
REF int,
RDES nvarchar(10)
)
insert into TableD
select 'P001',331,'AAA'
---修改了楼主的数据
---不然没法得到重量
update TableB set QYM='QY01',REF=331 where REF=332
---SQL
select F.REFE,F.RDESE,
sum(case when QSDESE='大小' then QAWE else null end) as 大小,
sum(case when QSDESE='重量' then QAWE else null end) as 重量
from
(select D.REF REFE,D.RDES RDESE,B.QAW QAWE,A.QSDES QSDESE
FROM TableD D
inner join TableC C on D.PNUM = c.PNUM
inner join TableB B on C.QYM = B.QYM
inner join TableA A on B.QNUM = A.QNUM) as F
group by REFE,RDESE
---结果集
--331 AAA 1 2