I am trying to collect statistics in a single SQL query to make the convenience of automatically sorting dates in a join. This is really only one table, but I want to calculate different cases of data.
The table I see looks something like this:
ID In Wanted
441 2011-03-14 0
439 2011-03-14 1
442 2011-03-14 0
428 2011-03-13 1
431 2011-03-13 1
425 2011-03-11 0
423 2011-03-11 1
420 2011-03-09 1
I approach the desired result with this query:
SELECT * FROM
(
(SELECT date(In) n, count(date(In)) cntw, null cntl FROM items i WHERE Wanted=1 group by date(In))
union all
(SELECT date(In) n, null cntw, count(date(In)) cntl FROM items i WHERE Wanted=0 group by date(In))
) Serie
Order by n DESC
But close is not close enough: D As a result, I get the following:
n cntw cntl
2011-03-14 null 2
2011-03-14 1 null
2011-03-13 2 null
2011-03-11 null 1
2011-03-11 1 null
2011-03-09 1 null
I want to "mix" the results in one line, by date:
n cntw cntl
2011-03-14 1 2
2011-03-13 2 null
2011-03-11 1 1
2011-03-09 1 null
As you can see, there is only one row for each date. In fact, the most ideal result would be to have even missing dates:
n cntw cntl
2011-03-14 1 2
2011-03-13 2 null
2011-03-12 null null
2011-03-11 1 1
2011-03-10 null null
2011-03-09 1 null
... but I think it is impossible.
Thank!
source
share