1

自己結合を使用して列挙型ステータスに基づいてカウントを取得する方法

「student」というテーブルがあり、ステータス0、1、2に列挙型を使用しています。0は非アクティブ、1はアクティブ、2は削除されています。

あれは

studentid studentenrollid studentname studentstatus

1          3                 xyz          1

2          3                 xyz          2

3          8                 asda         1

4          7                 sdd          1

5          9                 asds         0

6          3                 xyz          1

Studentrollidが一意であるステータスに基づいてカウントを取得する必要があります。

studentrollid | inactive | active | deleted

    3            0           2        1

    8            0           1        0

    7            0           1        0

    9            1           0        0
4

2 に答える 2

1

これを試してください:

SQLFIDDLEデモ

select studentenrollid,
sum(case when studentstatus = 1 then 1
    else 0 end) as Active, 
sum(case when studentstatus = 0 then 1
    else 0 end) as Inactive, 
sum(case when studentstatus = 2 then 1
    else 0 end) as Deleted
from demo
group by studentenrollid
;

| STUDENTENROLLID | ACTIVE | INACTIVE | DELETED |
-------------------------------------------------
|               3 |      2 |        0 |       1 |
|               7 |      1 |        0 |       0 |
|               8 |      1 |        0 |       0 |
|               9 |      0 |        1 |       0 |

別の短いクエリ:

select a.studentenrollid,
sum(a.studentstatus = 1) as Active, 
sum(a.studentstatus = 0) as Inactive,
sum(a.studentstatus = 2) as Deleted 
from demo a
group by a.studentenrollid
;

| STUDENTENROLLID | ACTIVE | INACTIVE | DELETED |
-------------------------------------------------
|               3 |      2 |        0 |       1 |
|               7 |      1 |        0 |       0 |
|               8 |      1 |        0 |       0 |
|               9 |      0 |        1 |       0 |

MYSQL5.1.61バージョンクエリ


コメントに記載されているOPのcreatestatment/insertステートメントに基づきます。

SQLFIDDELデモ2

| STUDENTENROLLID | ACTIVE | INACTIVE | DELETED |
-------------------------------------------------
|               1 |      1 |        0 |       0 |
|               2 |      1 |        0 |       0 |
|               3 |      1 |        0 |       0 |
|              41 |      9 |        1 |       1 |
于 2013-01-29T17:18:19.503 に答える
0

CASE式で集計関数を使用して、データをピボットできます。

select studentenrollid,
  count(case when studentstatus = '0' then studentid end) InActive,
  count(case when studentstatus = '1' then studentid end) Active,
  count(case when studentstatus = '2' then studentid end) Deleted
from yourtable
group by studentenrollid

SQL FiddlewithDemoを参照してください。

結果は次のとおりです。

| STUDENTENROLLID | INACTIVE | ACTIVE | DELETED |
-------------------------------------------------
|               3 |        0 |      2 |       1 |
|               7 |        0 |      1 |       0 |
|               8 |        0 |      1 |       0 |
|               9 |        1 |      0 |       0 |

コメントに配置したサンプルデータを使用して、#1を編集します。

CREATE TABLE IF NOT EXISTS demo 
( 
  studentid int(11) NOT NULL AUTO_INCREMENT, 
  studentenrollid int(11) NOT NULL, 
  studentstatus enum('0','1','2') NOT NULL DEFAULT '1', 
  PRIMARY KEY (studentid), 
  KEY studentenrollid (studentenrollid) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=84 ;


INSERT INTO demo (studentid, studentenrollid, studentstatus) 
VALUES
(22, 41, '2'),
(23, 1,'1'),
(24, 2, '1'),
(25, 3, '1'),
(26, 41, '1'),
(29, 41, '1'),
(30, 41, '1'),
(31, 41, '1'),
(32, 41, '1'),
(33, 41, '1'),
(34, 41,'1'),
(35, 41, '1'),
(36, 41, '1'),
(37, 41, '1') 

クエリは次のようになります。

select studentenrollid,
  count(case when studentstatus = '0' then studentid end) InActive,
  count(case when studentstatus = '1' then studentid end) Active,
  count(case when studentstatus = '2' then studentid end) Deleted
from demo
group by studentenrollid

結果(デモ付きのSQLフィドルを参照):

| STUDENTENROLLID | INACTIVE | ACTIVE | DELETED |
-------------------------------------------------
|               1 |        0 |      1 |       0 |
|               2 |        0 |      1 |       0 |
|               3 |        0 |      1 |       0 |
|              41 |        0 |     10 |       1 |

データベース(少なくともサンプルデータ)にある唯一の値であるため、削除されたカウントのみを取得していることに注意してください。

于 2013-01-29T17:12:05.673 に答える