Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Novice attempts to join or union failed. Need SQL to achieve results from two tables with common column. I want to have rows that contain unique vendor codes, with column 2 a count vendor codes from table zqm for date in June, with column 3 a count of vendor codes(VC) from table rr for each unique vendor code where owner is supplier and date in June.

Example sql desired result from below two tables:
VC     count VC       count RR
101	4             1
103     2             0

table zqm
VC     	date
101     June
101	June
101	June
101	June
103	June
103	June
103     July
Table rr
VC      owner      	date
101    supplier	        June
101    company	        June
103    company	        June
103    supplier         July
Posted
Updated 30-May-14 3:34am
v2

Here's another way of doing it:
SQL
with c as (
    select  vc,count(date) Count_VC
    from    zqm
    where   date = 'June'
    group by vc
    )
select  c.vc,c.Count_VC,sum(case rr.date when 'June' then 1 else 0 end) Count_RR
from    c
Left outer join rr on c.vc = rr.vc
where   rr.owner = 'supplier'
group by c.vc,c.Count_VC
 
Share this answer
 
SQL
select z.vc,
(select count(zqm.date)
from zqm where zqm.date = 'June' and zqm.vc=z.vc group by zqm.vc) as count_vc,
(CASE
  WHEN not exists (select * from rr where rr.date = 'June' and owner='supplier' and rr.vc=z.vc)
  THEN 0
  ELSE (select count(rr.date)
from rr where rr.date = 'June' and owner='supplier' and rr.vc=z.vc group by rr.vc)
  END)  as count_rr
from zqm z group by z.vc
 
Share this answer
 
Comments
RDBurmon 30-May-14 11:22am    
+5
Try this

SQL
SElect distinct * from maintbl INNER JOIN
(select *  from zqm where  date='June') AS zqm ON maintbl.VC=zqm.VC
INNER JOIN
(select * from rr WHERE owner ='supplier' and date='June') as rr ON rr.VC=zqm.VC
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900