Transpose the results of a MySQL query that outputs ranges











up vote
1
down vote

favorite












My source table (wplott_wpkl_winner) contains the field "lottery_number" that carries 1 to 6 digit numbers and the corresponding "draw_date".



    lottery_number | draw_date
==================================
0024 | 2018-11-10
4456 | 2018-11-10
3895 | 2018-11-10
4557 | 2018-11-10
4225 | 2018-11-10
2896 | 2018-11-10
3354 | 2018-11-10
1895 | 2018-11-10
78466 | 2018-11-10
998556 | 2018-11-10


My current MYSQL query is as below (I am trying to group the data into ranges)



select
count(case when wplott_wpkl_winner.lottery_number between 0 and 999 then 1 end) `0-999`,
count(case when wplott_wpkl_winner.lottery_number between 1000 and 1999 then 1 end) `1000-1999`,
count(case when wplott_wpkl_winner.lottery_number between 2000 and 2999 then 1 end) `2000-2999`,
count(case when wplott_wpkl_winner.lottery_number between 3000 and 3999 then 1 end) `3000-3999`,
count(case when wplott_wpkl_winner.lottery_number between 4000 and 4999 then 1 end) `4000-4999`,
count(case when wplott_wpkl_winner.lottery_number between 5000 and 5999 then 1 end) `5000-5999`,
count(case when wplott_wpkl_winner.lottery_number between 6000 and 6999 then 1 end) `6000-6999`,
count(case when wplott_wpkl_winner.lottery_number between 7000 and 7999 then 1 end) `7000-7999`,
count(case when wplott_wpkl_winner.lottery_number between 8000 and 8999 then 1 end) `8000-8999`,
count(case when wplott_wpkl_winner.lottery_number between 9000 and 9999 then 1 end) `9000-9999`
from wplott_wpkl_winner
where CHAR_LENGTH(wplott_wpkl_winner.lottery_number) = 4 AND wplott_wpkl_winner.draw_date > '2013-06-30'


It provides the below output



    0-999 | 1000-1999 | 2000-2999 | 3000-3999 | 4000- 4999 .... etc
=====================================================================
1 | 1 | 1 | 2 | 3


However, I would like to get the output in the below format.



    Range     | Count
=======================
0-999 | 1
1000-1999 | 1
2000-2999 | 1
3000-3999 | 2
4000-4999 | 3
.
.
.


Any help is highly appreciated. I did search in SO for a similar answer but none of the answers helped my particular case.



Thanks in advance!










share|improve this question




























    up vote
    1
    down vote

    favorite












    My source table (wplott_wpkl_winner) contains the field "lottery_number" that carries 1 to 6 digit numbers and the corresponding "draw_date".



        lottery_number | draw_date
    ==================================
    0024 | 2018-11-10
    4456 | 2018-11-10
    3895 | 2018-11-10
    4557 | 2018-11-10
    4225 | 2018-11-10
    2896 | 2018-11-10
    3354 | 2018-11-10
    1895 | 2018-11-10
    78466 | 2018-11-10
    998556 | 2018-11-10


    My current MYSQL query is as below (I am trying to group the data into ranges)



    select
    count(case when wplott_wpkl_winner.lottery_number between 0 and 999 then 1 end) `0-999`,
    count(case when wplott_wpkl_winner.lottery_number between 1000 and 1999 then 1 end) `1000-1999`,
    count(case when wplott_wpkl_winner.lottery_number between 2000 and 2999 then 1 end) `2000-2999`,
    count(case when wplott_wpkl_winner.lottery_number between 3000 and 3999 then 1 end) `3000-3999`,
    count(case when wplott_wpkl_winner.lottery_number between 4000 and 4999 then 1 end) `4000-4999`,
    count(case when wplott_wpkl_winner.lottery_number between 5000 and 5999 then 1 end) `5000-5999`,
    count(case when wplott_wpkl_winner.lottery_number between 6000 and 6999 then 1 end) `6000-6999`,
    count(case when wplott_wpkl_winner.lottery_number between 7000 and 7999 then 1 end) `7000-7999`,
    count(case when wplott_wpkl_winner.lottery_number between 8000 and 8999 then 1 end) `8000-8999`,
    count(case when wplott_wpkl_winner.lottery_number between 9000 and 9999 then 1 end) `9000-9999`
    from wplott_wpkl_winner
    where CHAR_LENGTH(wplott_wpkl_winner.lottery_number) = 4 AND wplott_wpkl_winner.draw_date > '2013-06-30'


    It provides the below output



        0-999 | 1000-1999 | 2000-2999 | 3000-3999 | 4000- 4999 .... etc
    =====================================================================
    1 | 1 | 1 | 2 | 3


    However, I would like to get the output in the below format.



        Range     | Count
    =======================
    0-999 | 1
    1000-1999 | 1
    2000-2999 | 1
    3000-3999 | 2
    4000-4999 | 3
    .
    .
    .


    Any help is highly appreciated. I did search in SO for a similar answer but none of the answers helped my particular case.



    Thanks in advance!










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      My source table (wplott_wpkl_winner) contains the field "lottery_number" that carries 1 to 6 digit numbers and the corresponding "draw_date".



          lottery_number | draw_date
      ==================================
      0024 | 2018-11-10
      4456 | 2018-11-10
      3895 | 2018-11-10
      4557 | 2018-11-10
      4225 | 2018-11-10
      2896 | 2018-11-10
      3354 | 2018-11-10
      1895 | 2018-11-10
      78466 | 2018-11-10
      998556 | 2018-11-10


      My current MYSQL query is as below (I am trying to group the data into ranges)



      select
      count(case when wplott_wpkl_winner.lottery_number between 0 and 999 then 1 end) `0-999`,
      count(case when wplott_wpkl_winner.lottery_number between 1000 and 1999 then 1 end) `1000-1999`,
      count(case when wplott_wpkl_winner.lottery_number between 2000 and 2999 then 1 end) `2000-2999`,
      count(case when wplott_wpkl_winner.lottery_number between 3000 and 3999 then 1 end) `3000-3999`,
      count(case when wplott_wpkl_winner.lottery_number between 4000 and 4999 then 1 end) `4000-4999`,
      count(case when wplott_wpkl_winner.lottery_number between 5000 and 5999 then 1 end) `5000-5999`,
      count(case when wplott_wpkl_winner.lottery_number between 6000 and 6999 then 1 end) `6000-6999`,
      count(case when wplott_wpkl_winner.lottery_number between 7000 and 7999 then 1 end) `7000-7999`,
      count(case when wplott_wpkl_winner.lottery_number between 8000 and 8999 then 1 end) `8000-8999`,
      count(case when wplott_wpkl_winner.lottery_number between 9000 and 9999 then 1 end) `9000-9999`
      from wplott_wpkl_winner
      where CHAR_LENGTH(wplott_wpkl_winner.lottery_number) = 4 AND wplott_wpkl_winner.draw_date > '2013-06-30'


      It provides the below output



          0-999 | 1000-1999 | 2000-2999 | 3000-3999 | 4000- 4999 .... etc
      =====================================================================
      1 | 1 | 1 | 2 | 3


      However, I would like to get the output in the below format.



          Range     | Count
      =======================
      0-999 | 1
      1000-1999 | 1
      2000-2999 | 1
      3000-3999 | 2
      4000-4999 | 3
      .
      .
      .


      Any help is highly appreciated. I did search in SO for a similar answer but none of the answers helped my particular case.



      Thanks in advance!










      share|improve this question















      My source table (wplott_wpkl_winner) contains the field "lottery_number" that carries 1 to 6 digit numbers and the corresponding "draw_date".



          lottery_number | draw_date
      ==================================
      0024 | 2018-11-10
      4456 | 2018-11-10
      3895 | 2018-11-10
      4557 | 2018-11-10
      4225 | 2018-11-10
      2896 | 2018-11-10
      3354 | 2018-11-10
      1895 | 2018-11-10
      78466 | 2018-11-10
      998556 | 2018-11-10


      My current MYSQL query is as below (I am trying to group the data into ranges)



      select
      count(case when wplott_wpkl_winner.lottery_number between 0 and 999 then 1 end) `0-999`,
      count(case when wplott_wpkl_winner.lottery_number between 1000 and 1999 then 1 end) `1000-1999`,
      count(case when wplott_wpkl_winner.lottery_number between 2000 and 2999 then 1 end) `2000-2999`,
      count(case when wplott_wpkl_winner.lottery_number between 3000 and 3999 then 1 end) `3000-3999`,
      count(case when wplott_wpkl_winner.lottery_number between 4000 and 4999 then 1 end) `4000-4999`,
      count(case when wplott_wpkl_winner.lottery_number between 5000 and 5999 then 1 end) `5000-5999`,
      count(case when wplott_wpkl_winner.lottery_number between 6000 and 6999 then 1 end) `6000-6999`,
      count(case when wplott_wpkl_winner.lottery_number between 7000 and 7999 then 1 end) `7000-7999`,
      count(case when wplott_wpkl_winner.lottery_number between 8000 and 8999 then 1 end) `8000-8999`,
      count(case when wplott_wpkl_winner.lottery_number between 9000 and 9999 then 1 end) `9000-9999`
      from wplott_wpkl_winner
      where CHAR_LENGTH(wplott_wpkl_winner.lottery_number) = 4 AND wplott_wpkl_winner.draw_date > '2013-06-30'


      It provides the below output



          0-999 | 1000-1999 | 2000-2999 | 3000-3999 | 4000- 4999 .... etc
      =====================================================================
      1 | 1 | 1 | 2 | 3


      However, I would like to get the output in the below format.



          Range     | Count
      =======================
      0-999 | 1
      1000-1999 | 1
      2000-2999 | 1
      3000-3999 | 2
      4000-4999 | 3
      .
      .
      .


      Any help is highly appreciated. I did search in SO for a similar answer but none of the answers helped my particular case.



      Thanks in advance!







      mysql wordpress






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 14:56









      Tim Biegeleisen

      214k1386137




      214k1386137










      asked Nov 10 at 13:29









      Thomas Koipuram

      103




      103
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          One approach uses a series of unions:



          SELECT
          `range`,
          count
          FROM
          (
          SELECT 1 AS pos, '0-999' AS `range`, COUNT(*) AS count
          FROM wplott_wpkl_winner
          WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999
          UNION ALL
          SELECT 2, '1000-1999', COUNT(*)
          FROM wplott_wpkl_winner
          WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999
          UNION ALL
          ... -- fill in remaining ranges here
          ) t
          ORDER BY pos;


          Note that I introduce a computed column pos so that we may maintain the desired ordering of the ranges in the final output. Also, I removed the check on the CHAR_LENGTH of the lottery_number, since the conditional sums already handle this logic.






          share|improve this answer























          • Why not SELECT 1 AS pos, '0-999' AS range, COUNT(1) AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999?
            – billynoah
            Nov 10 at 13:43










          • @billynoah I agree with your suggestion.
            – Tim Biegeleisen
            Nov 10 at 13:44










          • I am using WpDataTables in wordpress to run this query. Seems like it does not support UNION statement :(
            – Thomas Koipuram
            Nov 10 at 14:11










          • What is the error message? Maybe it is from a problem with my code, not just union. You're going to have to use union or some kind of cross tab functionality to generate the output you want.
            – Tim Biegeleisen
            Nov 10 at 14:12










          • Rendered query: SELECT range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT() AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999 UNION ALL SELECT 2, '1000-1999', COUNT() FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999) t ORDER BY pos ASC LIMIT 10 MySQL said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT(*) AS count FROM ' at line 1
            – Thomas Koipuram
            Nov 10 at 14:15











          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',
          autoActivateHeartbeat: false,
          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%2f53239437%2ftranspose-the-results-of-a-mysql-query-that-outputs-ranges%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          One approach uses a series of unions:



          SELECT
          `range`,
          count
          FROM
          (
          SELECT 1 AS pos, '0-999' AS `range`, COUNT(*) AS count
          FROM wplott_wpkl_winner
          WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999
          UNION ALL
          SELECT 2, '1000-1999', COUNT(*)
          FROM wplott_wpkl_winner
          WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999
          UNION ALL
          ... -- fill in remaining ranges here
          ) t
          ORDER BY pos;


          Note that I introduce a computed column pos so that we may maintain the desired ordering of the ranges in the final output. Also, I removed the check on the CHAR_LENGTH of the lottery_number, since the conditional sums already handle this logic.






          share|improve this answer























          • Why not SELECT 1 AS pos, '0-999' AS range, COUNT(1) AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999?
            – billynoah
            Nov 10 at 13:43










          • @billynoah I agree with your suggestion.
            – Tim Biegeleisen
            Nov 10 at 13:44










          • I am using WpDataTables in wordpress to run this query. Seems like it does not support UNION statement :(
            – Thomas Koipuram
            Nov 10 at 14:11










          • What is the error message? Maybe it is from a problem with my code, not just union. You're going to have to use union or some kind of cross tab functionality to generate the output you want.
            – Tim Biegeleisen
            Nov 10 at 14:12










          • Rendered query: SELECT range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT() AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999 UNION ALL SELECT 2, '1000-1999', COUNT() FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999) t ORDER BY pos ASC LIMIT 10 MySQL said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT(*) AS count FROM ' at line 1
            – Thomas Koipuram
            Nov 10 at 14:15















          up vote
          1
          down vote



          accepted










          One approach uses a series of unions:



          SELECT
          `range`,
          count
          FROM
          (
          SELECT 1 AS pos, '0-999' AS `range`, COUNT(*) AS count
          FROM wplott_wpkl_winner
          WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999
          UNION ALL
          SELECT 2, '1000-1999', COUNT(*)
          FROM wplott_wpkl_winner
          WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999
          UNION ALL
          ... -- fill in remaining ranges here
          ) t
          ORDER BY pos;


          Note that I introduce a computed column pos so that we may maintain the desired ordering of the ranges in the final output. Also, I removed the check on the CHAR_LENGTH of the lottery_number, since the conditional sums already handle this logic.






          share|improve this answer























          • Why not SELECT 1 AS pos, '0-999' AS range, COUNT(1) AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999?
            – billynoah
            Nov 10 at 13:43










          • @billynoah I agree with your suggestion.
            – Tim Biegeleisen
            Nov 10 at 13:44










          • I am using WpDataTables in wordpress to run this query. Seems like it does not support UNION statement :(
            – Thomas Koipuram
            Nov 10 at 14:11










          • What is the error message? Maybe it is from a problem with my code, not just union. You're going to have to use union or some kind of cross tab functionality to generate the output you want.
            – Tim Biegeleisen
            Nov 10 at 14:12










          • Rendered query: SELECT range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT() AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999 UNION ALL SELECT 2, '1000-1999', COUNT() FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999) t ORDER BY pos ASC LIMIT 10 MySQL said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT(*) AS count FROM ' at line 1
            – Thomas Koipuram
            Nov 10 at 14:15













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          One approach uses a series of unions:



          SELECT
          `range`,
          count
          FROM
          (
          SELECT 1 AS pos, '0-999' AS `range`, COUNT(*) AS count
          FROM wplott_wpkl_winner
          WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999
          UNION ALL
          SELECT 2, '1000-1999', COUNT(*)
          FROM wplott_wpkl_winner
          WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999
          UNION ALL
          ... -- fill in remaining ranges here
          ) t
          ORDER BY pos;


          Note that I introduce a computed column pos so that we may maintain the desired ordering of the ranges in the final output. Also, I removed the check on the CHAR_LENGTH of the lottery_number, since the conditional sums already handle this logic.






          share|improve this answer














          One approach uses a series of unions:



          SELECT
          `range`,
          count
          FROM
          (
          SELECT 1 AS pos, '0-999' AS `range`, COUNT(*) AS count
          FROM wplott_wpkl_winner
          WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999
          UNION ALL
          SELECT 2, '1000-1999', COUNT(*)
          FROM wplott_wpkl_winner
          WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999
          UNION ALL
          ... -- fill in remaining ranges here
          ) t
          ORDER BY pos;


          Note that I introduce a computed column pos so that we may maintain the desired ordering of the ranges in the final output. Also, I removed the check on the CHAR_LENGTH of the lottery_number, since the conditional sums already handle this logic.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 14:18

























          answered Nov 10 at 13:34









          Tim Biegeleisen

          214k1386137




          214k1386137












          • Why not SELECT 1 AS pos, '0-999' AS range, COUNT(1) AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999?
            – billynoah
            Nov 10 at 13:43










          • @billynoah I agree with your suggestion.
            – Tim Biegeleisen
            Nov 10 at 13:44










          • I am using WpDataTables in wordpress to run this query. Seems like it does not support UNION statement :(
            – Thomas Koipuram
            Nov 10 at 14:11










          • What is the error message? Maybe it is from a problem with my code, not just union. You're going to have to use union or some kind of cross tab functionality to generate the output you want.
            – Tim Biegeleisen
            Nov 10 at 14:12










          • Rendered query: SELECT range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT() AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999 UNION ALL SELECT 2, '1000-1999', COUNT() FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999) t ORDER BY pos ASC LIMIT 10 MySQL said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT(*) AS count FROM ' at line 1
            – Thomas Koipuram
            Nov 10 at 14:15


















          • Why not SELECT 1 AS pos, '0-999' AS range, COUNT(1) AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999?
            – billynoah
            Nov 10 at 13:43










          • @billynoah I agree with your suggestion.
            – Tim Biegeleisen
            Nov 10 at 13:44










          • I am using WpDataTables in wordpress to run this query. Seems like it does not support UNION statement :(
            – Thomas Koipuram
            Nov 10 at 14:11










          • What is the error message? Maybe it is from a problem with my code, not just union. You're going to have to use union or some kind of cross tab functionality to generate the output you want.
            – Tim Biegeleisen
            Nov 10 at 14:12










          • Rendered query: SELECT range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT() AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999 UNION ALL SELECT 2, '1000-1999', COUNT() FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999) t ORDER BY pos ASC LIMIT 10 MySQL said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT(*) AS count FROM ' at line 1
            – Thomas Koipuram
            Nov 10 at 14:15
















          Why not SELECT 1 AS pos, '0-999' AS range, COUNT(1) AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999?
          – billynoah
          Nov 10 at 13:43




          Why not SELECT 1 AS pos, '0-999' AS range, COUNT(1) AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999?
          – billynoah
          Nov 10 at 13:43












          @billynoah I agree with your suggestion.
          – Tim Biegeleisen
          Nov 10 at 13:44




          @billynoah I agree with your suggestion.
          – Tim Biegeleisen
          Nov 10 at 13:44












          I am using WpDataTables in wordpress to run this query. Seems like it does not support UNION statement :(
          – Thomas Koipuram
          Nov 10 at 14:11




          I am using WpDataTables in wordpress to run this query. Seems like it does not support UNION statement :(
          – Thomas Koipuram
          Nov 10 at 14:11












          What is the error message? Maybe it is from a problem with my code, not just union. You're going to have to use union or some kind of cross tab functionality to generate the output you want.
          – Tim Biegeleisen
          Nov 10 at 14:12




          What is the error message? Maybe it is from a problem with my code, not just union. You're going to have to use union or some kind of cross tab functionality to generate the output you want.
          – Tim Biegeleisen
          Nov 10 at 14:12












          Rendered query: SELECT range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT() AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999 UNION ALL SELECT 2, '1000-1999', COUNT() FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999) t ORDER BY pos ASC LIMIT 10 MySQL said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT(*) AS count FROM ' at line 1
          – Thomas Koipuram
          Nov 10 at 14:15




          Rendered query: SELECT range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT() AS count FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 0 AND 999 UNION ALL SELECT 2, '1000-1999', COUNT() FROM wplott_wpkl_winner WHERE draw_date > '2013-06-30' AND lottery_number BETWEEN 1000 AND 1999) t ORDER BY pos ASC LIMIT 10 MySQL said: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'range, count() FROM ( SELECT 1 AS pos, '0-999' AS range, COUNT(*) AS count FROM ' at line 1
          – Thomas Koipuram
          Nov 10 at 14:15


















          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%2f53239437%2ftranspose-the-results-of-a-mysql-query-that-outputs-ranges%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini