5

I've got the following query:

SELECT GVA12.FECHA_EMIS, GVA12.COD_VENDED, sum(GVA12.IMPORTE)
FROM GVA12
WHERE Month(GVA12.FECHA_EMIS)=Month(curDate())
  AND Year(GVA12.FECHA_EMIS)=Year(curDate())
  AND GVA12.COD_VENDED="EX"
  AND GVA12.T_COMP="FAC"
GROUP BY GVA12.FECHA_EMIS

This is for a monthly graph. I've got two questions. One, how can I show all the dates of the months as zero (the ones that don't have any sales), and two, is there any way to make the values go adding up, so the last value is the total of all the values.

Edit: @Bluefeet with your query, I created the following,

SELECT Month(Days.DMY), Year(Days.DMY), GVA12.COD_VENDED, sum(GVA12.IMPORTE)
FROM Days
left join GVA12
    on Month(Days.DMY) = Month(GVA12.FECHA_EMIS)
    and Year(Days.DMY) = Year(GVA12.FECHA_EMIS)
WHERE Month(GVA12.FECHA_EMIS)=Month(curDate())
  AND Year(GVA12.FECHA_EMIS)=Year(curDate())
  AND GVA12.COD_VENDED="EX"
  AND GVA12.T_COMP="FAC"
GROUP BY Month(Days.DMY), Year(Days.DMY) WITH ROLLUP

I got the result attached (screenshot). enter image description here

It doesn't show all the days of the month as I wanted. What can I do?

Edit #3

It works now, but I want to add another filter. This filter is added here http://sqlfiddle.com/#!2/9d46c/1

CalvT
  • 3,123
  • 6
  • 37
  • 54

3 Answers3

5

To get the total you can use the GROUP BY WITH ROLLUP which should give you the Total of all dates:

SELECT GVA12.FECHA_EMIS, GVA12.COD_VENDED, sum(GVA12.IMPORTE)
FROM GVA12
WHERE Month(GVA12.FECHA_EMIS)=Month(curDate())
  AND Year(GVA12.FECHA_EMIS)=Year(curDate())
  AND GVA12.COD_VENDED="EX"
  AND GVA12.T_COMP="FAC"
GROUP BY GVA12.FECHA_EMIS WITH ROLLUP

As far as returning dates that do not exist, There are many questions on SO that answer that including the following. Sometimes it is easier in MySQL to create a table to join on:

generate days from date range

Get a list of dates between two dates

Edit #1: if you have a table with dates, then you could use something similar to this:

SELECT Month(d.yourDateCol), Year(d.yourDateCol), g.COD_VENDED, sum(g.IMPORTE)
FROM dates d
left join GVA12 g
    on Month(d.yourDateCol) = Month(GVA12.FECHA_EMIS)
    and Year(d.yourDateCol) = Year(GVA12.FECHA_EMIS)
WHERE Month(g.FECHA_EMIS)=Month(curDate())
  AND Year(g.FECHA_EMIS)=Year(curDate())
  AND g.COD_VENDED="EX"
  AND g.T_COMP="FAC"
GROUP BY Month(d.yourDateCol), Year(d.yourDateCol) WITH ROLLUP

Edit #2: Without seeing your full table structure or some sample data, here is a version of the query that is working:

select month(d.dmy) Month, 
  year(d.dmy) Year, 
  coalesce(sum(g.Importe), 0) TotalImporte
from dates d
left join GVA12 g
  on month(d.dmy) = month(g.FECHA_EMIS)
  and year(d.dmy) = year(g.FECHA_EMIS)
group by month(d.dmy), year(d.dmy)  WITH ROLLUP

See SQL Fiddle with Demo. This returns the month/year for each month/year in the dates table even if it does not exist in the GVA12 table,

Edit #3: If you want the running total, not just the final total, then you should be able to use the following:

SET @running_total := 0;

SELECT month(Days.DMY) Month, 
  Year(Days.DMY) Year, 
  Date(Days.DMY) Date,
  g.COD_VENDED,
  @running_total := @running_total + Coalesce(TotalImport, 0) as TotalImport
FROM Days
left join
(
  select FECHA_EMIS,
    COD_VENDED,
    sum(IMPORTE) TotalImport
  from GVA12
  group by Date(FECHA_EMIS), Year(FECHA_EMIS)
) g
  on date(Days.DMY) = date(g.FECHA_EMIS)
  and g.COD_VENDED='EX'
  and Month(g.FECHA_EMIS)=Month(curDate())
  and Year(g.FECHA_EMIS)=Year(curDate())
WHERE month(days.dmy)=Month(curDate())

See SQL Fiddle with Demo

The result is:

| MONTH | YEAR |                           DATE | COD_VENDED | TOTALIMPORT |
----------------------------------------------------------------------------
|     1 | 2013 | January, 01 2013 00:00:00+0000 |     (null) |           0 |
|     1 | 2013 | January, 02 2013 00:00:00+0000 |         EX |        1000 |
|     1 | 2013 | January, 03 2013 00:00:00+0000 |         EX |        4000 |
|     1 | 2013 | January, 04 2013 00:00:00+0000 |     (null) |        4000 |
|     1 | 2013 | January, 05 2013 00:00:00+0000 |     (null) |        4000 |
|     1 | 2013 | January, 06 2013 00:00:00+0000 |     (null) |        4000 |
|     1 | 2013 | January, 07 2013 00:00:00+0000 |     (null) |        4000 |
Community
  • 1
  • 1
Taryn
  • 242,637
  • 56
  • 362
  • 405
  • I'll ask the same to u: do u know how to make a table that shows all the days of the current month (so each month it auto updates)? – CalvT Jan 07 '13 at 17:08
  • Would it be possible to say make a table with all the dates for this year, then after the join has been made, select the ones of this month? Thanks – CalvT Jan 07 '13 at 17:11
  • Okay! I suppose that if I only put the month and date in the table it would still work? Thanks – CalvT Jan 07 '13 at 17:13
  • Sorry, I'm a bit thick! Would you mind editing your query for a table called Date and a column in the format YYYY-MM-DD called Date? Thanks – CalvT Jan 07 '13 at 17:27
  • Sorry, how do I do that? Thanks – CalvT Jan 07 '13 at 18:14
  • @CalvT your table `GVA12` does not contain any values for this month, so it will not match the `curdate()` -- see this fiddle -- http://sqlfiddle.com/#!2/c5395/4 – Taryn Jan 07 '13 at 18:35
  • GVA12 now has the right data. http://sqlfiddle.com/#!2/1db7e/5 but the importe is not cumilative, I would like it to be 1000,2000,3000 etc. Also, each day is in twice... – CalvT Jan 07 '13 at 18:44
  • Sorry to bother you again, but please look at this as your previous query didn't work. http://sqlfiddle.com/#!2/eb857/2 – CalvT Jan 08 '13 at 09:22
  • SQL Error: ResultSet is from UPDATE. No Data. (It works on SQL Fiddle, but not on my database) – CalvT Jan 08 '13 at 09:23
  • @CalvT What do you mean it does not work? Do you get an error message? Here is another version that also is working on sql fiddle -- http://sqlfiddle.com/#!2/eb857/7 – Taryn Jan 08 '13 at 10:14
  • Your query is correct but when I run it on the program it comes up with the error message above. It works in Navicat & Heidi SQL so I suppose it's a problem with the program. Thanks – CalvT Jan 08 '13 at 10:17
  • @CalvT Sorry I am not sure why it would not be working for you in those programs. If you can't get it to work, then maybe post another questions because I do not know how to fix that. – Taryn Jan 08 '13 at 10:19
  • I'm trying to filter FAC in column T_COMP from GVA12. When I do this it comes up with an error Thanks – CalvT Jan 08 '13 at 12:03
  • @CalvT without knowing your database, etc. I don't know why you would be getting errors. – Taryn Jan 08 '13 at 12:07
  • It says something about not finding T_COMP. Is that because I put g.T_COMP instead of GVA12.T_COMP? – CalvT Jan 08 '13 at 12:15
  • @CalvT if you place a column in the select list, it must be carried through from a subquery- see this fiddle -- http://sqlfiddle.com/#!2/9d46c/3 – Taryn Jan 08 '13 at 12:28
  • The problem is that now the total should be 3000 because I changed one of the rows to REC instead of FAC. – CalvT Jan 08 '13 at 12:31
  • @bluefeet [is this just useful or brilliant](http://meta.stackexchange.com/questions/148066/can-i-search-my-comments-using-some-keywords)? In a quest of getting a great feature :) – bonCodigo Jan 08 '13 at 16:56
  • @bluefeet I'm having problems with the last query you gave me. It's jumping records, as you will see here [SQLFiddle](http://sqlfiddle.com/#!2/253a8/4) Sorry I'm such a bother! – CalvT Jan 11 '13 at 19:04
  • 3
    @CalvT might I suggest that if bluefeet's original answer solved your problem and now your requirements are shifting, you start a new question? – Aaron Bertrand Jan 11 '13 at 19:07
0

a couple ideas:

one - you need some data values to compare against - so you could build a new table to hold all the dates - statically - then you do an outer join to that to make sure you get the zeroes.

two - i'm not sure about mysql - but in Oracle this is a LAG function. maybe that is a helpful pointer for further research.

Randy
  • 16,480
  • 1
  • 37
  • 55
  • Okay, do u know how to make a table that shows all the days of the current month (so each month it auto updates)? – CalvT Jan 07 '13 at 17:06
0

Have a look at this link, there is explained how to list all dates between 2 dates Get a list of dates between two dates

you can list using the following but I think there are better solution in the above link:

select a.Date 
from (
    select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.Date between '2012-12-01' and '2012-12-31';
Community
  • 1
  • 1
justMe
  • 2,200
  • 16
  • 20