简体   繁体   中英

How can i join three table result into one result

I have following three result from the same table with different where statement

  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

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 :

 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. Instead of count you can also use a sum like this:

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)

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