请数据库高手进,我相相信你们能行.在线等……
下面是SQL语句
select HeaderText,FiledName, (select FiledName from DaQiJianCe where jiance_date =N '2007-4-18 ' and caiyang_id =2) as FiledData, DataType from Management where DataTable =N 'DaQiJianCe ' and ShowFiled = 1
说明
表一 <DaQiJianCe>
caiyang_id 字段1 字段2 字段3 字段4 jiance_date
2 10 20 30 40 2007-4-18
表二 <Management>
HeaderText FiledName DataType ShowFiled
语文 字段2 Int true
数学 字段3 Int true
通过上面的SQL语句要得到的结果
HeaderText FiledName FiledData DataType
语文 字段2 20 Int
数学 字段3 30 Int
但现结果(我也知道问题在哪儿,但解决不了)
HeaderText FiledName FiledData DataType
语文 字段2 字段2 Int
数学 字段3 字段2 Int
请高手解决一下问题呀,好痛苦……
------解决方案--------------------这样试试
select HeaderText,FiledName,
case when FiledName= '字段1 ' then (select 字段1 from DaQiJianCe where jiance_date =N '2007-4-18 ' and caiyang_id =2)
when FiledName= '字段2 ' then (select 字段2 from DaQiJianCe where jiance_date =N '2007-4-18 ' and caiyang_id =2)
when FiledName= '字段3 ' then (select 字段3 from DaQiJianCe where jiance_date =N '2007-4-18 ' and caiyang_id =2)
when FiledName= '字段4 ' then (select 字段4 from DaQiJianCe where jiance_date =N '2007-4-18 ' and caiyang_id =2)
end as FiledData,
DataType
from Management
where DataTable =N 'DaQiJianCe ' and ShowFiled = 1
------解决方案-------------------- create table DaQiJianCe(caiyang_id int,字段1 int, 字段2 int, 字段3 int, 字段4 int, jiance_date smalldatetime)
insert into DaQiJianCe select 2,10,20,30,40, '2007-4-18 '
create table Management(HeaderText varchar(10),FiledName varchar(10),DataType varchar(10),ShowFiled varchar(10))
insert into Management select '语文 ', '字段2 ', 'Int ', 'true '
union all select '数学 ', '字段3 ', 'Int ', 'true '
select ta.* from Management ta
left join
(
select * from DaQiJianCe
unpivot
(字段 for 字段值 in ([字段1],[字段2],[字段3],[字段4])
) as unpi
) tb
on ta.FiledName=tb.字段值
drop table DaQiJianCe,Management
/*
(1 行受影响)
(2 行受影响)
Head