SQL2000视图转换
SQL2000视图转换
原视图数据view_vip123
billid,billcode,quantity,referqty,unreferqty,materialid
1001,pc123,300,180,120,A
1001,pc123,300,180,120,B
1001,pc123,300,180,120,C
1001,pc123,300,180,120,D
1001,pc123,300,180,120,E
当:billid=billid,billcode=billcode,quantity=quantity,referqty=referqty,unreferqty=unreferqty 时的要求结果:
求转换为视图view_vip12345678 ,原视图view_vip123不变
把quantity,referqty,unreferqty只取一次数据,其它的数据为0
billid,billcode,quantity,referqty,unreferqty,materialid
1001,pc123,300,180,120,A
1001,pc123,0,0,120,B
1001,pc123,0,0,120,C
1001,pc123,0,0,120,D
1001,pc123,0,0,120,E
------解决方案--------------------如果materialid在同一個billid,billcode,quantity,referqty,unreferqty分組內不重復的話
試試下面的:
select * from (
select * from view_vip123 as a
where materialid=(select min(materialid) from view_vip123 as b
where a.billid=b.billid and a.billcode=b.billcode and a.quantity=b.quantity and a.referqty=b.referqty and a.unreferqty=b.unreferqty )
union all
select billid,billcode,0,0,0,materialid from view_vip123 as a
where materialid>(select min(materialid) from view_vip123 as b
where a.billid=b.billid and a.billcode=b.billcode and a.quantity=b.quantity and a.referqty=b.referqty and a.unreferqty=b.unreferqty ) ) as a
order by billid,billcode,materialid
------解决方案--------------------select identity(1,1) num,* into view_vip1234 from view_vip123
select * from (
select * from view_vip1234 as a
where num=(select min(num) from view_vip1234 as b
where a.billid=b.billid and a.billcode=b.billcode and a.quantity=b.quantity and a.referqty=b.referqty and a.unreferqty=b.unreferqty )
union all
select billid,billcode,0,0,0,materialid from view_vip1234 as a
where num>(select min(num) from view_vip1234 as b
where a.billid=b.billid and a.billcode=b.billcode and a.quantity=b.quantity and a.referqty=b.referqty and a.unreferqty=b.unreferqty ) ) as a
order by billid,billcode,materialid