我想使用案例条件来获取日期之间的值。
我有一个表名用户和一个列日期,我想使用日期条件从数据库中获取值。
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'
如何在一个语句中完成整个查询。
答案 0 :(得分:1)
以下是sum
内有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