[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Help on a Select Query

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
cscg1976
Asked:
cscg1976
3 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
imitchieCommented:
that query looks.. very interesting. it can be greatly simplified. brb
0
 
imitchieCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Rajesh_mjCommented:

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
 
imitchieCommented:
yup mymistake forgot sum.. good one rajesh
0
 
imitchieCommented:
Wow, I completely recoded your query, and .... hmm... nice one
0
 
cscg1976Author Commented:
My mistake. I wanted to give you half points. Can it be undone ???
I apologize again.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now