アプリ版:「スタンプのみでお礼する」機能のリリースについて

Oracle9iで開発をしております。

月間集計をするにあたり、SQLをどのように書けばよいのか質問させていただきたいと思います。

たとえば、COUNTテーブルというテーブルがあり
年月日 カウント数
2005/09/01 1000
2005/09/01 1000
2005/09/02 2000
2005/09/03 3000
2005/09/05 5000
2005/09/06 6000

というようにデータが入っていたとします。
現在、
SELECT 年月日, SUM(カウント数)
FROM COUNTテーブル
WHERE
COUNT_DATE BETWEEN TO_DATE(to_char(?||'/'||?||'/01')) AND LAST_DAY(TO_DATE(to_char(?||'/'||?||'/01')))
GROUP BY 年月日

としていまして、抽出されるデータは
2005/09/01 2000
2005/09/02 2000
2005/09/03 3000
2005/09/05 5000
2005/09/06 6000
となります。
ここで、2004/09/04や2005/09/07以降2005/09/30までのテーブルには存在
しない日付も抽出したいと思っています。
どのようにすれば抽出できるのでしょうか?

A 回答 (4件)

過ちに気づいた。



select
nvl(年月日,to_date('??/??'||'/'||to_char(R,'dd'),'yy/mm/dd') 年月日,
nvl(カウント数,0) カウント数
(
select 年月日,SUM(カウント数) カウント数
from カウントテーブル
when trunc(年月日,'mm') = to_date('??/??','yy/mm')
group by 年月日
) X,
(select ROWNUM R from
(select * from (select * from dual union all select * from dual)) Y1,
(select * from (select * from dual union all select * from dual)) Y2,
(select * from (select * from dual union all select * from dual)) Y3,
(select * from (select * from dual union all select * from dual)) Y4,
(select * from (select * from dual union all select * from dual)) Y5
) Z
where to_number(to_char(年月日(+),'dd') = R
    • good
    • 0
この回答へのお礼

SQL付でのご回答ありがとうございます。
参考にして試行錯誤しながら抽出することができました。

お礼日時:2005/09/29 11:02

まったくテストしてない妄想したSQLです。



select 年月日,カウント数
(
select 年月日,SUM(カウント数) カウント数
from カウントテーブル
when trunc(年月日,'mm') = to_date('??/??','yy/mm')
group by 年月日
) X,
(select ROWNUM R from
(select * from (select * from dual union all select * from dual)) Y1,
(select * from (select * from dual union all select * from dual)) Y2,
(select * from (select * from dual union all select * from dual)) Y3,
(select * from (select * from dual union all select * from dual)) Y4,
(select * from (select * from dual union all select * from dual)) Y5
) Z
where to_number(to_char(年月日(+),'dd') = R

本当は、加工処理用に表関数(ユーザ関数)を作ると、
必要件数を導き出すための無駄な問い合わせを、
スッキリさせることができるのですが..
    • good
    • 0

カレンダー表の生成だけがネックと勝手に判断してしまいますが、表関数を使うとよいでしょう。



参考URL:http://pukiwiki.postcle.com/zange/index.php?%C9% …
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。
表関数というものがあるのですね。
ユーザ関数は使えない状況なのでまた別件で検討してみたいと思います。

お礼日時:2005/09/29 11:01

SQLの細かい文法は苦手なので、概念だけ。



2005/9/1~2005/9/30までの値を返すダミー表をSELECTするSQLを作り、ご質問にあるSQLの結果表とOUTER JOIN(外部結合)してやれば良いです。
    • good
    • 0
この回答へのお礼

早速のご回答ありがとうございます。
ダミー表参考になりました。

お礼日時:2005/09/29 10:57

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

関連するカテゴリからQ&Aを探す