[英]How to get a single output for two different statements using sql
Hi this is my output format: 嗨,这是我的输出格式:
--------------------------------------------------------------------------------
CLASS TOTAL NO OF STUDENTS STUDENTS PURCHASED REMAINING STUDENTS
1A 52 26 26
now i want the result in above format (how many students are belongs to Class '1A' and count of purchased students and not purchased students). 现在我想要以上格式的结果(属于“ 1A”类的学生人数以及购买的学生而不是购买的学生的数量)。
i use this query but i get only not purchase students result. 我使用此查询,但我只会得到不购买学生的结果。
select count(studentid)
from student_table
where studentid not exists (select studentid from 'purchase_table')
;
anybody help to solve this problem. 任何人都可以解决这个问题。
try this one: 试试这个:
select st.class class
, st.cnt_tl total
, coalesce(sp.cnt_customer,0) customers
, st.cnt_tl - coalesce(sp.cnt_customer,0) remaining
from (
select s1.class
, count(*) cnt_tl
from student_table s1
group by s1.class
) st
left join (
select s2.class
, count(*) cnt_customer
from student_table s2
inner join purchase_table p on ( p.studentid = s2.studentid )
group by s2.class
) sp
on ( sp.class = st.class )
;
live demo (sqlfiddle) here (oracle 11g r2) . 在此处 (oracle 11g r2)进行实时演示(sqlfiddle ) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.