简体   繁体   中英

Combining multiple queries into one query in SQL

I have an issue in sql as I am quite new to this.

I have three queries

select count(*) as range1_50to60 from Customers where age between 50 and 60;

select count(*) as range2_30to40 from Customers where age between 30 and 40;

select count(*) as range3_20to30 from Customers where age between 20 and 30;

Is there any way by which we can combine these queries into one single query .

Regards

Sameera

Use case expressions to do conditional aggregation:

select count(case when age between 50 and 60 then 1 end) as range1_50to60,
       count(case when age between 30 and 40 then 1 end) as range2_30to40,
       count(case when age between 20 and 30 then 1 end) as range3_20to30
from Customers

where age between 20 and 60

The WHERE clause isn't really needed, but may speed things up!

You can use SUM like this:

SELECT SUM(IF(age between 50 and 60,1,0)) AS range1_50to60, 
       SUM(IF(age between 30 and 40,1,0)) AS range1_30to40, 
       SUM(IF(age between 20 and 30,1,0)) AS range1_20to30 
FROM Customers

You could try joining all the querys with UNION ALL, it would end like this.

select count(*) as result from Customers where age between 50 and 60
union all
select count(*) as result from Customers where age between 30 and 40;
union all
select count(*) as result from Customers where age between 20 and 30;

The results will be:

row[0] for range1_50to60
row[1] for range2_30to40 
row[2] for range3_20to30

First we have our dummy data

CREATE TABLE persons(
    person_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
    first_name VARCHAR2(50) NOT NULL,
    last_name VARCHAR2(50) NOT NULL,
    age NUMBER,
    PRIMARY KEY(person_id)
);
insert all 
    into persons(person_id,first_name,last_name,age) values(1,'jose','alvarez',32)
    into persons(person_id,first_name,last_name,age) values(2,'karla','romagnoli',24)
    into persons(person_id,first_name,last_name,age) values(3,'daniela','alcazar',24)
    into persons(person_id,first_name,last_name,age) values(4,'jaime','camilo',44)
    into persons(person_id,first_name,last_name,age) values(5,'jenifer','paola',22)
    into persons(person_id,first_name,last_name,age) values(6,'camila','puertas',55)
    into persons(person_id,first_name,last_name,age) values(7,'raul','duelas',30)
    into persons(person_id,first_name,last_name,age) values(8,'alejandra','bautizal',60)
    into persons(person_id,first_name,last_name,age) values(9,'domingo','cano',16)
    into persons(person_id,first_name,last_name,age) values(10,'felipe','vaca',25)
    into persons(person_id,first_name,last_name,age) values(11,'estefany','santes',28)
    into persons(person_id,first_name,last_name,age) values(12,'pamela','chu',55)
    into persons(person_id,first_name,last_name,age) values(13,'fernanda','zarate',67)
select 1 from dual;

Since you only want one row with the data, plus the fact that those final results are not correlated to each other, we can integrate them with a cartesian product in this way:

select 
q1.total as age20_30,
q2.total as age31_40,
q3.total as age50_60
from (
    select count(*) as total from persons p
    where p.age between 20 and 30
) q1, (
    select count(*) as total from persons p
    where p.age between 31 and 40
) q2, (
    select count(*) as total from persons p
    where p.age between 50 and 60
) q3 ;

And we get the next result

age20_30 age31_40 age50_60
6 1 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM