请问如何实现这样的查询?
最近在做一张报表,基础数据如下:
idtnotimekey
1001螺丝20130830Y
1002杯子20130901Y
1003茶叶
1004光盘
1005绿茶20130930Y
1006红茶20131001Y
查询“20130901”的数据,查询结果应为
idtnotimekey
1002杯子20130901Y
1003茶叶
1004光盘
请问如何通过sql来实现
[解决办法]
create table #tb(id int,tno varchar(10),[time] int,[key] varchar(1))
insert into #tb
select 1001,'螺丝',20130830,'Y'
union all select 1002,'杯子',20130901,'Y'
union all select 1003,'茶叶',null,null
union all select 1004,'光盘',null,null
union all select 1005,'绿茶',20130930,'Y'
union all select 1006,'红茶',20131001,'Y'
declare @d int
set @d=0
update #tb
set [time]=case when [time] is not null then [time] else @d end
,@d=case when [time] is null then @d else [time] end
select id,tno,[time]=case when [key] is null then null else [time] end,[key]
from #tb
where [time]=20130901
/*
idtnotimekey
1002杯子20130901Y
1003茶叶NULLNULL
1004光盘NULLNULL
*/