-2

我想使用案例条件来获取日期之间的值。

我有一个表名用户和一个列日期,我想使用日期条件从数据库中获取值。

select sum(visitor) AS V FROM user  WHERE date  < '21-05-2012' AND dDateTime > '11-05-2012' 
select sum(visitor) AS c FROM user  WHERE date  < '21-06-2012' AND dDateTime > '11-06-2012'
select sum(visitor) AS s FROM user  WHERE date  < '21-07-2012' AND dDateTime > '11-07-2012'
select sum(visitor) AS g FROM user  WHERE date  < '21-08-2012' AND dDateTime > '11-08-2012'

如何在一条语句中完成整个查询。

4

1 回答 1

1

sum这是带有内部的 a 的示例case

select sum(case when date < '21-05-2012' AND dDateTime > '11-05-2012' then visitor end) v 
,      sum(case when date < '21-06-2012' AND dDateTime > '11-06-2012' then visitor end) c
,      sum(case when date < '21-07-2012' AND dDateTime > '11-07-2012' then visitor end) s
,      sum(case when date < '21-08-2012' AND dDateTime > '11-08-2012' then visitor end) g
from   user
于 2012-06-05T06:38:45.660 回答