简体   繁体   中英

How to select values from same column using different where statements - SQL

I have the following code.

SELECT
 TO_CHAR(a.created_at, 'YYYY.MM') AS Datum,
 count(distinct(a.lead_demand_user_id)) AS Total_Leads
FROM
 leads a
WHERE
 date(a.created_at) BETWEEN '2017-06-01'
 AND 'TODAY' 
 AND a.lead_delivery_time IS NOT NULL
 AND a.lead_filler IN ('Android', 'iOS', 'MobileWeb', 'Web')
GROUP BY 1

The above query provides monthly leads.

Then I have another query, which provides only "Rent" Leads.

SELECT
 TO_CHAR(a.created_at, 'YYYY.MM') AS Datum,
 count(distinct(a.lead_demand_user_id)) AS Rent_Leads
FROM
 leads a
WHERE
 date(a.created_at) BETWEEN '2017-06-01'
 AND 'TODAY' 
 AND a.lead_delivery_time IS NOT NULL
 AND a.lead_filler IN ('Android', 'iOS', 'MobileWeb', 'Web')
 AND a.service IN ('Leads::Rent')
GROUP BY 1

Now I can create two separate queries, however I want both the values to be shown via a single query in the following format

Month -> Total Leads -> Rent Leads

I have tried using, join, union, subquery but not able to get the desired data.

Try this:

SELECT
   TO_CHAR(a.created_at, 'YYYY.MM') AS Datum,
   count(distinct(a.lead_demand_user_id)) AS Total_Leads,
   sum(case when a.service IN ('Leads::Rent') then 1 else 0 end) AS Rent_Leads
FROM leads a
WHERE date(a.created_at) BETWEEN '2017-06-01'
AND 'TODAY' 
AND a.lead_delivery_time IS NOT NULL
AND a.lead_filler IN ('Android', 'iOS', 'MobileWeb', 'Web')
GROUP BY 1

How about this one:

SELECT t1.Datum AS Month, t1.Total_Leads AS Total, t2.Rent_Leads AS Rent 
FROM (SELECT * FROM (SELECT TO_CHAR(a.created_at, 'YYYY.MM') AS Datum, count(distinct(a.lead_demand_user_id)) AS Total_Leads
                     FROM leads a
                     WHERE date(a.created_at) BETWEEN '2017-06-01'
                     AND 'TODAY' 
                     AND a.lead_delivery_time IS NOT NULL
                     AND a.lead_filler IN ('Android', 'iOS', 'MobileWeb', 'Web')) AS x
      GROUP BY 1         
     ) AS t1
INNER JOIN (SELECT * FROM (SELECT TO_CHAR(b.created_at, 'YYYY.MM') AS Datum, count(distinct(b.lead_demand_user_id)) AS Rent_Leads
                           FROM leads b
                           WHERE date(b.created_at) BETWEEN '2017-06-01'
                           AND 'TODAY' 
                           AND b.lead_delivery_time IS NOT NULL
                           AND b.lead_filler IN ('Android', 'iOS', 'MobileWeb', 'Web')
                           AND b.service IN ('Leads::Rent')) AS y
            GROUP BY 1
           ) AS t2
ON t1.Datum = t2.Datum;

An option I have used in this situation is to use union:

Use your first query as

SELECT
 TO_CHAR(a.created_at, 'YYYY.MM') AS Datum,
 count(distinct(a.lead_demand_user_id)) AS Total_Leads, 0 as Rent_leads...

union this with your second as

SELECT
 TO_CHAR(a.created_at, 'YYYY.MM') AS Datum,
 0 as Total_Leads,
 count(distinct(a.lead_demand_user_id)) AS Rent_Leads...

This works but means you effectively read the main table twice, an alternative is to use the fact that NULLS are not counted. I have assumed lead_demand_user_id is a number. In Oracle you can use Decode(a.service,'Leads::Rent',1,null) to create either a 1 or null.then count (distinct(a.lead_demand_user_id)*Decode(a.service,'Leads::Rent',1,null)) as Rent_Leads I'm not sure if Decode is available in other SQLs.

Hope this helps

Stephen L. Lee

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