MySQL - Join With Default Value









up vote
-1
down vote

favorite












I have standard data that came from MySql statement



SELECT tipe.* 
FROM
( SELECT 'Menunggu Approve Atasan' AS tipe UNION
SELECT 'Sudah Approved Atasan' UNION
SELECT 'Dalam Proses Pengerjaan IT' UNION
SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
SELECT 'Ditutup'
) AS tipe;
+---------------------------------------+
| tipe |
+---------------------------------------+
| Menunggu Approve Atasan |
| Sudah Approved Atasan |
| Dalam Proses Pengerjaan IT |
| Sudah Selesai Tapi Belum Ditutup User |
| Ditutup |
+---------------------------------------+
5 rows in set (0.02 sec)


Then I have data like this



SELECT requests.* 
FROM
( SELECT DATE(request.tanggal_permintaan) as tanggal_permintaan
, request.`status`
, COUNT(*) as total
FROM request
WHERE YEAR(request.tanggal_permintaan) = '2018'
GROUP
BY DATE(request.tanggal_permintaan)
, request.status
) AS requests;
+--------------------+---------------------------------------+-------+
| tanggal_permintaan | status | total |
+--------------------+---------------------------------------+-------+
| 2018-01-02 | Menunggu Approve Atasan | 1 |
| 2018-01-02 | Ditutup | 4 |
| 2018-01-03 | Ditutup | 1 |
+--------------------+---------------------------------------+-------+
3 rows in set (0.02 sec)


As you can see form result of the second query, it gives me data based on date. But, I need to include all the standard data in those second query.
I need result like this:



+--------------------+---------------------------------------+-------+
| tanggal_permintaan | status | total |
+--------------------+---------------------------------------+-------+
| 2018-01-02 | Menunggu Approve Atasan | 1 |
| 2018-01-02 | Sudah Approved Atasan | 0 |
| 2018-01-02 | Dalam Proses Pengerjaan IT | 0 |
| 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User | 0 |
| 2018-01-02 | Ditutup | 4 | // one day
| 2018-01-03 | Menunggu Approve Atasan | 0 |
| 2018-01-03 | Sudah Approved Atasan | 0 |
| 2018-01-03 | Dalam Proses Pengerjaan IT | 0 |
| 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User | 0 |
| 2018-01-03 | Ditutup | 1 | // one day
+--------------------+---------------------------------------+-------+


What query that I use to get like this ?
So far, I just try the left outer join but no luck



SELECT requests.* FROM (
SELECT
DATE(request.tanggal_permintaan) as tanggal_permintaan,
request.`status`,
COUNT(*) as total
FROM request
WHERE YEAR(request.tanggal_permintaan) = '2018'
GROUP BY DATE(request.tanggal_permintaan), request.status
) requests

LEFT OUTER JOIN (
SELECT tipe.* FROM(
SELECT 'Menunggu Approve Atasan' AS tipe UNION
SELECT 'Sudah Approved Atasan' UNION
SELECT 'Dalam Proses Pengerjaan IT' UNION
SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
SELECT 'Ditutup'
) tipe
)standard
ON standard.tipe = requests.status

ORDER BY requests.tanggal_permintaan









share|improve this question



























    up vote
    -1
    down vote

    favorite












    I have standard data that came from MySql statement



    SELECT tipe.* 
    FROM
    ( SELECT 'Menunggu Approve Atasan' AS tipe UNION
    SELECT 'Sudah Approved Atasan' UNION
    SELECT 'Dalam Proses Pengerjaan IT' UNION
    SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
    SELECT 'Ditutup'
    ) AS tipe;
    +---------------------------------------+
    | tipe |
    +---------------------------------------+
    | Menunggu Approve Atasan |
    | Sudah Approved Atasan |
    | Dalam Proses Pengerjaan IT |
    | Sudah Selesai Tapi Belum Ditutup User |
    | Ditutup |
    +---------------------------------------+
    5 rows in set (0.02 sec)


    Then I have data like this



    SELECT requests.* 
    FROM
    ( SELECT DATE(request.tanggal_permintaan) as tanggal_permintaan
    , request.`status`
    , COUNT(*) as total
    FROM request
    WHERE YEAR(request.tanggal_permintaan) = '2018'
    GROUP
    BY DATE(request.tanggal_permintaan)
    , request.status
    ) AS requests;
    +--------------------+---------------------------------------+-------+
    | tanggal_permintaan | status | total |
    +--------------------+---------------------------------------+-------+
    | 2018-01-02 | Menunggu Approve Atasan | 1 |
    | 2018-01-02 | Ditutup | 4 |
    | 2018-01-03 | Ditutup | 1 |
    +--------------------+---------------------------------------+-------+
    3 rows in set (0.02 sec)


    As you can see form result of the second query, it gives me data based on date. But, I need to include all the standard data in those second query.
    I need result like this:



    +--------------------+---------------------------------------+-------+
    | tanggal_permintaan | status | total |
    +--------------------+---------------------------------------+-------+
    | 2018-01-02 | Menunggu Approve Atasan | 1 |
    | 2018-01-02 | Sudah Approved Atasan | 0 |
    | 2018-01-02 | Dalam Proses Pengerjaan IT | 0 |
    | 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User | 0 |
    | 2018-01-02 | Ditutup | 4 | // one day
    | 2018-01-03 | Menunggu Approve Atasan | 0 |
    | 2018-01-03 | Sudah Approved Atasan | 0 |
    | 2018-01-03 | Dalam Proses Pengerjaan IT | 0 |
    | 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User | 0 |
    | 2018-01-03 | Ditutup | 1 | // one day
    +--------------------+---------------------------------------+-------+


    What query that I use to get like this ?
    So far, I just try the left outer join but no luck



    SELECT requests.* FROM (
    SELECT
    DATE(request.tanggal_permintaan) as tanggal_permintaan,
    request.`status`,
    COUNT(*) as total
    FROM request
    WHERE YEAR(request.tanggal_permintaan) = '2018'
    GROUP BY DATE(request.tanggal_permintaan), request.status
    ) requests

    LEFT OUTER JOIN (
    SELECT tipe.* FROM(
    SELECT 'Menunggu Approve Atasan' AS tipe UNION
    SELECT 'Sudah Approved Atasan' UNION
    SELECT 'Dalam Proses Pengerjaan IT' UNION
    SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
    SELECT 'Ditutup'
    ) tipe
    )standard
    ON standard.tipe = requests.status

    ORDER BY requests.tanggal_permintaan









    share|improve this question

























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I have standard data that came from MySql statement



      SELECT tipe.* 
      FROM
      ( SELECT 'Menunggu Approve Atasan' AS tipe UNION
      SELECT 'Sudah Approved Atasan' UNION
      SELECT 'Dalam Proses Pengerjaan IT' UNION
      SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
      SELECT 'Ditutup'
      ) AS tipe;
      +---------------------------------------+
      | tipe |
      +---------------------------------------+
      | Menunggu Approve Atasan |
      | Sudah Approved Atasan |
      | Dalam Proses Pengerjaan IT |
      | Sudah Selesai Tapi Belum Ditutup User |
      | Ditutup |
      +---------------------------------------+
      5 rows in set (0.02 sec)


      Then I have data like this



      SELECT requests.* 
      FROM
      ( SELECT DATE(request.tanggal_permintaan) as tanggal_permintaan
      , request.`status`
      , COUNT(*) as total
      FROM request
      WHERE YEAR(request.tanggal_permintaan) = '2018'
      GROUP
      BY DATE(request.tanggal_permintaan)
      , request.status
      ) AS requests;
      +--------------------+---------------------------------------+-------+
      | tanggal_permintaan | status | total |
      +--------------------+---------------------------------------+-------+
      | 2018-01-02 | Menunggu Approve Atasan | 1 |
      | 2018-01-02 | Ditutup | 4 |
      | 2018-01-03 | Ditutup | 1 |
      +--------------------+---------------------------------------+-------+
      3 rows in set (0.02 sec)


      As you can see form result of the second query, it gives me data based on date. But, I need to include all the standard data in those second query.
      I need result like this:



      +--------------------+---------------------------------------+-------+
      | tanggal_permintaan | status | total |
      +--------------------+---------------------------------------+-------+
      | 2018-01-02 | Menunggu Approve Atasan | 1 |
      | 2018-01-02 | Sudah Approved Atasan | 0 |
      | 2018-01-02 | Dalam Proses Pengerjaan IT | 0 |
      | 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User | 0 |
      | 2018-01-02 | Ditutup | 4 | // one day
      | 2018-01-03 | Menunggu Approve Atasan | 0 |
      | 2018-01-03 | Sudah Approved Atasan | 0 |
      | 2018-01-03 | Dalam Proses Pengerjaan IT | 0 |
      | 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User | 0 |
      | 2018-01-03 | Ditutup | 1 | // one day
      +--------------------+---------------------------------------+-------+


      What query that I use to get like this ?
      So far, I just try the left outer join but no luck



      SELECT requests.* FROM (
      SELECT
      DATE(request.tanggal_permintaan) as tanggal_permintaan,
      request.`status`,
      COUNT(*) as total
      FROM request
      WHERE YEAR(request.tanggal_permintaan) = '2018'
      GROUP BY DATE(request.tanggal_permintaan), request.status
      ) requests

      LEFT OUTER JOIN (
      SELECT tipe.* FROM(
      SELECT 'Menunggu Approve Atasan' AS tipe UNION
      SELECT 'Sudah Approved Atasan' UNION
      SELECT 'Dalam Proses Pengerjaan IT' UNION
      SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
      SELECT 'Ditutup'
      ) tipe
      )standard
      ON standard.tipe = requests.status

      ORDER BY requests.tanggal_permintaan









      share|improve this question















      I have standard data that came from MySql statement



      SELECT tipe.* 
      FROM
      ( SELECT 'Menunggu Approve Atasan' AS tipe UNION
      SELECT 'Sudah Approved Atasan' UNION
      SELECT 'Dalam Proses Pengerjaan IT' UNION
      SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
      SELECT 'Ditutup'
      ) AS tipe;
      +---------------------------------------+
      | tipe |
      +---------------------------------------+
      | Menunggu Approve Atasan |
      | Sudah Approved Atasan |
      | Dalam Proses Pengerjaan IT |
      | Sudah Selesai Tapi Belum Ditutup User |
      | Ditutup |
      +---------------------------------------+
      5 rows in set (0.02 sec)


      Then I have data like this



      SELECT requests.* 
      FROM
      ( SELECT DATE(request.tanggal_permintaan) as tanggal_permintaan
      , request.`status`
      , COUNT(*) as total
      FROM request
      WHERE YEAR(request.tanggal_permintaan) = '2018'
      GROUP
      BY DATE(request.tanggal_permintaan)
      , request.status
      ) AS requests;
      +--------------------+---------------------------------------+-------+
      | tanggal_permintaan | status | total |
      +--------------------+---------------------------------------+-------+
      | 2018-01-02 | Menunggu Approve Atasan | 1 |
      | 2018-01-02 | Ditutup | 4 |
      | 2018-01-03 | Ditutup | 1 |
      +--------------------+---------------------------------------+-------+
      3 rows in set (0.02 sec)


      As you can see form result of the second query, it gives me data based on date. But, I need to include all the standard data in those second query.
      I need result like this:



      +--------------------+---------------------------------------+-------+
      | tanggal_permintaan | status | total |
      +--------------------+---------------------------------------+-------+
      | 2018-01-02 | Menunggu Approve Atasan | 1 |
      | 2018-01-02 | Sudah Approved Atasan | 0 |
      | 2018-01-02 | Dalam Proses Pengerjaan IT | 0 |
      | 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User | 0 |
      | 2018-01-02 | Ditutup | 4 | // one day
      | 2018-01-03 | Menunggu Approve Atasan | 0 |
      | 2018-01-03 | Sudah Approved Atasan | 0 |
      | 2018-01-03 | Dalam Proses Pengerjaan IT | 0 |
      | 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User | 0 |
      | 2018-01-03 | Ditutup | 1 | // one day
      +--------------------+---------------------------------------+-------+


      What query that I use to get like this ?
      So far, I just try the left outer join but no luck



      SELECT requests.* FROM (
      SELECT
      DATE(request.tanggal_permintaan) as tanggal_permintaan,
      request.`status`,
      COUNT(*) as total
      FROM request
      WHERE YEAR(request.tanggal_permintaan) = '2018'
      GROUP BY DATE(request.tanggal_permintaan), request.status
      ) requests

      LEFT OUTER JOIN (
      SELECT tipe.* FROM(
      SELECT 'Menunggu Approve Atasan' AS tipe UNION
      SELECT 'Sudah Approved Atasan' UNION
      SELECT 'Dalam Proses Pengerjaan IT' UNION
      SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
      SELECT 'Ditutup'
      ) tipe
      )standard
      ON standard.tipe = requests.status

      ORDER BY requests.tanggal_permintaan






      mysql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 5:39









      Strawberry

      25.8k83149




      25.8k83149










      asked Nov 12 at 5:18









      Fadly Dzil

      8111239




      8111239






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote













          To get that output, you need to create a CROSS JOIN of your tipe values with each of the distinct dates in your request table:



          SELECT r.date, t.tipe
          FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
          SELECT 'Sudah Approved Atasan' UNION
          SELECT 'Dalam Proses Pengerjaan IT' UNION
          SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
          SELECT 'Ditutup'
          ) t
          CROSS JOIN
          (SELECT DISTINCT DATE(tanggal_permintaan) AS date
          FROM request
          WHERE YEAR(tanggal_permintaan) = 2018) r


          This will give you a table that looks like this:



          | date | tipe |
          +--------------------+---------------------------------------+
          | 2018-01-02 | Menunggu Approve Atasan |
          | 2018-01-02 | Sudah Approved Atasan |
          | 2018-01-02 | Dalam Proses Pengerjaan IT |
          | 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User |
          | 2018-01-02 | Ditutup |
          | 2018-01-03 | Menunggu Approve Atasan |
          | 2018-01-03 | Sudah Approved Atasan |
          | 2018-01-03 | Dalam Proses Pengerjaan IT |
          | 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User |
          | 2018-01-03 | Ditutup |


          You can then LEFT JOIN that to your requests table to get the result you want:



          SELECT s.date, s.tipe, COUNT(*) AS total
          FROM (SELECT r.date, t.tipe
          FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
          SELECT 'Sudah Approved Atasan' UNION
          SELECT 'Dalam Proses Pengerjaan IT' UNION
          SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
          SELECT 'Ditutup'
          ) t
          CROSS JOIN
          (SELECT DISTINCT DATE(tanggal_permintaan) AS date
          FROM request
          WHERE YEAR(tanggal_permintaan) = 2018) r
          ) s
          LEFT JOIN request rq ON rq.status = s.tipe
          GROUP BY s.date, s.tipe





          share|improve this answer




















          • Basically you give me a Enlightenment. Thanks
            – Fadly Dzil
            Nov 12 at 6:01

















          up vote
          1
          down vote













          • We can get all unique dates in the year 2018 in a Derived table. Year() function on the data does not allow it to use Indexes. I have changed it to use Between.. so that it can use the index.

          • Similarly, get all unique statuses in a separate Derived Table.


          • Cross Join between them to get all the possible combinations.

          • Now, use this all_combinations and left join to your main query, on status and date, to get the required result-set.

          Try the following:



          SELECT all_combinations.tanggal_permintaan, 
          all_combinations.tipe,
          COALESCE(requests.total, 0) AS total
          FROM
          (
          SELECT all_tanggal_permintaan.tanggal_permintaan,
          all_tipe.tipe
          FROM
          (SELECT DISTINCT DATE(tanggal_permintaan) as tanggal_permintaan
          FROM request
          WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
          '2018-12-31 23:59:59'
          ) AS all_tanggal_permintaan

          CROSS JOIN

          (SELECT 'Menunggu Approve Atasan' AS tipe UNION ALL
          SELECT 'Sudah Approved Atasan' UNION ALL
          SELECT 'Dalam Proses Pengerjaan IT' UNION ALL
          SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION ALL
          SELECT 'Ditutup'
          ) AS all_tipe
          ) AS all_combinations

          LEFT OUTER JOIN

          (
          SELECT
          DATE(tanggal_permintaan) as tanggal_permintaan,
          status,
          COUNT(*) AS total
          FROM request
          WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
          '2018-12-31 23:59:59'
          GROUP BY DATE(tanggal_permintaan), status
          ) AS requests
          ON requests.tanggal_permintaan = all_combinations.tanggal_permintaan AND
          requests.status = all_combinations.tipe

          ORDER BY all_combinations.tanggal_permintaan





          share|improve this answer





























            up vote
            1
            down vote













            This is a guess, but it seems you need a list of status values, and it surprises me this isn't already in a table, but this might work:



            SELECT
            g.tanggal_permintaan, g.total, s.status
            FROM (
            SELECT DISTINCT
            request.status
            FROM request
            ) s
            LEFT JOIN (
            SELECT
            DATE(request.tanggal_permintaan) AS tanggal_permintaan
            ,request.status
            ,COUNT(*) AS total
            FROM request
            WHERE request.tanggal_permintaan) >= '20180101'
            AND request.tanggal_permintaan) < '20190101'
            GROUP BY
            DATE(request.tanggal_permintaan)
            ,request.status
            ) g
            ON s.status = g.status





            share|improve this answer






















            • I am sorry brother, it not works. Still default list not loaded
              – Fadly Dzil
              Nov 12 at 5:35










            • OK, I suggest you try the query by Nick. But please avoid using functions on date columns like this YEAR(request.tanggal_permintaan) = '2018' see my query above. also see stackoverflow.com/a/25564544/2067753
              – Used_By_Already
              Nov 12 at 5:47

















            up vote
            0
            down vote













            Thanks for brother @Nick and @Used_By_Already



            Here it is my final query.
            I create a unique id based concat to connecting them via left join.



            SELECT standard.*, transactions.* FROM (
            SELECT CONCAT(r.date, '#', t.tipe) as 'id', r.date AS tanggal_permintaan, t.tipe
            FROM (
            SELECT DISTINCT
            request.status AS tipe
            FROM request
            ) t
            CROSS JOIN (
            SELECT DISTINCT DATE(tanggal_permintaan) AS date
            FROM request
            WHERE YEAR(request.tanggal_permintaan) = '2018'
            ) r

            ) AS standard

            LEFT JOIN (
            SELECT CONCAT(requests.tanggal_permintaan, '#' , requests.status) AS 'id', requests.tanggal_permintaan, requests.status, requests.total
            FROM (
            SELECT
            DATE(request.tanggal_permintaan) as tanggal_permintaan,
            request.`status`,
            COUNT(*) as total
            FROM request
            WHERE YEAR(request.tanggal_permintaan) = '2018'
            GROUP BY DATE(request.tanggal_permintaan), request.status
            ) requests
            ) AS transactions
            ON transactions.id = standard.id

            ORDER BY standard.id





            share|improve this answer




















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256303%2fmysql-join-with-default-value%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              To get that output, you need to create a CROSS JOIN of your tipe values with each of the distinct dates in your request table:



              SELECT r.date, t.tipe
              FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
              SELECT 'Sudah Approved Atasan' UNION
              SELECT 'Dalam Proses Pengerjaan IT' UNION
              SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
              SELECT 'Ditutup'
              ) t
              CROSS JOIN
              (SELECT DISTINCT DATE(tanggal_permintaan) AS date
              FROM request
              WHERE YEAR(tanggal_permintaan) = 2018) r


              This will give you a table that looks like this:



              | date | tipe |
              +--------------------+---------------------------------------+
              | 2018-01-02 | Menunggu Approve Atasan |
              | 2018-01-02 | Sudah Approved Atasan |
              | 2018-01-02 | Dalam Proses Pengerjaan IT |
              | 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User |
              | 2018-01-02 | Ditutup |
              | 2018-01-03 | Menunggu Approve Atasan |
              | 2018-01-03 | Sudah Approved Atasan |
              | 2018-01-03 | Dalam Proses Pengerjaan IT |
              | 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User |
              | 2018-01-03 | Ditutup |


              You can then LEFT JOIN that to your requests table to get the result you want:



              SELECT s.date, s.tipe, COUNT(*) AS total
              FROM (SELECT r.date, t.tipe
              FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
              SELECT 'Sudah Approved Atasan' UNION
              SELECT 'Dalam Proses Pengerjaan IT' UNION
              SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
              SELECT 'Ditutup'
              ) t
              CROSS JOIN
              (SELECT DISTINCT DATE(tanggal_permintaan) AS date
              FROM request
              WHERE YEAR(tanggal_permintaan) = 2018) r
              ) s
              LEFT JOIN request rq ON rq.status = s.tipe
              GROUP BY s.date, s.tipe





              share|improve this answer




















              • Basically you give me a Enlightenment. Thanks
                – Fadly Dzil
                Nov 12 at 6:01














              up vote
              2
              down vote













              To get that output, you need to create a CROSS JOIN of your tipe values with each of the distinct dates in your request table:



              SELECT r.date, t.tipe
              FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
              SELECT 'Sudah Approved Atasan' UNION
              SELECT 'Dalam Proses Pengerjaan IT' UNION
              SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
              SELECT 'Ditutup'
              ) t
              CROSS JOIN
              (SELECT DISTINCT DATE(tanggal_permintaan) AS date
              FROM request
              WHERE YEAR(tanggal_permintaan) = 2018) r


              This will give you a table that looks like this:



              | date | tipe |
              +--------------------+---------------------------------------+
              | 2018-01-02 | Menunggu Approve Atasan |
              | 2018-01-02 | Sudah Approved Atasan |
              | 2018-01-02 | Dalam Proses Pengerjaan IT |
              | 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User |
              | 2018-01-02 | Ditutup |
              | 2018-01-03 | Menunggu Approve Atasan |
              | 2018-01-03 | Sudah Approved Atasan |
              | 2018-01-03 | Dalam Proses Pengerjaan IT |
              | 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User |
              | 2018-01-03 | Ditutup |


              You can then LEFT JOIN that to your requests table to get the result you want:



              SELECT s.date, s.tipe, COUNT(*) AS total
              FROM (SELECT r.date, t.tipe
              FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
              SELECT 'Sudah Approved Atasan' UNION
              SELECT 'Dalam Proses Pengerjaan IT' UNION
              SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
              SELECT 'Ditutup'
              ) t
              CROSS JOIN
              (SELECT DISTINCT DATE(tanggal_permintaan) AS date
              FROM request
              WHERE YEAR(tanggal_permintaan) = 2018) r
              ) s
              LEFT JOIN request rq ON rq.status = s.tipe
              GROUP BY s.date, s.tipe





              share|improve this answer




















              • Basically you give me a Enlightenment. Thanks
                – Fadly Dzil
                Nov 12 at 6:01












              up vote
              2
              down vote










              up vote
              2
              down vote









              To get that output, you need to create a CROSS JOIN of your tipe values with each of the distinct dates in your request table:



              SELECT r.date, t.tipe
              FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
              SELECT 'Sudah Approved Atasan' UNION
              SELECT 'Dalam Proses Pengerjaan IT' UNION
              SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
              SELECT 'Ditutup'
              ) t
              CROSS JOIN
              (SELECT DISTINCT DATE(tanggal_permintaan) AS date
              FROM request
              WHERE YEAR(tanggal_permintaan) = 2018) r


              This will give you a table that looks like this:



              | date | tipe |
              +--------------------+---------------------------------------+
              | 2018-01-02 | Menunggu Approve Atasan |
              | 2018-01-02 | Sudah Approved Atasan |
              | 2018-01-02 | Dalam Proses Pengerjaan IT |
              | 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User |
              | 2018-01-02 | Ditutup |
              | 2018-01-03 | Menunggu Approve Atasan |
              | 2018-01-03 | Sudah Approved Atasan |
              | 2018-01-03 | Dalam Proses Pengerjaan IT |
              | 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User |
              | 2018-01-03 | Ditutup |


              You can then LEFT JOIN that to your requests table to get the result you want:



              SELECT s.date, s.tipe, COUNT(*) AS total
              FROM (SELECT r.date, t.tipe
              FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
              SELECT 'Sudah Approved Atasan' UNION
              SELECT 'Dalam Proses Pengerjaan IT' UNION
              SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
              SELECT 'Ditutup'
              ) t
              CROSS JOIN
              (SELECT DISTINCT DATE(tanggal_permintaan) AS date
              FROM request
              WHERE YEAR(tanggal_permintaan) = 2018) r
              ) s
              LEFT JOIN request rq ON rq.status = s.tipe
              GROUP BY s.date, s.tipe





              share|improve this answer












              To get that output, you need to create a CROSS JOIN of your tipe values with each of the distinct dates in your request table:



              SELECT r.date, t.tipe
              FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
              SELECT 'Sudah Approved Atasan' UNION
              SELECT 'Dalam Proses Pengerjaan IT' UNION
              SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
              SELECT 'Ditutup'
              ) t
              CROSS JOIN
              (SELECT DISTINCT DATE(tanggal_permintaan) AS date
              FROM request
              WHERE YEAR(tanggal_permintaan) = 2018) r


              This will give you a table that looks like this:



              | date | tipe |
              +--------------------+---------------------------------------+
              | 2018-01-02 | Menunggu Approve Atasan |
              | 2018-01-02 | Sudah Approved Atasan |
              | 2018-01-02 | Dalam Proses Pengerjaan IT |
              | 2018-01-02 | Sudah Selesai Tapi Belum Ditutup User |
              | 2018-01-02 | Ditutup |
              | 2018-01-03 | Menunggu Approve Atasan |
              | 2018-01-03 | Sudah Approved Atasan |
              | 2018-01-03 | Dalam Proses Pengerjaan IT |
              | 2018-01-03 | Sudah Selesai Tapi Belum Ditutup User |
              | 2018-01-03 | Ditutup |


              You can then LEFT JOIN that to your requests table to get the result you want:



              SELECT s.date, s.tipe, COUNT(*) AS total
              FROM (SELECT r.date, t.tipe
              FROM (SELECT 'Menunggu Approve Atasan' AS tipe UNION
              SELECT 'Sudah Approved Atasan' UNION
              SELECT 'Dalam Proses Pengerjaan IT' UNION
              SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION
              SELECT 'Ditutup'
              ) t
              CROSS JOIN
              (SELECT DISTINCT DATE(tanggal_permintaan) AS date
              FROM request
              WHERE YEAR(tanggal_permintaan) = 2018) r
              ) s
              LEFT JOIN request rq ON rq.status = s.tipe
              GROUP BY s.date, s.tipe






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 12 at 5:35









              Nick

              22.3k71535




              22.3k71535











              • Basically you give me a Enlightenment. Thanks
                – Fadly Dzil
                Nov 12 at 6:01
















              • Basically you give me a Enlightenment. Thanks
                – Fadly Dzil
                Nov 12 at 6:01















              Basically you give me a Enlightenment. Thanks
              – Fadly Dzil
              Nov 12 at 6:01




              Basically you give me a Enlightenment. Thanks
              – Fadly Dzil
              Nov 12 at 6:01












              up vote
              1
              down vote













              • We can get all unique dates in the year 2018 in a Derived table. Year() function on the data does not allow it to use Indexes. I have changed it to use Between.. so that it can use the index.

              • Similarly, get all unique statuses in a separate Derived Table.


              • Cross Join between them to get all the possible combinations.

              • Now, use this all_combinations and left join to your main query, on status and date, to get the required result-set.

              Try the following:



              SELECT all_combinations.tanggal_permintaan, 
              all_combinations.tipe,
              COALESCE(requests.total, 0) AS total
              FROM
              (
              SELECT all_tanggal_permintaan.tanggal_permintaan,
              all_tipe.tipe
              FROM
              (SELECT DISTINCT DATE(tanggal_permintaan) as tanggal_permintaan
              FROM request
              WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
              '2018-12-31 23:59:59'
              ) AS all_tanggal_permintaan

              CROSS JOIN

              (SELECT 'Menunggu Approve Atasan' AS tipe UNION ALL
              SELECT 'Sudah Approved Atasan' UNION ALL
              SELECT 'Dalam Proses Pengerjaan IT' UNION ALL
              SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION ALL
              SELECT 'Ditutup'
              ) AS all_tipe
              ) AS all_combinations

              LEFT OUTER JOIN

              (
              SELECT
              DATE(tanggal_permintaan) as tanggal_permintaan,
              status,
              COUNT(*) AS total
              FROM request
              WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
              '2018-12-31 23:59:59'
              GROUP BY DATE(tanggal_permintaan), status
              ) AS requests
              ON requests.tanggal_permintaan = all_combinations.tanggal_permintaan AND
              requests.status = all_combinations.tipe

              ORDER BY all_combinations.tanggal_permintaan





              share|improve this answer


























                up vote
                1
                down vote













                • We can get all unique dates in the year 2018 in a Derived table. Year() function on the data does not allow it to use Indexes. I have changed it to use Between.. so that it can use the index.

                • Similarly, get all unique statuses in a separate Derived Table.


                • Cross Join between them to get all the possible combinations.

                • Now, use this all_combinations and left join to your main query, on status and date, to get the required result-set.

                Try the following:



                SELECT all_combinations.tanggal_permintaan, 
                all_combinations.tipe,
                COALESCE(requests.total, 0) AS total
                FROM
                (
                SELECT all_tanggal_permintaan.tanggal_permintaan,
                all_tipe.tipe
                FROM
                (SELECT DISTINCT DATE(tanggal_permintaan) as tanggal_permintaan
                FROM request
                WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
                '2018-12-31 23:59:59'
                ) AS all_tanggal_permintaan

                CROSS JOIN

                (SELECT 'Menunggu Approve Atasan' AS tipe UNION ALL
                SELECT 'Sudah Approved Atasan' UNION ALL
                SELECT 'Dalam Proses Pengerjaan IT' UNION ALL
                SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION ALL
                SELECT 'Ditutup'
                ) AS all_tipe
                ) AS all_combinations

                LEFT OUTER JOIN

                (
                SELECT
                DATE(tanggal_permintaan) as tanggal_permintaan,
                status,
                COUNT(*) AS total
                FROM request
                WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
                '2018-12-31 23:59:59'
                GROUP BY DATE(tanggal_permintaan), status
                ) AS requests
                ON requests.tanggal_permintaan = all_combinations.tanggal_permintaan AND
                requests.status = all_combinations.tipe

                ORDER BY all_combinations.tanggal_permintaan





                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  • We can get all unique dates in the year 2018 in a Derived table. Year() function on the data does not allow it to use Indexes. I have changed it to use Between.. so that it can use the index.

                  • Similarly, get all unique statuses in a separate Derived Table.


                  • Cross Join between them to get all the possible combinations.

                  • Now, use this all_combinations and left join to your main query, on status and date, to get the required result-set.

                  Try the following:



                  SELECT all_combinations.tanggal_permintaan, 
                  all_combinations.tipe,
                  COALESCE(requests.total, 0) AS total
                  FROM
                  (
                  SELECT all_tanggal_permintaan.tanggal_permintaan,
                  all_tipe.tipe
                  FROM
                  (SELECT DISTINCT DATE(tanggal_permintaan) as tanggal_permintaan
                  FROM request
                  WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
                  '2018-12-31 23:59:59'
                  ) AS all_tanggal_permintaan

                  CROSS JOIN

                  (SELECT 'Menunggu Approve Atasan' AS tipe UNION ALL
                  SELECT 'Sudah Approved Atasan' UNION ALL
                  SELECT 'Dalam Proses Pengerjaan IT' UNION ALL
                  SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION ALL
                  SELECT 'Ditutup'
                  ) AS all_tipe
                  ) AS all_combinations

                  LEFT OUTER JOIN

                  (
                  SELECT
                  DATE(tanggal_permintaan) as tanggal_permintaan,
                  status,
                  COUNT(*) AS total
                  FROM request
                  WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
                  '2018-12-31 23:59:59'
                  GROUP BY DATE(tanggal_permintaan), status
                  ) AS requests
                  ON requests.tanggal_permintaan = all_combinations.tanggal_permintaan AND
                  requests.status = all_combinations.tipe

                  ORDER BY all_combinations.tanggal_permintaan





                  share|improve this answer














                  • We can get all unique dates in the year 2018 in a Derived table. Year() function on the data does not allow it to use Indexes. I have changed it to use Between.. so that it can use the index.

                  • Similarly, get all unique statuses in a separate Derived Table.


                  • Cross Join between them to get all the possible combinations.

                  • Now, use this all_combinations and left join to your main query, on status and date, to get the required result-set.

                  Try the following:



                  SELECT all_combinations.tanggal_permintaan, 
                  all_combinations.tipe,
                  COALESCE(requests.total, 0) AS total
                  FROM
                  (
                  SELECT all_tanggal_permintaan.tanggal_permintaan,
                  all_tipe.tipe
                  FROM
                  (SELECT DISTINCT DATE(tanggal_permintaan) as tanggal_permintaan
                  FROM request
                  WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
                  '2018-12-31 23:59:59'
                  ) AS all_tanggal_permintaan

                  CROSS JOIN

                  (SELECT 'Menunggu Approve Atasan' AS tipe UNION ALL
                  SELECT 'Sudah Approved Atasan' UNION ALL
                  SELECT 'Dalam Proses Pengerjaan IT' UNION ALL
                  SELECT 'Sudah Selesai Tapi Belum Ditutup User' UNION ALL
                  SELECT 'Ditutup'
                  ) AS all_tipe
                  ) AS all_combinations

                  LEFT OUTER JOIN

                  (
                  SELECT
                  DATE(tanggal_permintaan) as tanggal_permintaan,
                  status,
                  COUNT(*) AS total
                  FROM request
                  WHERE tanggal_permintaan BETWEEN '2018-01-01 00:00:00' AND
                  '2018-12-31 23:59:59'
                  GROUP BY DATE(tanggal_permintaan), status
                  ) AS requests
                  ON requests.tanggal_permintaan = all_combinations.tanggal_permintaan AND
                  requests.status = all_combinations.tipe

                  ORDER BY all_combinations.tanggal_permintaan






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 12 at 5:43

























                  answered Nov 12 at 5:30









                  Madhur Bhaiya

                  19.2k62236




                  19.2k62236




















                      up vote
                      1
                      down vote













                      This is a guess, but it seems you need a list of status values, and it surprises me this isn't already in a table, but this might work:



                      SELECT
                      g.tanggal_permintaan, g.total, s.status
                      FROM (
                      SELECT DISTINCT
                      request.status
                      FROM request
                      ) s
                      LEFT JOIN (
                      SELECT
                      DATE(request.tanggal_permintaan) AS tanggal_permintaan
                      ,request.status
                      ,COUNT(*) AS total
                      FROM request
                      WHERE request.tanggal_permintaan) >= '20180101'
                      AND request.tanggal_permintaan) < '20190101'
                      GROUP BY
                      DATE(request.tanggal_permintaan)
                      ,request.status
                      ) g
                      ON s.status = g.status





                      share|improve this answer






















                      • I am sorry brother, it not works. Still default list not loaded
                        – Fadly Dzil
                        Nov 12 at 5:35










                      • OK, I suggest you try the query by Nick. But please avoid using functions on date columns like this YEAR(request.tanggal_permintaan) = '2018' see my query above. also see stackoverflow.com/a/25564544/2067753
                        – Used_By_Already
                        Nov 12 at 5:47














                      up vote
                      1
                      down vote













                      This is a guess, but it seems you need a list of status values, and it surprises me this isn't already in a table, but this might work:



                      SELECT
                      g.tanggal_permintaan, g.total, s.status
                      FROM (
                      SELECT DISTINCT
                      request.status
                      FROM request
                      ) s
                      LEFT JOIN (
                      SELECT
                      DATE(request.tanggal_permintaan) AS tanggal_permintaan
                      ,request.status
                      ,COUNT(*) AS total
                      FROM request
                      WHERE request.tanggal_permintaan) >= '20180101'
                      AND request.tanggal_permintaan) < '20190101'
                      GROUP BY
                      DATE(request.tanggal_permintaan)
                      ,request.status
                      ) g
                      ON s.status = g.status





                      share|improve this answer






















                      • I am sorry brother, it not works. Still default list not loaded
                        – Fadly Dzil
                        Nov 12 at 5:35










                      • OK, I suggest you try the query by Nick. But please avoid using functions on date columns like this YEAR(request.tanggal_permintaan) = '2018' see my query above. also see stackoverflow.com/a/25564544/2067753
                        – Used_By_Already
                        Nov 12 at 5:47












                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      This is a guess, but it seems you need a list of status values, and it surprises me this isn't already in a table, but this might work:



                      SELECT
                      g.tanggal_permintaan, g.total, s.status
                      FROM (
                      SELECT DISTINCT
                      request.status
                      FROM request
                      ) s
                      LEFT JOIN (
                      SELECT
                      DATE(request.tanggal_permintaan) AS tanggal_permintaan
                      ,request.status
                      ,COUNT(*) AS total
                      FROM request
                      WHERE request.tanggal_permintaan) >= '20180101'
                      AND request.tanggal_permintaan) < '20190101'
                      GROUP BY
                      DATE(request.tanggal_permintaan)
                      ,request.status
                      ) g
                      ON s.status = g.status





                      share|improve this answer














                      This is a guess, but it seems you need a list of status values, and it surprises me this isn't already in a table, but this might work:



                      SELECT
                      g.tanggal_permintaan, g.total, s.status
                      FROM (
                      SELECT DISTINCT
                      request.status
                      FROM request
                      ) s
                      LEFT JOIN (
                      SELECT
                      DATE(request.tanggal_permintaan) AS tanggal_permintaan
                      ,request.status
                      ,COUNT(*) AS total
                      FROM request
                      WHERE request.tanggal_permintaan) >= '20180101'
                      AND request.tanggal_permintaan) < '20190101'
                      GROUP BY
                      DATE(request.tanggal_permintaan)
                      ,request.status
                      ) g
                      ON s.status = g.status






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 12 at 5:58

























                      answered Nov 12 at 5:31









                      Used_By_Already

                      22.3k21838




                      22.3k21838











                      • I am sorry brother, it not works. Still default list not loaded
                        – Fadly Dzil
                        Nov 12 at 5:35










                      • OK, I suggest you try the query by Nick. But please avoid using functions on date columns like this YEAR(request.tanggal_permintaan) = '2018' see my query above. also see stackoverflow.com/a/25564544/2067753
                        – Used_By_Already
                        Nov 12 at 5:47
















                      • I am sorry brother, it not works. Still default list not loaded
                        – Fadly Dzil
                        Nov 12 at 5:35










                      • OK, I suggest you try the query by Nick. But please avoid using functions on date columns like this YEAR(request.tanggal_permintaan) = '2018' see my query above. also see stackoverflow.com/a/25564544/2067753
                        – Used_By_Already
                        Nov 12 at 5:47















                      I am sorry brother, it not works. Still default list not loaded
                      – Fadly Dzil
                      Nov 12 at 5:35




                      I am sorry brother, it not works. Still default list not loaded
                      – Fadly Dzil
                      Nov 12 at 5:35












                      OK, I suggest you try the query by Nick. But please avoid using functions on date columns like this YEAR(request.tanggal_permintaan) = '2018' see my query above. also see stackoverflow.com/a/25564544/2067753
                      – Used_By_Already
                      Nov 12 at 5:47




                      OK, I suggest you try the query by Nick. But please avoid using functions on date columns like this YEAR(request.tanggal_permintaan) = '2018' see my query above. also see stackoverflow.com/a/25564544/2067753
                      – Used_By_Already
                      Nov 12 at 5:47










                      up vote
                      0
                      down vote













                      Thanks for brother @Nick and @Used_By_Already



                      Here it is my final query.
                      I create a unique id based concat to connecting them via left join.



                      SELECT standard.*, transactions.* FROM (
                      SELECT CONCAT(r.date, '#', t.tipe) as 'id', r.date AS tanggal_permintaan, t.tipe
                      FROM (
                      SELECT DISTINCT
                      request.status AS tipe
                      FROM request
                      ) t
                      CROSS JOIN (
                      SELECT DISTINCT DATE(tanggal_permintaan) AS date
                      FROM request
                      WHERE YEAR(request.tanggal_permintaan) = '2018'
                      ) r

                      ) AS standard

                      LEFT JOIN (
                      SELECT CONCAT(requests.tanggal_permintaan, '#' , requests.status) AS 'id', requests.tanggal_permintaan, requests.status, requests.total
                      FROM (
                      SELECT
                      DATE(request.tanggal_permintaan) as tanggal_permintaan,
                      request.`status`,
                      COUNT(*) as total
                      FROM request
                      WHERE YEAR(request.tanggal_permintaan) = '2018'
                      GROUP BY DATE(request.tanggal_permintaan), request.status
                      ) requests
                      ) AS transactions
                      ON transactions.id = standard.id

                      ORDER BY standard.id





                      share|improve this answer
























                        up vote
                        0
                        down vote













                        Thanks for brother @Nick and @Used_By_Already



                        Here it is my final query.
                        I create a unique id based concat to connecting them via left join.



                        SELECT standard.*, transactions.* FROM (
                        SELECT CONCAT(r.date, '#', t.tipe) as 'id', r.date AS tanggal_permintaan, t.tipe
                        FROM (
                        SELECT DISTINCT
                        request.status AS tipe
                        FROM request
                        ) t
                        CROSS JOIN (
                        SELECT DISTINCT DATE(tanggal_permintaan) AS date
                        FROM request
                        WHERE YEAR(request.tanggal_permintaan) = '2018'
                        ) r

                        ) AS standard

                        LEFT JOIN (
                        SELECT CONCAT(requests.tanggal_permintaan, '#' , requests.status) AS 'id', requests.tanggal_permintaan, requests.status, requests.total
                        FROM (
                        SELECT
                        DATE(request.tanggal_permintaan) as tanggal_permintaan,
                        request.`status`,
                        COUNT(*) as total
                        FROM request
                        WHERE YEAR(request.tanggal_permintaan) = '2018'
                        GROUP BY DATE(request.tanggal_permintaan), request.status
                        ) requests
                        ) AS transactions
                        ON transactions.id = standard.id

                        ORDER BY standard.id





                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Thanks for brother @Nick and @Used_By_Already



                          Here it is my final query.
                          I create a unique id based concat to connecting them via left join.



                          SELECT standard.*, transactions.* FROM (
                          SELECT CONCAT(r.date, '#', t.tipe) as 'id', r.date AS tanggal_permintaan, t.tipe
                          FROM (
                          SELECT DISTINCT
                          request.status AS tipe
                          FROM request
                          ) t
                          CROSS JOIN (
                          SELECT DISTINCT DATE(tanggal_permintaan) AS date
                          FROM request
                          WHERE YEAR(request.tanggal_permintaan) = '2018'
                          ) r

                          ) AS standard

                          LEFT JOIN (
                          SELECT CONCAT(requests.tanggal_permintaan, '#' , requests.status) AS 'id', requests.tanggal_permintaan, requests.status, requests.total
                          FROM (
                          SELECT
                          DATE(request.tanggal_permintaan) as tanggal_permintaan,
                          request.`status`,
                          COUNT(*) as total
                          FROM request
                          WHERE YEAR(request.tanggal_permintaan) = '2018'
                          GROUP BY DATE(request.tanggal_permintaan), request.status
                          ) requests
                          ) AS transactions
                          ON transactions.id = standard.id

                          ORDER BY standard.id





                          share|improve this answer












                          Thanks for brother @Nick and @Used_By_Already



                          Here it is my final query.
                          I create a unique id based concat to connecting them via left join.



                          SELECT standard.*, transactions.* FROM (
                          SELECT CONCAT(r.date, '#', t.tipe) as 'id', r.date AS tanggal_permintaan, t.tipe
                          FROM (
                          SELECT DISTINCT
                          request.status AS tipe
                          FROM request
                          ) t
                          CROSS JOIN (
                          SELECT DISTINCT DATE(tanggal_permintaan) AS date
                          FROM request
                          WHERE YEAR(request.tanggal_permintaan) = '2018'
                          ) r

                          ) AS standard

                          LEFT JOIN (
                          SELECT CONCAT(requests.tanggal_permintaan, '#' , requests.status) AS 'id', requests.tanggal_permintaan, requests.status, requests.total
                          FROM (
                          SELECT
                          DATE(request.tanggal_permintaan) as tanggal_permintaan,
                          request.`status`,
                          COUNT(*) as total
                          FROM request
                          WHERE YEAR(request.tanggal_permintaan) = '2018'
                          GROUP BY DATE(request.tanggal_permintaan), request.status
                          ) requests
                          ) AS transactions
                          ON transactions.id = standard.id

                          ORDER BY standard.id






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 at 6:03









                          Fadly Dzil

                          8111239




                          8111239



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256303%2fmysql-join-with-default-value%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              ReactJS Fetched API data displays live - need Data displayed static

                              政党