加入表格。聚集和巩固计数

时间:2018-02-27 17:21:39

标签: sql oracle

enter image description here我有两张桌子

表1 - 合格球员

Id_sequence         
121             
345             
765 

表2 - 与玩家相关的所有企业注意:只有6个game_ids 1a,1b,1c,2a,2b,2c

id_sequence player_id   game_id phone_no    
121     aaa     1a      111111111       
131     aaa     1b      111222111       
141     aaa     1a      111112222       
345     bbb     2a      222222222       
656     bbb     2c      222211122       
789     bbb     1c      222211122       
632     bbb     2b      222222222       
765     ccc     2b      333333333       
897     ddd     1a      444444444       
433     ddd     2c      555555444

预期产出:

select all id_sequence from Table 1 and join Table 2.   

然后选择与该player_id相关的所有其他数据,然后汇总并合并计数

player_id game id_1a game id_1b game id_1c game id_2a game id_2b game id_2c no_of _phones
aaa     2   1                                   3
bbb                       1           1                    1            1       2
ccc                                  1              1

目前,我正在创建一个表3,它存储从连接表1和表2获得的player_id。然后再次连接表3和2。 任何想法和想法都会有很大帮助。

2 个答案:

答案 0 :(得分:0)

PIVOT应该照顾它

const database = firebase.database().ref('/posts')
module.exports = database

获得NO_PHONES的附录

SQL> create table t1 ( id int);

Table created.

SQL>
SQL> insert into t1 values (121             );

1 row created.

SQL> insert into t1 values (345             );

1 row created.

SQL> insert into t1 values (765 );

1 row created.

SQL>
SQL>
SQL> create table t2 ( id int, player varchar2(10), game varchar2(10), phone int );

Table created.

SQL>
SQL> insert into t2 values (121     ,'aaa' ,    '1a'  ,    111111111);

1 row created.

SQL> insert into t2 values (131     ,'aaa' ,    '1b'  ,    111222111  );

1 row created.

SQL> insert into t2 values (141     ,'aaa' ,    '1a'  ,    111112222  );

1 row created.

SQL> insert into t2 values (345     ,'bbb' ,    '2a'  ,    222222222  );

1 row created.

SQL> insert into t2 values (656     ,'bbb' ,    '2c'  ,    222211122  );

1 row created.

SQL> insert into t2 values (789     ,'bbb' ,    '1c'  ,    222211122  );

1 row created.

SQL> insert into t2 values (632     ,'bbb' ,    '2b'  ,    222222222  );

1 row created.

SQL> insert into t2 values (765     ,'ccc' ,    '2b'  ,    333333333  );

1 row created.

SQL> insert into t2 values (897     ,'ddd' ,    '1a'  ,    444444444  );

1 row created.

SQL> insert into t2 values (433     ,'ddd' ,    '2c'  ,    555555444);

1 row created.

SQL>
SQL>
SQL> SELECT *
  2  FROM   (SELECT player, game, phone
  3          FROM   t2)
  4  PIVOT  (count(phone) AS cnt FOR (game) IN ('1a','1b','2a','2b','3a','3c'));

PLAYER       '1a'_CNT   '1b'_CNT   '2a'_CNT   '2b'_CNT   '3a'_CNT   '3c'_CNT
---------- ---------- ---------- ---------- ---------- ---------- ----------
aaa                 2          1          0          0          0          0
bbb                 0          0          1          1          0          0
ddd                 1          0          0          0          0          0
ccc                 0          0          0          1          0          0

4 rows selected.

答案 1 :(得分:0)

测试数据(另见:dbfiddle

SQL> select * from ae;
ID_SEQUENCE  PLAYER_ID  GAME_ID  PHONE_NO   
121          aaa        1a       111111111  
131          aaa        1b       111222111  
141          aaa        1a       111112222  
345          bbb        2a       222222222  
656          bbb        2c       222211122  
789          bbb        1c       222211122  
632          bbb        2b       222222222  
765          ccc        2b       333333333  
897          ddd        1a       444444444  
433          ddd        2c       555555444 

查询

select
  dt1.*
, dt2.no_of_phones
from (
  (
    select game_id, player_id
    from ae 
  ) pivot  
    (
      count( game_id ) for game_id in 
      (
        '1a' as "game id_1a"
      , '1b' as "game id_1b" 
      , '1c' as "game id_1c"
      , '2a' as "game id_2a"
      , '2b' as "game id_2b"
      , '2c' as "game id_2c"
     )
  )
) dt1 join ( 
  select 
    player_id
  , count( phone_no ) no_of_phones
  from ae
  group by player_id
) dt2
  on dt1.player_id = dt2.player_id
order by dt1.player_id
;

结果

PLAYER_ID  game id_1a  game id_1b  game id_1c  game id_2a  game id_2b  game id_2c  NO_OF_PHONES  
aaa        2           1           0           0           0           0           3             
bbb        0           0           1           1           1           1           4             
ccc        0           0           0           0           1           0           1             
ddd        1           0           0           0           0           1           2