Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two SQL queries on one table

Tags:

sql

sql-server

I have a tableA with different values:

 data
------
 10
 15
 20
 40
 40000
 50000
 60000

Also, I need to get some statistic information on that data (and I want to do it in one query), for example:

select count(data) from tableA where data < 100
union all
select count(data) from tableA  where data >= 100

As result, I receive

(No column name)
----------------
4
3

But I want to receive results in one row, like this:

Small | Big
---------
4     | 3 

How to do it? Is it possible?

like image 978
qehgt Avatar asked Sep 18 '12 05:09

qehgt


1 Answers

select count(case when data < 100 then 1 end) as Small,
       count(case when data >= 100 then 1 end) as Big
from TableA

With average it would look like this.

select avg(case when data < 100 then data end) as Small,
       avg(case when data >= 100 then data end) as Big
from TableA
like image 160
Mikael Eriksson Avatar answered Oct 29 '22 15:10

Mikael Eriksson