简体   繁体   English

根据来自另一个表的组比例在表中创建组

[英]Create groups in a table based on group proportions from another table

Context: 内容:

I have two tables. 我有两张桌子。 TABLE A has data with example format (with 12 ordered groups AL, A = highest, L = lowest): 表A具有示例格式的数据(具有12个有序组AL,A =最高,L =最低):

ID   | BAND
---- | ---- 
1    | A    
2    | B  
3    | A  
4    | C  
5    | D  
6    | F 
7    | D  
8    | H 
...

TABLE B has data with example format: 表B具有示例格式的数据:

ID   | SCORE
---- | ---- 
1    | 0.12    
2    | 0.37  
3    | 0.21  
4    | 0.55  
5    | 0.01  
6    | 0.90 
7    | 0.10  
8    | 0.71    
...

I have calculated the proportional sizes of each group in TABLE A using: 我使用以下方法计算了表A中每组的比例大小:

 CREATE TABLE table_a_group_pct AS
 SELECT band
 , count(*) * 100.0 / sum(count(*)) over() AS pct 
 FROM table_a 
 GROUP BY band;

With output: 输出:

BAND | PCT
---- | ----  
A    | 12  
B    | 15  
C    | 11  
D    | 9 
E    | 10  
F    | 8  
G    | 11  
H    | 10  
I    | 6
J    | 4
K    | 3
L    | 1

I wish to create 12 ordered (by score) groups for TABLE B with the same proportional sizes as the groups in TABLE A. 我希望为表B创建12个有序(按分数)的组,并与表A中的组具有相同的比例大小。

Eg 12% of rows in TABLE A have group = A, then the top 12% rows (based on score) would be given group = A and so on.... 例如,表A中12%的行具有group = A,则基于得分的前12%行将被赋予group = A,依此类推。

I think I can solve the problem by using the NTILE(100) function to find the % position of each score and then use a CASE WHEN to create manual groups based on the cumulative % of each group in table A. (ie if Band A has the top 12% of IDs then I find the 88th percentile in TABLE B and do: 我想我可以通过使用NTILE(100)函数找到每个分数的%位置,然后使用CASE WHEN根据表A中每个组的累积百分比来创建手动组来解决该问题。拥有ID的前12%,然后在表B中找到第88个百分位数,然后执行以下操作:

CASE WHEN score_pct > 88 then 'A' 
     WHEN score_pct BETWEEN 88 and 73 then 'B' ...
END AS group`

However I'm trying to understand if there is smarter way to tackle this problem. 但是,我试图了解是否有更聪明的方法来解决此问题。

Other information: TABLE A & TABLE B are not the same size and don't have the exact same ID's, I'm just trying to create similarly proportioned groups. 其他信息:表A和表B的大小不同,并且没有完全相同的ID,我只是在尝试创建类似比例的组。

My expected output is something like this: 我的预期输出是这样的:

ID   | SCORE | BAND
---- | ----  | ----
1    | 0.12  | K/11
2    | 0.37  | G/7
3    | 0.21  | H/8
4    | 0.55  | E/5
5    | 0.01  | L/12
6    | 0.90  | A/1
7    | 0.10  | K/11
8    | 0.71  | B/2  

[Edited my question to add clarity] [编辑了我的问题以提高清晰度]

This can be achieved by using the cume_dist analytic function along with some funky joining (in pre-12c) like so: 这可以通过使用cume_dist分析函数以及一些时髦的连接(在12c之前)来实现,如下所示:

(NB I have amended the data in table_a so that it contains the first 8 grades; this doesn't match with your example data so don't be surprised when my output doesn't match yours.) (注意,我已经修改了table_a中的数据,使其包含前8个等级;这与您的示例数据不匹配,因此当我的输出与您的输出不匹配时,请不要感到惊讶。)

WITH table_a AS (SELECT 1 ID, 'A' band FROM dual UNION ALL
                 SELECT 2 ID, 'B' band FROM dual UNION ALL
                 SELECT 3 ID, 'A' band FROM dual UNION ALL
                 SELECT 4 ID, 'C' band FROM dual UNION ALL
                 SELECT 5 ID, 'D' band FROM dual UNION ALL
                 SELECT 6 ID, 'E' band FROM dual UNION ALL
                 SELECT 7 ID, 'D' band FROM dual UNION ALL
                 SELECT 8 ID, 'F' band FROM dual),
     table_b AS (SELECT 1 ID, 0.12 score FROM dual UNION ALL
                 SELECT 2 ID, 0.37 score FROM dual UNION ALL
                 SELECT 3 ID, 0.21 score FROM dual UNION ALL
                 SELECT 4 ID, 0.55 score FROM dual UNION ALL
                 SELECT 5 ID, 0.01 score FROM dual UNION ALL
                 SELECT 6 ID, 0.90 score FROM dual UNION ALL
                 SELECT 7 ID, 0.10 score FROM dual UNION ALL
                 SELECT 8 ID, 0.71 score FROM dual),
-- end of data set-up, see the rest of the query below:
        a_pc AS (SELECT DISTINCT band,
                        cume_dist() OVER (ORDER BY band) pc_cume_dist
                 FROM   table_a),
        b_pc AS (SELECT id,
                        score,
                        cume_dist() OVER (ORDER BY score DESC) pc_cume_dist
                 FROM   table_b)
SELECT b_pc.id,
       b_pc.score,
       b_pc.pc_cume_dist,
       min(a_pc.band) band
FROM   b_pc
       INNER JOIN a_pc ON (a_pc.band = CASE WHEN b_pc.pc_cume_dist <= a_pc.pc_cume_dist AND a_pc.band = 'A' THEN 'A'
                                            WHEN b_pc.pc_cume_dist <= a_pc.pc_cume_dist AND a_pc.band = 'B' THEN 'B'
                                            WHEN b_pc.pc_cume_dist <= a_pc.pc_cume_dist AND a_pc.band = 'C' THEN 'C'
                                            WHEN b_pc.pc_cume_dist <= a_pc.pc_cume_dist AND a_pc.band = 'D' THEN 'D'
                                            WHEN b_pc.pc_cume_dist <= a_pc.pc_cume_dist AND a_pc.band = 'E' THEN 'E'
                                            WHEN b_pc.pc_cume_dist <= a_pc.pc_cume_dist AND a_pc.band = 'F' THEN 'F'
                                       END)
GROUP BY b_pc.id, b_pc.score, b_pc.pc_cume_dist
ORDER BY b_pc.score DESC;

        ID      SCORE PC_CUME_DIST BAND
---------- ---------- ------------ ----
         6        0.9        0.125 A
         8       0.71         0.25 A
         4       0.55        0.375 B
         2       0.37          0.5 C
         3       0.21        0.625 D
         1       0.12         0.75 D
         7        0.1        0.875 E
         5       0.01            1 F

Or, in 12c you can use the LATERAL join, like so: 或者,在12c中,您可以使用LATERAL连接,如下所示:

WITH table_a AS (SELECT 1 ID, 'A' band FROM dual UNION ALL 
                 SELECT 2 ID, 'B' band FROM dual UNION ALL 
                 SELECT 3 ID, 'A' band FROM dual UNION ALL 
                 SELECT 4 ID, 'C' band FROM dual UNION ALL 
                 SELECT 5 ID, 'D' band FROM dual UNION ALL 
                 SELECT 6 ID, 'E' band FROM dual UNION ALL 
                 SELECT 7 ID, 'D' band FROM dual UNION ALL 
                 SELECT 8 ID, 'F' band FROM dual), 
     table_b AS (SELECT 1 ID, 0.12 score FROM dual UNION ALL 
                 SELECT 2 ID, 0.37 score FROM dual UNION ALL 
                 SELECT 3 ID, 0.21 score FROM dual UNION ALL 
                 SELECT 4 ID, 0.55 score FROM dual UNION ALL 
                 SELECT 5 ID, 0.01 score FROM dual UNION ALL 
                 SELECT 6 ID, 0.90 score FROM dual UNION ALL 
                 SELECT 7 ID, 0.10 score FROM dual UNION ALL 
                 SELECT 8 ID, 0.71 score FROM dual), 
        a_pc AS (SELECT DISTINCT band, 
                        cume_dist() OVER (ORDER BY band) pc_cume_dist 
                 FROM   table_a), 
        b_pc AS (SELECT id, 
                        score, 
                        cume_dist() OVER (ORDER BY score DESC) pc_cume_dist 
                 FROM   table_b) 
SELECT b_pc.id, 
       b_pc.score, 
       b_pc.pc_cume_dist, 
       a_pc2.band 
FROM   b_pc, 
       lateral (SELECT MIN(band) band 
                FROM   a_pc 
                WHERE  a_pc.pc_cume_dist >= b_pc.pc_cume_dist) a_pc2 
order by b_pc.score desc

        ID      SCORE PC_CUME_DIST BAND
---------- ---------- ------------ ----
         6        0.9        0.125 A
         8       0.71         0.25 A
         4       0.55        0.375 B
         2       0.37          0.5 C
         3       0.21        0.625 D
         1       0.12         0.75 D
         7        0.1        0.875 E
         5       0.01            1 F

Here's an example of it running on Oracle's LiveSQL (which is at version 12.2) . 这是在Oracle LiveSQL(版本12.2)上运行的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM