Laman

Query Pengelompokan Merek dengan Jenis

Gambarannya seperti ini
Langsung saja SQL-nya:

CREATE TABLE tabela (
    nomor  integer,
    tgl_kerusakan date,
    merek VARCHAR(35),
    kerusakan VARCHAR(35)
);

INSERT INTO tabela (nomor, tgl_kerusakan, merek, kerusakan) VALUES
    (1, '2014/09/26', 'Samsung', 'Mesin'),
    (2, '2014/09/26', 'Nokia', 'Speaker'),
    (3, '2014/09/26', 'Nokia', 'Mesin'),
    (4, '2014/09/26', 'Blackberry', 'LCD'),
    (5, '2014/09/26', 'Samsung', 'Mesin'),
    (6, '2014/09/26', 'Sony', 'Batre'),
    (7, '2014/09/27', 'Samsung', 'Mesin')
;

 SELECT merek,
         SUM(CASE WHEN kerusakan = "Mesin" THEN 1 ELSE 0 END) AS Mesin,
         SUM(CASE WHEN kerusakan = "Speaker" THEN 1 ELSE 0 END) AS Speaker,
         SUM(CASE WHEN kerusakan = "LCD" THEN 1 ELSE 0 END) AS LCD,
         SUM(CASE WHEN kerusakan = "Batre" THEN 1 ELSE 0 END) AS Batre,
         (SUM(CASE WHEN kerusakan = "Mesin" THEN 1 ELSE 0 END) +
          SUM(CASE WHEN kerusakan = "Speaker" THEN 1 ELSE 0 END) +
          SUM(CASE WHEN kerusakan = "LCD" THEN 1 ELSE 0 END) +
          SUM(CASE WHEN kerusakan = "Batre" THEN 1 ELSE 0 END)) AS Jumlah
FROM
     tabela
WHERE
     tgl_kerusakan = "2014/09/26"
GROUP BY
     merek

No comments:

Post a Comment

Silahkan