日期:2014-05-17 浏览次数:20768 次
create table mh
(Id int,V int,商品名称 varchar(10),字段 varchar(10),改动前的值 int,改动后的值 int)
insert into mh
select 10998,1,'电脑','数量',1,2 union all
select 10998,1,'桌子','数量',1,2 union all
select 10998,1,'电脑','单价',1000,1200 union all
select 10998,1,'桌子','单价',200,240 union all
select 10998,2,'电脑','数量',2,3 union all
select 10998,2,'桌子','数量',2,3 union all
select 10998,2,'电脑','单价',1200,1100 union all
select 10998,2,'桌子','单价',240,220
declare @tsql varchar(6000)
select @tsql=isnull(@tsql+',','')
+'(select top 1 b.改动前的值 from mh b
where b.Id=a.Id and b.V=a.V and b.商品名称=a.商品名称 and b.字段='''+字段+''') '''+字段+'改前'', '
+'(select top 1 b.改动后的值 from mh b
where b.Id=a.Id and b.V=a.V and b.商品名称=a.商品名称 and b.字段='''+字段+''') '''+字段+'改后'' '
from (select distinct 字段 from mh) t order by 字段 desc
select @tsql='select a.Id,a.V,a.商品名称,'+@tsql
+' from mh a group by a.Id,a.V,a.商品名称 '
exec(@tsql)
/*
Id V 商品名称 数量改前 数量改后 单价改前 单价改后
----------- ----------- ---------- ----------- ----------- ----------- -----------
10998 1 电脑 1 2 1000 1200
10998 1 桌子 &nb