请教各位前辈,下面的查询语句中,我想要查询的时间段是在查询@TB时指定,现在的问题是我给变量插入数据时如果不指定时间段就会查出表中所有数据.这个查询我该怎么改,请指点一下,谢谢.
declare @tb table(fsm_sppp varchar(50),flb_lbbm varchar(50), spguid uniqueidentifier,gzh varchar(50),xsrq datetime,sl decimal,je decimal)
insert @tb
select fsm_sppp,flb_lbbm, a.*
from (
Select spguid , gzh,cast (floor(cast(xsrq as float)) as datetime) AS xsrq,
sum(sl) as sl, je as je From DJ_QTXSMX--where (xsh like'01%' or xsh like'04%') and cast(convert(char(8),XSRQ,112) as datetime)>='2008-3-1' AND cast(convert(char(8),XSRQ,112) as datetime)<='2008-3-31'group by spguid ,gzh,je,xsrq,sl) as a
inner join jc_spbm on spguid = guid_spbm
inner join jc_splb on guid_splb=fsm_splbselect xsrq,fsm_sppp,flb_lbbm,
'500以下'=sum(case when je<500 then sl else 0 end),
'501 -1000'=sum(case when je>500 and je<=1000 then sl else 0 end),
'1001-1500'=sum(case when je>1000 and je<=1500 then sl else 0 end),
'1501-2000'=sum(case when je>1500 and je<=2000 then sl else 0 end),
'2001-2500'=sum(case when je>2001 and je<=2500 then sl else 0 end),
'2501-3000'=sum(case when je>2501 and je<=3000 then sl else 0 end),
'3001-3500'=sum(case when je>3001 and je<=3500 then sl else 0 end),
'3501-4000'=sum(case when je>3501 and je<=4000 then sl else 0 end),
'4001-4500'=sum(case when je>4001 and je<=4500 then sl else 0 end),
'4501-5000'=sum(case when je>4501 and je<=5000 then sl else 0 end),
'5000以上'=sum(case when je>5001 then sl else 0 end),
'合计'=sum(sl)
from @tb
where cast(convert(char(8),XSRQ,112) as datetime)>='2008-3-1' AND cast(convert(char(8),XSRQ,112) as datetime)<='2008-3-31'
group by xsrq, fsm_sppp,flb_lbbm
declare @tb table(fsm_sppp varchar(50),flb_lbbm varchar(50), spguid uniqueidentifier,gzh varchar(50),xsrq datetime,sl decimal,je decimal)
insert @tb
select fsm_sppp,flb_lbbm, a.*
from (
Select spguid , gzh,cast (floor(cast(xsrq as float)) as datetime) AS xsrq,
sum(sl) as sl, je as je From DJ_QTXSMX--where (xsh like'01%' or xsh like'04%') and cast(convert(char(8),XSRQ,112) as datetime)>='2008-3-1' AND cast(convert(char(8),XSRQ,112) as datetime)<='2008-3-31'group by spguid ,gzh,je,xsrq,sl) as a
inner join jc_spbm on spguid = guid_spbm
inner join jc_splb on guid_splb=fsm_splbselect xsrq,fsm_sppp,flb_lbbm,
'500以下'=sum(case when je<500 then sl else 0 end),
'501 -1000'=sum(case when je>500 and je<=1000 then sl else 0 end),
'1001-1500'=sum(case when je>1000 and je<=1500 then sl else 0 end),
'1501-2000'=sum(case when je>1500 and je<=2000 then sl else 0 end),
'2001-2500'=sum(case when je>2001 and je<=2500 then sl else 0 end),
'2501-3000'=sum(case when je>2501 and je<=3000 then sl else 0 end),
'3001-3500'=sum(case when je>3001 and je<=3500 then sl else 0 end),
'3501-4000'=sum(case when je>3501 and je<=4000 then sl else 0 end),
'4001-4500'=sum(case when je>4001 and je<=4500 then sl else 0 end),
'4501-5000'=sum(case when je>4501 and je<=5000 then sl else 0 end),
'5000以上'=sum(case when je>5001 then sl else 0 end),
'合计'=sum(sl)
from @tb
where cast(convert(char(8),XSRQ,112) as datetime)>='2008-3-1' AND cast(convert(char(8),XSRQ,112) as datetime)<='2008-3-31'
group by xsrq, fsm_sppp,flb_lbbm
-->
where XSRQ>='2008-3-1' AND XSRQ <='2008-3-31'
--> @tb.XSRQ本来就是定义为datetime,直接比较就可以了,转来转去浪费资源
XSRQ是字段为和要转为float再转为datetime
因为日期后面有时分秒
cast(convert(char(8),XSRQ,112) as datetime)=cast (floor(cast(xsrq as float)) as datetime)
2、@TB不加条件插入,在select @TB时加条件也一样
3、其实不要用@TB中间表变量也可实现
那应该怎么改,我是菜鸟,接触SQL不久.请说明白一点.THANKS
这么说是datetime类型啦,转为float再转datetime,浪费资源。...
from (
Select spguid , gzh, xsrq,
sum(sl) as sl, je as je From DJ_QTXSMX
where xsrq >=XSRQ>='2008-3-1' AND XSRQ <='2008-3-31'
group by spguid ,gzh,je,xsrq,sl) as a
...