mysql - Can I sub query this or not? -


i've been trying query/subquery work stats have failed, started again scratch. can results want it's still in 2 separate queries. have little no experience sub queries after reasearching, heart tells me should able in 1 query. info in 1 table need use 2 separate group in order right data. i'm hoping can me head around or @ least point me in right direction... in advance

select month(bookadhistory)-1 monthnum,     count(distinct bookidhistory) totalbooks,     count(distinct case when bookddchistory = 300 bookidhistory end) breaches     bookhistory     join book on bookid = bookidhistory     bookid = 44     group month (bookadhistory) order monthnum;  select month(historycreated)-1 monthnum,     count(distinct case when bookddchistory between 1 , 99 bookidhistory end) delays,     count(distinct case when bookddchistory = 200 bookidhistory end) extns,     count(distinct case when bookddchistory = 100 bookidhistory end) lateclose     bookhistory     join book on bookid = bookidhistory     bookid = 44     group month (historycreated) order monthnum; 

there may ways simplify this, using have , sticking them in subquery(s) like:

select     *     (          select month(bookadhistory)-1 monthnum,             count(distinct bookidhistory) totalbooks,             count(distinct case when bookddchistory = 300 bookidhistory end) breaches         bookhistory             join book on bookid = bookidhistory         bookid = 44         group month (bookadhistory)          order monthnum     ) t1     inner join     (          select month(historycreated)-1 monthnum,             count(distinct case when bookddchistory between 1 , 99 bookidhistory end) delays,             count(distinct case when bookddchistory = 200 bookidhistory end) extns,             count(distinct case when bookddchistory = 100 bookidhistory end) lateclose         bookhistory             join book on bookid = bookidhistory         bookid = 44         group month (historycreated)          order monthnum;     ) t2 on         t1.monthnum = t2.monthnum 

Popular posts from this blog