请问可以在一个SQL语句中查询出两个时间段的数据来吗?(100分)

  • 主题发起人 主题发起人 sjm
  • 开始时间 开始时间
S

sjm

Unregistered / Unconfirmed
GUEST, unregistred user!
假设表1中有Date1和Amount两个字段,现在我想将2008-10-01 ~ 2008-10-31这整个时间段的Amount之和统计一下,并还想统计一下2008-10-01之前的Amount之和,可以用一条SQL语句实现吗? 谢谢!
 
select sum(amount) from 表1
where date1>='2008-10-01' and oper_date<='2008-10-31'
union
select sum(amount) from 表1
where date1<='2008-10-01'
 
to 贝尔:
你那样不成了两条记录了吗,我的本意是一条查询结果记录。
 
楼下的比较好
 
select sum(amount),(select sum(a.amount)
from 表1 a
where a.date1<='2008-10-01'
) as amount1 from 表1
where date1>='2008-10-01' and oper_date<='2008-10-31'
 
----
declare @s varchar(8000)
set @s=''
select @s=@s+','+''''+rtrim(amount)+''''
from
(
select sum(amount) from 表1
where date1>='2008-10-01' and oper_date<='2008-10-31'
union
select sum(amount) from 表1
where date1<='2008-10-01'
)T
print @s
select right(@s,len(@s)-1) as amount
 
SELECT SUM(CASE WHEN Date1>='2008-10-1' THEN Amount END) AS AmountInOct,SUM(CASE WHEN Date1<'2008-10-1' THEN Amount END) AS AmountBeforeOct
FROM 表1
WHERE Date1<='2008-10-31'
 
select a.amount1,b.amount2
from (select sum(amount) amount1 from 表1 where Date1 between '2008-10-01' and '2008-10-31') as a,
(select sum(amount) amount2 from 表1 where Date1<'2008-10-01') as b
 
很简单,如下:
select sum(case when date1 <'2008/10/01' then amount else 0 end ) amount1,sum(case when date1 between '2008/10/01' and '2008/10/31' then amount else 0 end) amount2 from 表1
 
select t1.a1 as a1 ,t2.a2 as a2 from
(select sum(amount) as a1 , 'K' as k from 表1 where date1 <='2008-10-01' ) t1,
(select sum(amount) as a2 , 'K' as k from 表1 where date1 >='2008-10-01' and date1 <='2008-10-31') t2
where
t1.k = t2.k
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
890
SUNSTONE的Delphi笔记
S
I
回复
0
查看
439
import
I
后退
顶部