求高手们指教 重复字段,该怎么处理
求高手们指教 重复字段
cardname itemname quantity yf date
supplier test 10 24 2013-2-22
supplier test 20 24 2013-2-22
supplier test 30 36 2013-2-24
supplier test 40 48 2013-2-25
在上面表中,通过SQL语句“yf”字段可以实现下面的结果吗?谢谢。
cardname itemname quantity yf date
supplier test 10 24 2013-2-22
supplier test 20 0 2013-2-22
supplier test 30 36 2013-2-24
supplier test 40 48 2013-2-25
------解决方案--------------------
同日期 同数量 的,数量只在第一次显示?
------解决方案--------------------
是这样吗:
------解决方案--------------------
cardname, itemname,quantity , yf=case when no=1 then yf else 0 end , date
from (select * no=row_number() over(partition by cardname,yf order by getdate()) from 表) t
cardname itemname quantity yf date
supplier test 10 24 2013-2-22
supplier test 20 24 2013-2-22
supplier test 30 36 2013-2-24
supplier test 40 48 2013-2-25
在上面表中,通过SQL语句“yf”字段可以实现下面的结果吗?谢谢。
cardname itemname quantity yf date
supplier test 10 24 2013-2-22
supplier test 20 0 2013-2-22
supplier test 30 36 2013-2-24
supplier test 40 48 2013-2-25
------解决方案--------------------
同日期 同数量 的,数量只在第一次显示?
------解决方案--------------------
是这样吗:
--drop table a
create table a(cardname varchar(20),itemname varchar(10),quantity int,yf int,date datetime)
insert into a
select 'supplier', 'test', 10 ,24 ,'2013-2-22' union all
select 'supplier', 'test', 20 ,24 ,'2013-2-22' union all
select 'supplier', 'test', 30 ,36 ,'2013-2-24' union all
select 'supplier', 'test', 40 , 48 ,'2013-2-25'
go
select cardname,itemname,quantity,
case when rownum >= 2 then 0 else yf end as yf,
date
from
(
select *,
ROW_NUMBER() over(partition by yf,date order by getdate()) rownum
from a
)a
/*
cardname itemname quantity yf date
supplier test 10 24 2013-02-22 00:00:00.000
supplier test 20 0 2013-02-22 00:00:00.000
supplier test 30 36 2013-02-24 00:00:00.000
supplier test 40 48 2013-02-25 00:00:00.000
*/
------解决方案--------------------
cardname, itemname,quantity , yf=case when no=1 then yf else 0 end , date
from (select * no=row_number() over(partition by cardname,yf order by getdate()) from 表) t