sql - How to Group by with Count and Join? -


i have 2 tables, a(id, name), b(id). want record group name, , count of a(id) , b(id)

i trying way

select left(od.number, 3)        terminal,         count(left(od.number, 2)) ordercount,         count(ot.orderid)    gff_bog_orderlocation.dbo.orderdetail od,         gff_bog_orderlocation.dbo.ordertable ot   ot.orderid in (select orderid                          gff_bog_orderlocation.dbo.orderdetail                         left(number, 3) in(select left(number, 3)                                                                                gff_bog_orderlocation.dbo.orderdetail                                                  group  left(number, 3)))  group  left(od.number, 3)  order  terminal  

but not getting properly.

based on question , not sql i'd you'd need this.

create table tablea (id int, name varchar(50)) create table tableb (id int) insert tablea values (1, 'us'), (2, 'us'), (3, 'canada'), (4, 'mexico'), (5, 'mexico') insert tableb values (1), (1), (1), (2), (2), (3), (3), (3), (4), (4), (4), (5), (5), (5), (5), (5)   select ta.name,        count(*) tableacount,        tablebcount   tablea ta         join (             select name,                    count(*) tablebcount               tablea ta                    left join tableb tb on ta.id = tb.id              group                        name          ) tb on ta.name = tb.name group ta.name,         tablebcount  drop table tablea drop table tableb  result:   name    tableacount tablebcount ----    ----------- ----------- canada  1           3 mexico  2           8      2           5 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -