같은 테이블에 가입하고 그룹 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`

여기에 내가 얻을하지만 난 날짜, 브랜드, 시장을 얻는 방법을 파악하고, 두 번째 줄을 제거 할 수없는 것입니다.


    |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=10378&siteId=1