簡體   English   中英

SQL中將多個查詢合並為一個查詢

[英]Combining multiple queries into one query in SQL

我在 sql 中有一個問題,因為我對此很陌生。

我有三個疑問

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;

有什么方法可以將這些查詢合並為一個查詢。

問候

薩梅拉

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

確實不需要WHERE子句,但可以加快處理速度!

您可以像這樣使用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

您可以嘗試將所有查詢與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;

結果將是:

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

首先我們有我們的虛擬數據

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;

由於您只需要一行數據,加上這些最終結果彼此不相關的事實,我們可以通過以下方式將它們與笛卡爾積集成:

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 ;

我們得到下一個結果

年齡20_30 年齡31_40 年齡50_60
6 1 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM