0

日付間の値を取得するための単一の条件が必要です。

テーブル名ユーザーと列日付があり、日付条件を使用してデータベースから値をフェッチしたい。

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つのSQLステートメントに入れたい

4

1 に答える 1

1

多分このようなもの:

select 
    sum(CASE WHEN date  < '21-05-2012' AND dDateTime > '11-05-2012' THEN visitor ELSE 0 END) AS V, 
    sum(CASE WHEN date  < '21-06-2012' AND dDateTime > '11-06-2012' THEN visitor ELSE 0 END) AS c,
    sum(CASE WHEN date  < '21-07-2012' AND dDateTime > '11-07-2012' THEN visitor ELSE 0 END) AS s,
    sum(CASE WHEN date  < '21-08-2012' AND dDateTime > '11-08-2012' THEN visitor ELSE 0 END) AS g
FROM 
    user  
于 2012-06-05T07:23:41.813 に答える