简体   繁体   English

我如何将三个表的结果合并为一个结果

[英]How can i join three table result into one result

I have following three result from the same table with different where statement 我从同一张表中获得以下三个结果,不同的where语句

  SELECT count(*) as LONG_TERM_LEASE_2017_Apirl
  FROM drivenow.rent 
  where (rentalterm="Both" OR rentalterm="Long") AND (created_at BETWEEN '2017-04-01' AND '2017-04-30');

SELECT count(*) as LONG_TERM_LEASE_2017_May 
FROM drivenow.rent 
where (rentalterm="Both" OR rentalterm="Long") AND (created_at BETWEEN '2017-05-01' AND '2017-05-30');

SELECT count(*) as LONG_TERM_LEASE_2017_June
FROM drivenow.rent
WHERE (rentalterm="both" OR  rentalterm="Long" )AND (created_at BETWEEN '2017-06-01' AND '2017-06-30');

I want to show something like this in my SQL result side by side 我想在我的SQL结果中并排显示这样的内容

LONG_TERM_LEASE_2017_Apirl|LONG_TERM_LEASE_2017_May|LONG_TERM_LEASE_2017_June

How can i achieve this? 我怎样才能做到这一点? Sorry i am beginner in programming. 对不起,我是编程初学者。

SELECT 
COUNT(CASE WHEN (rentalterm="Both" OR rentalterm="Long") AND (created_at BETWEEN '2017-04-01' AND '2017-04-30') then 1 ELSE NULL END) as LONG_TERM_LEASE_2017_Apirl,
COUNT(CASE WHEN (rentalterm="Both" OR rentalterm="Long") AND (created_at BETWEEN '2017-05-01' AND '2017-05-30'); then 1 ELSE NULL END) as LONG_TERM_LEASE_2017_May ,
COUNT(CASE WHEN (rentalterm="both" OR  rentalterm="Long" )AND (created_at BETWEEN '2017-06-01' AND '2017-06-30') then 1 ELSE NULL END) as LONG_TERM_LEASE_2017_June
from drivenow.rent ;

You could cross join those three Selects, but in fact you need only a single Select using conditional aggregation : 您可以cross join这三个Select,但是实际上您只需要使用条件聚合来选择一个Select:

 SELECT
    count(case when created_at BETWEEN '2017-04-01' AND '2017-04-30' then 1 end) as LONG_TERM_LEASE_2017_Apirl,
    count(case when created_at BETWEEN '2017-05-01' AND '2017-05-30' then 1 end) as LONG_TERM_LEASE_2017_May,
    count(case when created_at BETWEEN '2017-06-01' AND '2017-06-30' then 1 end) as LONG_TERM_LEASE_2017_June
  FROM drivenow.rent 
  where (rentalterm="Both" OR rentalterm="Long") 
    AND (created_at BETWEEN '2017-04-01' AND '2017-06-30' );

The 1 returned by the case is just a dummy value for counting. 案例返回的1只是用于计数的虚拟值。 Instead of count you can also use a sum like this: 除了count您还可以使用如下所示的总和:

sum(case when created_at BETWEEN '2017-04-01' AND '2017-04-30' then 1 else 0 end)

Try like this: 尝试这样:

select
SUM(case when (rentalterm="Both" OR rentalterm="Long") AND (created_at BETWEEN '2017-04-01' AND '2017-04-30') then 1 else 0) as LONG_TERM_LEASE_2017_Apirl,
SUM(case when (rentalterm="Both" OR rentalterm="Long") AND (created_at BETWEEN '2017-05-01' AND '2017-05-30') then 1 else 0) as LONG_TERM_LEASE_2017_May,
SUM(case when (rentalterm="both" OR  rentalterm="Long" )AND (created_at BETWEEN '2017-06-01' AND '2017-06-30') then 1 else 0) as LONG_TERM_LEASE_2017_June
FROM drivenow.rent 
WHERE(rentalterm="Both" OR rentalterm="Long") AND (created_at BETWEEN '2017-04-01' AND '2017-06-30') then 1 else 0)

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

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