同じテーブル上で参加し、グループのMySQL

ニコラス:

私は複数の日付、ブランド、市場では、次の表を持っています

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          |
...

私は何を取得しようとすることです

 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       |
...

私は次のことを試みたが、それは動作しませんでした

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`

ここで私が得るものですが、私は、日付、ブランド、市場を取得し、2行目を削除する方法を見つけ出すことはできません。


    |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       |
    ....

おかげで、

VBoka:

私は、これはあなたが探しているものであると信じて:

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

ここではデモがあります

結果:

ここでは、画像の説明を入力します。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=10379&siteId=1