GROUP BYを2回実行しているときに、列の束が何らかの条件を満たす場合にカウントしないようにするにはどうすればよいですか?

巨大

SQLFiddleデモ

私はsampleこのようなテーブルを持っています

サンプルのテーブル

+------------+------------------------+--------+------+
| id_laporan | id_laporan_rekomendasi | status | id   |
+------------+------------------------+--------+------+
|          3 |                      2 |      2 | 01   |
|          3 |                      2 |      2 | 01   |
|          3 |                      2 |      2 | 01   |
|          3 |                      2 |      3 | 01   |
|          8 |                      3 |      2 | 01   |
|          8 |                      3 |      2 | 01   |
|          8 |                      4 |      2 | 01   |
|          7 |                      1 |      2 | 02   |
|          7 |                      1 |      2 | 02   |
|          7 |                      1 |      2 | 02   |
|          7 |                      1 |      3 | 02   |
|          7 |                      5 |      2 | 02   ||
|          7 |                      5 |      3 | 02   |
+------------+------------------------+--------+------+

GROUP BYidCOUNT/SUM作成したいのですがid、いくつかの列が特定の条件を満たす場合はいくつですか。よりよく知られている質問をするために、最初に私はid2つのセクションになります

Id:01

+------------+------------------------+--------+------+
| id_laporan | id_laporan_rekomendasi | status | id   |
+------------+------------------------+--------+------+
|          3 |                      2 |      2 | 01   |
|          3 |                      2 |      2 | 01   |
|          3 |                      2 |      2 | 01   |
|          3 |                      2 |      3 | 01   |
|          8 |                      3 |      2 | 01   |
|          8 |                      3 |      2 | 01   |
|          8 |                      4 |      2 | 01   |

Id:02

|          7 |                      1 |      2 | 02   |
|          7 |                      1 |      2 | 02   |
|          7 |                      1 |      2 | 02   |
|          7 |                      1 |      3 | 02   |
|          7 |                      5 |      2 | 02   ||
|          7 |                      5 |      3 | 02   |
+------------+------------------------+--------+------+

id : 01最初セクションを見てくださいセクションのid_laporan見るid : 01id38があり、横id_laporanに列がありますid_laporan_rekomendasi以下のためにid_laporan : 3それがあるid_laporan_rekomendasi2とのためにid_laporan : 8それがあるid_laporan_rekomendasi34

現在、列の各行にid_laporan_rekomendasistatus(の横の列id_laporan_rekomendasi)があります。要するに、私が前に説明したことはこれらのようなものです:

Report_ID:3

+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
|          3 |                      2 |      2 |
|          3 |                      2 |      2 |
|          3 |                      2 |      2 |
|          3 |                      2 |      3 | 

id_report:8

+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
|          8 |                      3 |      2 |
|          8 |                      3 |      2 | 
|          8 |                      4 |      2 | 

それぞれid_laporan_rekomendasistatusid_laporan_rekomendasi右側の列、列およびその上の異なるIDごとにstatus、少なくとも1つのデータ3があり2、他の番号ではなく、他の番号としてカウントされ1ます。0

そのためId_laporan:3それはとしてカウント1のために、Id_laporan:8としてはカウントさ0さえので**Id_laporan:8**、列には2つの異なるIDましたid_laporan_rekomendasiが、列にstatusデータがない3、それがとしてカウント0たとえば、テーブルは次のようになります

サンプル

id_report:8

+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
|          8 |                      3 |      2 |
|          8 |                      3 |      3 | 
|          8 |                      4 |      3 | 

次に、IDが異なる2それぞれid_laporan_rekomendasiについて、としてカウントされ1ます。私たちは手段.that(私はこのサンプルの上に、前に説明したように)テーブルには、条件を満たしていると仮定ことができ**Id_laporan:3**そして**Id_laporan:8**私の説明によると、彼らはデータを持っている10私はこれらをカウントした後、私はしていSUMたもの(その両方10列の)データid:01これらの方法はと同じid:02でした。したがって、期待される出力は次のようになります。

その他のシナリオ

では、ステータス3がすでに途中にある場合はどうなりますid_laporan_rekomendasiか?、idは間違いなくstatus = 3の途中でスキップさid_laporan_rekomendasiます。実際には3、同じ中に2つのデータが見つかるシナリオはありませんid_laporan_rekomendasiこのようになります

+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
|          3 |                      2 |      2 | // 01 : 0, because status = 2
|          3 |                      2 |      3 | // 01 : 1, calculation function works because status = 3
|          3 |                      2 |      2 | // 01 : 1, because status = 2
|          3 |                      2 |      2 | // 01 : 1, because status = 2

重要な注意それぞれのid_laporan_rekomendasiデータステータス=31つしかないため、以下のシナリオのよう3に、ステータス=が2回見つかったシナリオはありません。id_laporan_rekomendasi

誤ったシナリオ

+------------+------------------------+--------+
| id_laporan | id_laporan_rekomendasi | status |
+------------+------------------------+--------+
|          3 |                      2 |      2 | 
|          3 |                      2 |      3 | // Data status = 3, okay, for id_laporan_rekomendasi = 2, i dont need to check for the rest of id_laporan_rekomendasi's status, good grief, i will skip to the next id_laporan_rekomendasi` maybe, i will check id_laporan_rekomendasi = 3 
|          3 |                      2 |      3 | // what is this... ther is no way...
|          3 |                      2 |      2 | 

これは私の場合に期待する出力です

+------+--------------+
| id   | count        |
+------+--------------+
| 01   |      1       |     
| 02   |      2.      |
+------+--------------+

私が期待する結果が最初の表に基づいて上記の表のようになる理由は、計算は次のようになります。

+------------+------------------------+--------+------+
| id_laporan | id_laporan_rekomendasi | status | id   |
+------------+------------------------+--------+------+
|          3 |                      2 |      2 | 01   | // 01 : 0, because status 2
|          3 |                      2 |      2 | 01   | // 01 : 0, because status 2
|          3 |                      2 |      2 | 01   | // 01 : 0, because status 2
|          3 |                      2 |      2 | 01   | // 01 : 0, because status 2
|          3 |                      2 |      2 | 01   | // 01 : 0, because status 2
|          3 |                      2 |      3 | 01   | // 01 : 1, at this point the calculation works because status 3
|          8 |                      3 |      2 | 01   | // 01 : 1, because status 2
|          8 |                      3 |      2 | 01   | // 01 : 1, because status 2 
|          8 |                      4 |      2 | 01   | // 01 : 1, because status 2 
|          7 |                      1 |      2 | 02   | // 02 : 0, because status 2 
|          7 |                      1 |      2 | 02   | // 02 : 0, because status 2 
|          7 |                      1 |      2 | 02   | // 02 : 0, because status 2 
|          7 |                      1 |      3 | 02   | // 02 : 1, at this point the calculation works because status 3
|          7 |                      5 |      2 | 02   | // 02 : 1, because status 2  
|          7 |                      5 |      3 | 02   | // 02 : 2, at this point the calculation works because status 3
+------------+------------------------+--------+------+

私はこのクエリのように試しました

SELECT id, count(id) from sample group by `id`

しかし、結果は以下のようになります

+------+--------------+
| id   | count        |
+------+--------------+
| 01   |      7       |     
| 02   |      6       |
+------+--------------+

CASE内部で関数を使用countする必要があることは知っていますが、複雑なテーブルでそれを行う方法がわかりません。

巨大

私は、おかげ答えを得たゴードン・リノフポール・シュピーゲルのためのDISTINCTCASEではCOUNT()機能、私は本当に(y)を認めます

このコードにはまだバグがあることは知っていますが、少なくともこの答えを自分で即興で作るのに役立ちます

SELECT t1.id , sum(tot) from ( 
  SELECT id, id_laporan, id_laporan_rekomendasi, 
  COUNT(distinct case when status = 3 then 1 end) as tot
  FROM sample t1
GROUP by id_laporan_rekomendasi ) t1
GROUP BY t1.id

結果

| id | sum(tot) |
|----|----------|
| 01 |        1 |
| 02 |        2 |

これがSQLFiddleデモです

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ