- PR -

データ抽出が上手くいきません

投稿者投稿内容
kukumimi
会議室デビュー日: 2008/12/30
投稿数: 5
投稿日時: 2009-01-02 15:52
バージョンは2005です
かつのり
ぬし
会議室デビュー日: 2004/03/18
投稿数: 2015
お住まい・勤務地: 札幌
投稿日時: 2009-01-02 19:04
直接的な回答ではなく、多少苦言になるかもしれませんが、
SQLを質問されるときには、RDBMSの名前、バージョンは必須です。

クロスプラットフォームなRDBMSの場合はOS情報、
RDBMSによっては、利用しているアドオンやストレージエンジンなどの追加情報も必要です。

何故かといいますと、使える関数、構文がRDBMSによって全く違うためです。
質問者がOracleで動くSQLを聞いているのに、
回答者がSQLServerでしか動かないSQLを回答したりすると、
非常に不毛なやりとりが行われるわけです。

kukumimiさんが回答される立場になったときに、
頑張って回答されたSQLが、
 「このSQLでは動きませんでした。」
と返されると、どう思われるでしょう?

今後意識していただければと思います。
kukumimi
会議室デビュー日: 2008/12/30
投稿数: 5
投稿日時: 2009-01-03 22:12
皆様、いろいろなご指摘など本当にありがとうございます。
勉強になります。
大変恐縮ですが、再度質問させて下さい。

maintbl
a_code | b_code | a_suuchi | b_suuchi | c_code | d_code | kbn
10 | xx | 1   | 10   | b | xx01 | 99
10 | xx | 1   | 10   | a | xx01 | 99
10 | yy | 10  | 100  | b | yy01 | 99
10 | yy | 10  | 100  | a | yy01 | 99
10 | xx | 100 | 1000 | b | xx02 | 99
10 | yy | 100 | 1000 | a | yy02 | 99
10 | xx | 50  | 500  | b | xx02 | 10
10 | xx | 50  | 500   | a | xx02 | 10
10 | xx | 80  | 800  | b | xx02 | 20
10 | yy | 80  | 800  | a | yy02 | 20
10 | yy | 60  | 600  | b | yy03 | 30
10 | yy | 40  | 400  | a | yy03 | 30
20 | xx | 1   | 10   | b | xx01 | 99
20 | xx | 1   | 10   | a | xx01 | 99
20 | yy | 10  | 200  | b | yy01 | 99
20 | yy | 10  | 100  | a | yy01 | 99


subtbl
d_code | kbn
xx01  | 99
yy01  | 99
xx02  | 99
yy02  | 99
xx03  | 99
yy03  | 99


上記のような2種類のTBLがあるのですが、以下の結果を抽出したいです。

a_codeごとの、a_suuchiの計(xx)、a_suuchiの計(yy)、b_suuchiの計
です。

条件は、
maintblのd_codeが同じ値で、
maintblのc_codeがaの場合のb_suuchiの合計と、
maintblのc_codeがbの場合のb_suuchiの合計が同じで、
なおかつ、maintblとsubtblのdcodeとkbnが同じデータは
加算対象としない。
です。上記TBLですと、1,2,3,4,13,14行目のデータが
加算対象外です。
例)1,2行目ですが、d_codeがxx01で同じで、
c_codeがaの場合のb_suuchiの合計は10で、
c_codeがbの場合のb_suuchiの合計は10のため、合計は同じ。
なおかつ、maintblとsubtblのdcodeがxx01とkbnが99と同じ
であるため、加算対象としません。


上記TBLの場合ですと、以下の抽出結果となります。
a_code | a_suuchiの計(xx) | a_suuchiの計(yy) | b_suuchiの計
10 | 280 | 280 | 5600
20 | 0   | 20  | 300


私が考えたSQL文は以下になりますが、思うような結果が取得できません。

SELECT a.a_code,
SUM(CASE WHEN a.b_code = 'xx' THEN a_suuchi ELSE 0 END) as 'a_suuchiの計(xx)',
SUM(CASE WHEN a.b_code = 'yy' THEN a_suuchi ELSE 0 END) as 'a_suuchiの計(yy)',
SUM(a.b_suuchi) as 'b_suuchiの計'
FROM maintbl a, subtbl b
WHERE (a.d_code = b.d_code) AND (a.kbn = b.kbn)
GROUP BY a.a_code,a.d_code
HAVING SUM(CASE WHEN (a.c_code = 'a') AND (a.d_code = b.d_code) AND
(a.kbn = b.kbn) THEN b_suuchi ELSE 0 END)
<> SUM(CASE WHEN (a.c_code = 'b') AND (a.d_code = b.d_code) AND
(a.kbn = b.kbn) THEN b_suuchi ELSE 0 END)


[ メッセージ編集済み 編集者: kukumimi 編集日時 2009-01-03 22:20 ]

[ メッセージ編集済み 編集者: kukumimi 編集日時 2009-01-03 22:23 ]

[ メッセージ編集済み 編集者: kukumimi 編集日時 2009-01-03 22:29 ]

[ メッセージ編集済み 編集者: kukumimi 編集日時 2009-01-03 22:31 ]

[ メッセージ編集済み 編集者: kukumimi 編集日時 2009-01-03 22:32 ]

[ メッセージ編集済み 編集者: kukumimi 編集日時 2009-01-03 22:34 ]

[ メッセージ編集済み 編集者: kukumimi 編集日時 2009-01-03 22:35 ]
明智重蔵
大ベテラン
会議室デビュー日: 2005/09/05
投稿数: 127
投稿日時: 2009-01-05 19:35
Oracle10gR2で作ってみました
SQLServer2005でも多分動くでしょう

>maintblのd_codeが同じ値で、
>maintblのc_codeがaの場合のb_suuchiの合計と、
>maintblのc_codeがbの場合のb_suuchiの合計が同じで、
>なおかつ、maintblとsubtblのdcodeとkbnが同じデータは
>加算対象としない。
にドモルガンの法則を適用してみました

コード:
create table maintbl(a_code,b_code,a_suuchi,b_suuchi,c_code,d_code,kbn) as
select 10,'xx',  1,  10,'b','xx01',99 from dual union all
select 10,'xx',  1,  10,'a','xx01',99 from dual union all
select 10,'yy', 10, 100,'b','yy01',99 from dual union all
select 10,'yy', 10, 100,'a','yy01',99 from dual union all
select 10,'xx',100,1000,'b','xx02',99 from dual union all
select 10,'yy',100,1000,'a','yy02',99 from dual union all
select 10,'xx', 50, 500,'b','xx02',10 from dual union all
select 10,'xx', 50, 500,'a','xx02',10 from dual union all
select 10,'xx', 80, 800,'b','xx02',20 from dual union all
select 10,'yy', 80, 800,'a','yy02',20 from dual union all
select 10,'yy', 60, 600,'b','yy03',30 from dual union all
select 10,'yy', 40, 400,'a','yy03',30 from dual union all
select 20,'xx',  1,  10,'b','xx01',99 from dual union all
select 20,'xx',  1,  10,'a','xx01',99 from dual union all
select 20,'yy', 10, 200,'b','yy01',99 from dual union all
select 20,'yy', 10, 100,'a','yy01',99 from dual;

create table subtbl(d_code,kbn) as
select 'xx01',99 from dual union all
select 'yy01',99 from dual union all
select 'xx02',99 from dual union all
select 'yy02',99 from dual union all
select 'xx03',99 from dual union all
select 'yy03',99 from dual;

select a_code,
SUM(CASE b_code when 'xx' THEN a_suuchi ELSE 0 END) as "a_suuchiの計(xx)",
SUM(CASE b_code when 'yy' THEN a_suuchi ELSE 0 END) as "a_suuchiの計(yy)",
SUM(b_suuchi) as "b_suuchiの計"
from (select a_code,b_code,a_suuchi,b_suuchi,d_code,kbn,
      sum(CASE c_code WHEN 'a' THEN b_suuchi ELSE 0 END)
      over(partition by a_code,d_code) as sumXX,
      sum(CASE c_code WHEN 'b' THEN b_suuchi ELSE 0 END)
      over(partition by a_code,d_code) as sumYY
        from maintbl) a
 where sumXX <> sumYY
    or not exists(select 1 from subtbl b
                   where b.d_code = a.d_code
                     and b.kbn    = a.kbn)
group by a_code;

  A_CODE  a_suuchiの計(xx)  a_suuchiの計(yy)  b_suuchiの計
--------  ----------------  ----------------  ------------
      10               280               280          5600
      20                 0                20           300


spy
会議室デビュー日: 2009/01/06
投稿数: 5
投稿日時: 2009-01-06 21:24
http://d.hatena.ne.jp/busaikuro/20081209#c1231120586

スキルアップ/キャリアアップ(JOB@IT)