MySQL Join and group by on same table

nicolas :

I have the following table with multiple dates, brands and markets

Date       | brand       | Market   |  CONV   | PgmGRPs     | 
-----------|-------------|----------|---------|-------------|
2020-01-01 | ABC         | MTL      | conv.   | 80          | 
2020-01-01 | ABC         | MTL      | Spec.   | 20          |
2020-01-01 | ABC         | TO       | conv.   | 70          |
2020-01-01 | ABC         | TO       | Spec.   | 40          |
2020-01-02 | DEF         | TO       | conv.   | 80          |
2020-01-02 | DEF         | TO       | Spec.   | 20          |
...

What I try to get is

 Date      |brand | Market |Conv. PgmGRPs |Spec. PgmGRPs |Total PgmGRPs |Conv. Ratio | Spec. Ratio|
-----------|------|--------|--------------|--------------|--------------|------------|------------|
2020-01-01 | ABC  | MTL    | 80           | 20           | 100          | 0.80       | 0.20       |
2020-01-01 | ABC  | TO     | 70           | 40           | 110          | 0.64       | 0.36       |
2020-01-02 | DEF  | TO     | 80           | 20           | 100          | 0.80       | 0.20       |
...

I tried the following but it didn't work

SELECT
a.`Brand`,
a.`PgmGRPs` as 'Conv. PgmGRPs',
b.`PgmGRPs` as 'Spec. PgmGRPs',
a.`PgmGRPs` + b.`PgmGRPs` AS 'Total PgmGRPs',
ROUND(a.`PgmGRPs`/ (a.`PgmGRPs` + b.`PgmGRPs`),2) as 'Conv. Ratio',
ROUND(b.`PgmGRPs`/ (a.`PgmGRPs` + b.`PgmGRPs`),2) as 'Spec. Ratio'

FROM 
(SELECT
`Brand`,
IF ( `CONV` = 'Conv.', SUM(`PgmGRPs`), 0)
 AS 'PgmGRPs'
FROM transform_data_1
GROUP BY `Brand`, `CONV`
) a

LEFT JOIN 
(SELECT
`Brand`,
`CONV`,
SUM(`PgmGRPs`) as 'PgmGRPs'
FROM transform_data_1
WHERE `CONV` = 'Spec.'
GROUP BY `Brand`, `CONV`
) b
ON a.`Brand` = b.`Brand`

Here is what I get but I can't figure out how to get the date, brand, market and remove the second line.


    |brand | Market |Conv. PgmGRPs |Spec. PgmGRPs |Total PgmGRPs |Conv. Ratio | Spec. Ratio|
    |------|--------|--------------|--------------|--------------|------------|------------|
    | ABC  | MTL    | 80           | 20           | 100          | 0.80       | 0.20       |
    | ABC  | MTL    | 0            | 20           | 20           | 0          | 1.00       |
    ....

Thanks,

VBoka :

I believe this is what you are looking for:

select tab.`Date`
       , tab.brand
       , tab.market
       , Conv_PgmGRPs
       , Spec_PgmGRPs 
       , Total_PgmGRPs
       , ROUND(Conv_PgmGRPs / (Conv_PgmGRPs + Spec_PgmGRPs),2) as 'Conv. Ratio'
       , ROUND(Spec_PgmGRPs / (Conv_PgmGRPs + Spec_PgmGRPs),2) as 'Spec. Ratio'
from (select `Date`
       , brand
       , Market
       , (select sum(PgmGRPs)  
          from transform_data_1 t1 
          where t.`Date` = t1.`Date`
          and  t.brand = t1.brand
          and t.Market = t1.Market
          and t1.CONV = 'conv.') Conv_PgmGRPs
       , (select sum(PgmGRPs)  
          from transform_data_1 t1 
          where t.`Date` = t1.`Date`
          and  t.brand = t1.brand
          and t.Market = t1.Market
          and t1.CONV = 'Spec.') Spec_PgmGRPs
       , sum(PgmGRPs) Total_PgmGRPs
from transform_data_1 t
group by `Date`
       , brand
       , Market) tab

Here is a demo

The result:

enter image description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=10216&siteId=1