例えば以下のテーブルから
|ordercode(int)|dat(date)|cat(int)|price(int)|tantou(text)|
|:--|:--:|--:|
|240|2017-05-11|1|12000|鈴木|
|240|2017-05-11|1|3000|鈴木|
|240|2017-05-11|2|8000|鈴木|
|242|2017-05-11|1|12000|田中|
|242|2017-05-11|1|3000|田中|
|242|2017-05-11|1|2000|田中|
|242|2017-05-11|2|10000|田中|
|242|2017-05-11|2|8000|田中|
|243|2017-05-11|1|12000|伊藤|
|243|2017-05-11|2|8000|伊藤|
|243|2017-05-11|2|5000|伊藤|
|243|2017-05-11|2|2000|伊藤|
|243|2017-05-11|2|3000|鈴木|
|243|2017-05-11|3|5000|鈴木|
|243|2017-05-11|3|3000|鈴木|
|245|2017-05-11|1|12000|鈴木|
「ordercode」「dat」「tanotu」でグループ化し
cat毎に合計し
横並びにしたい場合、どのようなsqlを書けばよろしいでしょうか?
|ordercode|dat|tantou|cat1|cat2|cat3|
|:--|:--:|--:|
|240|2017-05-11|鈴木|15000|8000|0|
|242|2017-05-11|田中|17000|18000|0|
|243|2017-05-11|伊藤|12000|15000|0|
|243|2017-05-11|鈴木|0|3000|8000|
|245|2017-05-11|鈴木|12000|0|0|
追記:
上記から発展させたく追記します。
|ordercode(int)|dat(date)|cat(int)|price(int)|tantou(text)|
|:--|:--:|--:|
|240|2017-05-11|1|12000|鈴木|
|240|2017-05-11|1|3000|鈴木|
|240|2017-05-11|2|8000|鈴木|
|240|2017-05-11|3|4000|鈴木|
|242|2017-05-11|1|12000|田中|
|242|2017-05-11|1|3000|田中|
|242|2017-05-11|1|2000|田中|
|242|2017-05-11|3|2000|田中|
|242|2017-05-11|2|10000|田中|
|242|2017-05-11|2|8000|田中|
|243|2017-05-11|1|12000|伊藤|
|243|2017-05-11|2|8000|伊藤|
|243|2017-05-11|2|5000|伊藤|
|243|2017-05-11|2|2000|伊藤|
|243|2017-05-11|1|13000|鈴木|
|243|2017-05-11|2|3000|鈴木|
|243|2017-05-11|3|5000|鈴木|
|243|2017-05-11|3|3000|鈴木|
|245|2017-05-11|1|12000|鈴木|
|245|2017-05-11|3|1000|鈴木|
下記のようにしたい。
条件は
cat3>0である場合
cat2>0であればcat2-cat3を返す
cat2<cat3の場合はcat2を0とし
cat1をcat1-(cat3-cat2)とする
|ordercode|dat|tantou|cat1|cat2|cat3|
|:--|:--:|--:|
|240|2017-05-11|鈴木|15000|4000|4000|
|242|2017-05-11|田中|17000|16000|2000|
|243|2017-05-11|伊藤|12000|15000|0|
|243|2017-05-11|鈴木|8000|0|8000|
|245|2017-05-11|鈴木|11000|0|1000|

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/22 08:47
2017/05/22 13:12
2017/05/22 13:53 編集
2017/05/25 04:46