1

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

4
  • 1
    You can use UNION to combine these 3 queries results into one table, if that is what you are asking for. Commented Feb 5, 2018 at 15:52
  • Possible duplicate of merging two SELECT queries Commented Feb 5, 2018 at 15:52
  • Possible duplicate of Count with if condition in mysql query Commented Feb 5, 2018 at 15:57
  • You haven't said how you wish your result to appear. 1 row 3 columns or 3 rows 1 column. Commented Feb 5, 2018 at 16:12

4 Answers 4

4

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.