私は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 BY
列id
をCOUNT/SUM
作成したいのですがid
、いくつかの列が特定の条件を満たす場合はいくつですか。よりよく知られている質問をするために、最初に私はid
2つのセクションになります
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 : 01
id3
と8
があり、横id_laporan
に列がありますid_laporan_rekomendasi
。以下のためにid_laporan : 3
それがあるid_laporan_rekomendasi
:2
とのためにid_laporan : 8
それがあるid_laporan_rekomendasi
:3
と4
。
現在、列の各行にid_laporan_rekomendasi
はstatus
(の横の列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_rekomendasi
にstatus
。id_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**
私の説明によると、彼らはデータを持っている1
と0
。私はこれらをカウントした後、私はしていSUM
たもの(その両方1
と0
列の)データ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
データステータス=3
は1つしかないため、以下のシナリオのよう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
する必要があることは知っていますが、複雑なテーブルでそれを行う方法がわかりません。
私は、おかげ答えを得たゴードン・リノフとポール・シュピーゲルのためのDISTINCT
とCASE
では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 |
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加