Note that there are some explanatory texts on larger screens.

plurals
  1. POGet date even if it doesn't exist in table from SQL SELECT statement
    primarykey
    data
    text
    <p>I have a table that stores the amount of errors according to what alarm-id it is. The table looks something like this:</p> <pre><code>|----DATE----|---ALARM_ID---|---COUNTER---| | 2012-01-01 | 1 | 32 | | 2012-01-01 | 2 | 28 | | 2012-01-02 | 1 | 12 | | 2012-01-02 | 2 | 23 | | 2012-01-03 | 1 | 3 | | 2012-01-03 | 2 | 9 | | 2012-01-05 | 1 | 8 | | 2012-01-05 | 2 | 1 | | 2012-01-07 | 1 | 102 | | 2012-01-07 | 2 | 78 | </code></pre> <p>Notice the gap between date (2012-01-03 - 2012-01-05) and (2012-01-05 - 2012-01-07). On these dates there isn't any data because the system, that my program is monitoring, haven't reported any errors at that date. What I'm looking for is a SQL SELECT query that returns the total amount of errors on each date, for example:</p> <pre><code>|----DATE----|---COUNTER---| | 2012-01-01 | 60 | | 2012-01-02 | 35 | | 2012-01-03 | 12 | | 2012-01-04 | 0 | | 2012-01-05 | 9 | | 2012-01-06 | 0 | | 2012-01-07 | 180 | </code></pre> <p>I have a query that returns ID's even if they doesn't exist in the table, and if the ID doesn't exist, return the ID anyway with the COUNTER value 0. As such:</p> <pre><code> BEFORE AFTER |---ID---|---COUNTER---| |---ID---|---COUNTER---| | 1 | 2 | | 1 | 2 | | 2 | 6 | | 2 | 6 | | 3 | 1 | --&gt; | 3 | 1 | | 5 | 9 | | 4 | 0 | | 6 | 10 | | 5 | 9 | | 6 | 10 | | 7 | 0 | | 8 | 0 | </code></pre> <p>The query goes like this:</p> <pre><code>select t.num as ID, coalesce(yt.COUNTER, 0) from all_stats yt right join ( select t1.num + t2.num * 10 + t3.num * 100 + t4.num * 1000 as num from ( select 1 as num union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0 ) t1 cross join ( select 1 as num union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0 ) t2 cross join ( select 1 as num union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0 ) t3 cross join ( select 1 as num union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 0 ) t4 ) t on yt.ID = t.num where (t.num between (select min(ID) from all_stats) and (select max(ID) from all_stats)) order by ID </code></pre> <p>I can't figure out how I can change this query when it's regarding dates. Can someone please help me on this issue?</p> <p>I'm using MySQL</p> <p>Thanks in advance, Steve-O</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload