Unix Technical Forum

Help on query for report

This is a discussion on Help on query for report within the MySQL forums, part of the Database Server Software category; --> Hello all. I have this difficulty with my query for my report. I want to create report that use ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:11 AM
Lemune
 
Posts: n/a
Default Help on query for report

Hello all.
I have this difficulty with my query for my report.
I want to create report that use bar chart, for the data I'm using
query to get it.
This is my query :
SELECT p.name,MONTHNAME(s.date) as month,if(sum(d.quantity) IS Null,
0,sum(d.quantity)) as sumQuantity from products p inner 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 MONTHNAME(s.date),p.id;

The problem is when there is no record on that month for the product i
want it to show 0 value, so the chart will show 0 to, not nothing.
Any clue or help?
Thanks.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:11 AM
J.O. Aho
 
Posts: n/a
Default Re: Help on query for report

Lemune wrote:
> Hello all.
> I have this difficulty with my query for my report.
> I want to create report that use bar chart, for the data I'm using
> query to get it.
> This is my query :
> SELECT p.name,MONTHNAME(s.date) as month,if(sum(d.quantity) IS Null,
> 0,sum(d.quantity)) as sumQuantity from products p inner 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 MONTHNAME(s.date),p.id;
>
> The problem is when there is no record on that month for the product i
> want it to show 0 value, so the chart will show 0 to, not nothing.
> Any clue or help?
> Thanks.
>


Think you need to make a function/procedure that returns this value for you,
or you do the checking in the script language that you use

simple php example

if(mysql_num_rows()) {
while($row=mysql_fetch_array($res)) {
echo $row['name'] ." ";
echo $row['month'] ." ";
echo $row['sumQuantity'] ."\n";
}
} else {
echo "0";
}

--

//Aho
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:11 AM
Lemune
 
Posts: n/a
Default Re: Help on query for report

Thank you for your reply Aho.
In simple, what i'm trying to get with my query is the query will show
record from month 1-7 (Jan,Feb, and so on) for each product, include
the month that the product didn't have record on the sales_detail
record on the month from sales. In other word if i have 5 product, i
will have 35 record.

In your example, it will check whether it has row or not, if not then
give value 0.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:11 AM
Adam Englander
 
Posts: n/a
Default Re: Help on query for report

Lemune wrote:
> Thank you for your reply Aho.
> In simple, what i'm trying to get with my query is the query will show
> record from month 1-7 (Jan,Feb, and so on) for each product, include
> the month that the product didn't have record on the sales_detail
> record on the month from sales. In other word if i have 5 product, i
> will have 35 record.
>
> In your example, it will check whether it has row or not, if not then
> give value 0.
>
>

I think the problem lies with your inner joins. It will likely not show
products without sales. An easy way to check is to remove the summary
calculations and see if your query returns the product data needed to
make the query.

You could also try using a nested query, they tend to work well in
reporting and my give you better performance. It always depends on the
table structures and sizes.

Adam
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 11:11 AM
Lemune
 
Posts: n/a
Default Re: Help on query for report

Hi, Adam.
Thanks for your reply.

I tried to use outer join like your suggestion. This is my query:
( remove the sales, i tried just products and sales_details)
SELECT p.name, sum(CASE WHEN d.quantity is NULL THEN 0 ELSE d.quantity
END ) AS sumTotal FROM products p Left OUTER JOIN sales_details d ON
p.id = d.product_id group by p.id;

and this query show all products.

Then i use this query and make grouping by date(month) with this query

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 right outer 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,MONTHNAME(s.date);

In this query, it still not show the product that doesn't have record
in the date range on sales_detail
and one more thing, it is also doesn't show the month where there is
no record in sales on the date range.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 11:11 AM
Captain Paralytic
 
Posts: n/a
Default Re: Help on query for report

On 9 Jul, 07:41, Lemune <alfredosilito...@gmail.com> wrote:
> Hi, Adam.
> Thanks for your reply.
>
> I tried to use outer join like your suggestion. This is my query:
> ( remove the sales, i tried just products and sales_details)
> SELECT p.name, sum(CASE WHEN d.quantity is NULL THEN 0 ELSE d.quantity
> END ) AS sumTotal FROM products p Left OUTER JOIN sales_details d ON
> p.id = d.product_id group by p.id;
>
> and this query show all products.
>
> Then i use this query and make grouping by date(month) with this query
>
> 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 right outer 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,MONTHNAME(s.date);
>
> In this query, it still not show the product that doesn't have record
> in the date range on sales_detail
> and one more thing, it is also doesn't show the month where there is
> no record in sales on the date range.


You need to create a calendar table and have that as the main table,
using LEFT JOINS against the other tables. Then you get an entry for
each date in the calendar table.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 11:11 AM
Lemune
 
Posts: n/a
Default Re: Help on query for report

Thanks Captain Paralytic, for your suggestion.
I'm for the meantime will use some function that will create and array
that suit my need, where the array will contain the query result, and
then my report datasource will use that array, untill I found another
solution that work.

Thanks all.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:43 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com