Re: Help on query for report
- From: Lennart <erik.lennart.jonsson@xxxxxxxxx>
- Date: Mon, 09 Jul 2007 12:58:13 +0200
Lemune wrote:
Hi Lennart.
Thanks for your reply.
I have tried your suggestion, here my new query (I'm using mysql 5)
SELECT p.name,MONTHNAME(s.date) as month,sum(CASE WHEN d.quantity is
NULL THEN 0 ELSE d.quantity END ) AS sumTotal from products p left
outer join (sales s inner join sales_details d on s.id=d.sale_id) on
p.id=d.product_id WHERE S.date BETWEEN CAST('2007-01-01 00:00:00' as
Datetime) AND CAST('2007-07-30 23:59:59' as Date) group by p.id,month
Union
SELECT p.name, MONTHNAME( s.date ) AS MONTH , 0 AS sumTotal FROM
products p, sales s WHERE NOT EXISTS (SELECT p.name,
MONTHNAME( s.date ) AS MONTH , sum(CASE WHEN d.quantity IS NULL THEN 0
ELSE d.quantity END ) AS sumTotal FROM products p LEFT OUTER JOIN
( sales s INNER JOIN sales_details d ON s.id = d.sale_id ) ON p.id =
d.product_id WHERE S.date BETWEEN CAST( '2007-01-01 00:00:00' AS
Datetime ) AND CAST( '2007-07-30 23:59:59' AS Date ) GROUP BY p.id,
MONTH );
It still doesn't give me the 0 value for my question.
Where did i make mistake for my new query, because i understand that
WHERE NOT EXISTS will give me value 0 for condition where it give
nothing.
Sorry for any confusion I might have caused. You still need to generate the domain of all possible values. Either create a table and fill it with all possible values, or construct one on the fly. I dont know how to do this in mysql, but here are some examples I've seen in other dbms:
lateral(values ('January'),('February', etc ) ) month_names (x)
(select 'January' union Select 'February' union etc )) month_names (x)
If you create a permanent table, it is probably better to do it on date basis rather than on a month basis
.
- Follow-Ups:
- Re: Help on query for report
- From: Tonkuma
- Re: Help on query for report
- References:
- Help on query for report
- From: Lemune
- Re: Help on query for report
- From: Lennart
- Re: Help on query for report
- From: Lemune
- Help on query for report
- Prev by Date: Re: Help on query for report
- Next by Date: Re: Firebird, Microsoft Express, Postgre, or Virtuoso?
- Previous by thread: Re: Help on query for report
- Next by thread: Re: Help on query for report
- Index(es):
Relevant Pages
|
|