简体   繁体   中英

MYSQL: How to join two tables using Inner join and then calculatin the total number from the second table for the following examples

I am stuck with the following requirement and I am finding it difficult to crack the query for it.

Consider a table customer with the following fields

id   signup_date  first_payment_date 
10    2015-03-20     null
11    2015-03-20     null
12    2015-03-20     null
13    2015-03-20     null
14    2015-05-23     null
15    2015-05-23     null

Consider another table transaction_history

id   product_name
10    vod trial
10    vod trial 
11    vod trial
12    vod trial
12    vod
13    vod trial
14    vod trial
15    vod trial
15    vod trial

I need to pick the id from customer table and look up in transaction_history table based on the signup_date and first_payment_date is null .

Now I need to check if this id is present in transaction_history and check if he has at least 1 entry with product_name = "vod trial" . If he has then he is a row in the result I want.

At the end I need to calculate the total number of id's from transaction_history who has at least one row where product_name="vod_trial" and this should be on a date basis mentioned in signup_date in customer table.

I wrote a query in the following manner:

SELECT 
    ts.guid,
    cs.signup_date,
    (SELECT 
        COUNT(ts2.guid)
     FROM
        transaction_history ts2
     WHERE
        cs.guid = ts2.guid
        AND ts2.product_name = "vod trial"
    HAVING COUNT(ts2.guid) = 1) AS count_ts_guid
FROM
    customer AS cs,
    transaction_history AS ts
WHERE
    cs.guid = ts.guid
    AND cs.first_payment_date IS NULL;

But in the above query I am not able to calculate the total count signup_datewise .

Would be great if someone could help me out.

Sample result:

date         new trials
2015-03-20      2
2015-05-23      1

I am not sure I fully understand. You want customers without first_payment_date that have a trial entry in the transaction table?

select *
from customer
where first_payment_date is null
and id in (select id from transaction_history where product_name = 'vod trial');

Okay, from your last comment it seems, you want customers that have no trial entry in the transaction table, too. And you want to display them with their trial transaction count. So:

select signup_date, 
  (
    select count(*) 
    from transaction_history th
    where th.product_name = 'vod trial'
    and th.id = c.id
  )
from customer c
where first_payment_date is null;

If you even want to group by date, then aggregate:

select signup_date, 
  sum((
    select count(*) 
    from transaction_history th
    where th.product_name = 'vod trial'
    and th.id = c.id
  ))
from customer c
where first_payment_date is null
group by signup_date;

Next try: Join all customers and transactions, such as to only get customers present in the transactions table. Then aggregate.

select c.signup_date, count(*) 
from customer c
join transaction_history th on th.id = c.id and th.product_name = 'vod trial'
where c.first_payment_date is null
group by c.signup_date;

Or do you want this:

select c.signup_date, count(case when th.product_name = 'vod trial' then 1 end) 
from customer c
join transaction_history th on th.id = c.id
where c.first_payment_date is null
group by c.signup_date;

I'd better make this a separate answer. You want to find customers that have only one entry in transaction_history and that entry must be 'vod trial'. So read the transaction table, group by customer id and count. Check your criteria with HAVING. Then join the found IDs with the customer table and group by date.

select c.signup_date, count(*)
from customer c
join
(
  select id
  from transaction_history
  group by id
  having count(*) = 1
  and min(product_name) = 'vod trial'
) t on t.id = c.id
group by c.signup_date;

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