sql - How to combine two query's results into one row? -
i have 2 queries return 1 result each i.e 1 number
select count(*) stockcounta table_a dept='aaa'
results
stockcounta 550
.
select count(*) stockcountb table_b dept='bbb'
results
stockcountb 450
i wish join 2 results 1 row record i.e
| stockcounta | stockcountb | 550 | 450
you can use:
select (select count(*) stockcounta table_a dept='aaa') stockcounta, (select count(*) stockcountb table_b dept='bbb') stockcountb
explanation: can select single value field in select statement, write like
select x.*, (select * table_y y) valuefromy table_x x
this work when sub query returns @ 1 row. 0 rows valuefromy return null
, more 1 row, query fail.
an additional feature of select
(in sql server) can select values without specifying table @ all, this:
select 3.14 moreorlesspi
you can combine both facts combine 2 counts single result, writing query looks like:
select (select query returns @ 1 row) result1, (select query returns @ 1 row) result2
Comments
Post a Comment