Solved

Help on a Select Query

Posted on 2007-11-22
8
255 Views
Last Modified: 2013-12-07
I have the following select Query

select a.Date,c.categoryid,count(*),k.countFemale,m.countMale,sum(a.weight), (sum(a.weight)/16*100)
from animals_arrived a,categories c,items i,(select ai.Date,ci.categoryid,ii.itemid,count(*) as countFemale
from animals_arrived ai,categories ci,items ii
where ai.supplierid = 1 and ci.categoryid= 1 and ci.categoryid = ii.categoryid
and ii.itemid = ai.itemid and ai.itemid = 1
group by ai.Date,ci.categoryid,ii.itemid) k,
(select ai.Date,ci.categoryid,ii.itemid,count(*) as countMale
from animals_arrived ai,categories ci,items ii
where ai.supplierid = 1 and ci.categoryid= 1 and ci.categoryid = ii.categoryid
and ii.itemid = ai.itemid and ai.itemid = 2
group by ai.Date,ci.categoryid,ii.itemid) m
where a.supplierid = 1 and c.categoryid= 1 and c.categoryid = i.categoryid and i.itemid = a.itemid
and c.categoryid=k.categoryid and i.itemid = k.itemid and a.Date = k.date
and c.categoryid=m.categoryid and i.itemid = m.itemid and a.Date = m.date
group by a.Date,c.categoryid,k.countFemale,m.countMale


Now if the "m" table or the "k" table returns nothing then nothing comes out from the query. I don't want that. What i want if no row that matches with the criteria comes out then set countFemale or countMale as ZERO.
Can you please help me ?

 
0
Comment
Question by:cscg1976
8 Comments
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
Comment Utility
you will have to LEFT JOIN the "m" and "k" tables.

left me try:

select a.Date,c.categoryid,count(*),k.countFemale,m.countMale,sum(a.weight), (sum(a.weight)/16*100)
from animals_arrived a
inner join items i
 on i.itemid = a.itemid
inner join categories c
 on c.categoryid = i.categoryid 
and c.categoryid= 1 
left join (select ai.Date,ci.categoryid,ii.itemid,count(*) as countFemale
from animals_arrived ai,categories ci,items ii
where ai.supplierid = 1 and ci.categoryid= 1 and ci.categoryid = ii.categoryid 
and ii.itemid = ai.itemid and ai.itemid = 1 
group by ai.Date,ci.categoryid,ii.itemid) k
  on i.itemid = k.itemid 
 and a.Date = k.date 
 and c.categoryid=k.categoryid 
left join (select ai.Date,ci.categoryid,ii.itemid,count(*) as countMale
from animals_arrived ai,categories ci,items ii
where ai.supplierid = 1 and ci.categoryid= 1 and ci.categoryid = ii.categoryid 
and ii.itemid = ai.itemid and ai.itemid = 2 
group by ai.Date,ci.categoryid,ii.itemid) m
  on c.categoryid=m.categoryid 
 and i.itemid = m.itemid 
 and a.Date = m.date 
where a.supplierid = 1 
group by a.Date,c.categoryid,k.countFemale,m.countMale

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
Comment Utility
that query looks.. very interesting. it can be greatly simplified. brb
0
 
LVL 25

Accepted Solution

by:
imitchie earned 200 total points
Comment Utility
run this query against the same database, and see if you get the same result as angel's
SELECT   a.DATE,

         c.categoryid,

         COUNT(* ),

         CASE i.item_id WHEN 1 THEN 1 ELSE 0 END AS countfemale,

         CASE i.item_id WHEN 2 THEN 1 ELSE 0 END AS countmale,

         SUM(a.weight),

         (SUM(a.weight) / 16 * 100)

FROM     animals_arrived a

INNER JOIN items i on i.itemid = a.itemid

INNER JOIN categories c on c.categoryid = i.categoryid and c.categoryid = 1

WHERE    a.supplierid = 1

GROUP BY a.DATE,c.categoryid

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Assisted Solution

by:Rajesh_mj
Rajesh_mj earned 200 total points
Comment Utility

Based on imitchie code,

SELECT   a.DATE,
         c.categoryid,
         COUNT(* ),
         Sum(CASE i.item_id WHEN 1 THEN 1 ELSE 0 END) AS countfemale,
         Sum(CASE i.item_id WHEN 2 THEN 1 ELSE 0 END) AS countmale,
         SUM(a.weight),
         (SUM(a.weight) / 16 * 100)
FROM     animals_arrived a
INNER JOIN items i on i.itemid = a.itemid
INNER JOIN categories c on c.categoryid = i.categoryid and c.categoryid = 1
WHERE    a.supplierid = 1
GROUP BY a.DATE,c.categoryid
0
 
LVL 25

Expert Comment

by:imitchie
Comment Utility
yup mymistake forgot sum.. good one rajesh
0
 
LVL 25

Expert Comment

by:imitchie
Comment Utility
Wow, I completely recoded your query, and .... hmm... nice one
0
 

Author Comment

by:cscg1976
Comment Utility
My mistake. I wanted to give you half points. Can it be undone ???
I apologize again.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now