简体   繁体   English

SQL中将多个查询合并为一个查询

[英]Combining multiple queries into one query in SQL

I have an issue in sql as I am quite new to this.我在 sql 中有一个问题,因为我对此很陌生。

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: case表达式进行条件聚合:

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! 确实不需要WHERE子句,但可以加快处理速度!

You can use SUM like this: 您可以像这样使用SUM

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. 您可以尝试将所有查询与UNION ALL连接起来,这样就可以结束。

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年龄20_30 age31_40年龄31_40 age50_60年龄50_60
6 6 1 1 3 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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