sql - 创建具有多个字段值的交叉表

标签 sql postgresql

这是我当前表格的示例:

game_id |player_id|player_name       |event_id|event_desc        |count  |
--------|---------|------------------|--------|------------------|-------|
1       |        1|player1           |       3|Shot              |      1|
1       |        1|player1           |       5|Rebound           |      3|
1       |        1|player1           |       7|Foul              |      1|
1       |        1|player1           |      14|Assist            |      1|
1       |        1|player1           |      17|Subbed in         |      4|
1       |        1|player1           |      18|Subbed out        |      3|
1       |        1|player1           |      19|Drew a Foul       |      2|
1       |        1|player1           |      20|Free Throws Scored|      3|
1       |        1|player1           |      21|Free Throws Missed|      1|
1       |        2|player2           |       3|Shot              |      7|
1       |        2|player2           |       4|Miss              |     10|
1       |        2|player2           |       5|Rebound           |      2|
1       |        2|player2           |       7|Foul              |      1|
1       |        2|player2           |      14|Assist            |      1|
1       |        2|player2           |      17|Subbed in         |      4|
1       |        2|player2           |      18|Subbed out        |      4|
1       |        2|player2           |      19|Drew a Foul       |      2|

我需要基于此创建一个 View ,以便我将每个玩家的每场比赛的统计数据分组。不同的统计数据基于特定 event_ID 的计数。我有大约 20 个不同的 ID 需要映射。

game id | player_id | shot | Miss | Rebound |Foul | Assist | ...
1       |1          |1     |0     |3        |1    |1
1       |2          |7     |10    |2        |1    |1

我认为我必须为此使用交叉表功能,但我不确定如何为此准确地编写代码,我不是该领域的真正知识人。如果有人可以帮助我解决这个问题,我将不胜感激。

尝试了以下代码:

select
    game_id,
    player_id,
    player_name,
    sum(case when event_desc = 'Jump Ball' then wjxbfs1 else 0) as Jump_Ball,
    sum(case when event_desc = 'Shot' then wjxbfs1 else 0) as Shot,
    sum(case when event_desc = 'Miss' then wjxbfs1 else 0) as Miss,
    sum(case when event_desc = 'Rebound' then wjxbfs1 else 0) as Rebound,
    sum(case when event_desc = 'Assist' then wjxbfs1 else 0) as Assist,
    sum(case when event_desc = 'Block' then wjxbfs1 else 0) as Block,
    sum(case when event_desc = 'Steal' then wjxbfs1 else 0) as Steal,
    sum(case when event_desc = 'Turnover' then wjxbfs1 else 0) as Turnover,
    sum(case when event_desc = 'Foul' then wjxbfs1 else 0) as Foul,
    sum(case when event_desc = 'Free Throws Taken' then wjxbfs1 else 0) as FT_Taken,
    sum(case when event_desc = 'Free Throws Scored' then wjxbfs1 else 0) as FT_Scored,
    sum(case when event_desc = 'Free Throws MIssed' then wjxbfs1 else 0) as FT_Missed,
    sum(case when event_desc = 'Timeout' then wjxbfs1 else 0) as Timeout,
    sum(case when event_desc = 'Violation' then wjxbfs1 else 0) as Violation,
    sum(case when event_desc = 'Subbed in' then wjxbfs1 else 0) as Subbed_In,
    sum(case when event_desc = 'Subbed out' then wjxbfs1 else 0) as Subbed_Out,
    sum(case when event_desc = 'Drew a Foul' then wjxbfs1 else 0) as Drew_Foul,
    sum(case when event_desc = 'Ejection' then wjxbfs1 else 0) as Ejected   
from
    stats
group by
    game_id,
    player_id,
    player_name 

返回以下错误: SQL 错误 [42601]: 错误: ")"处或附近的语法错误位置: 119

这是通过用“end”替换“else 0”来解决的。

最佳答案

你可以尝试用case when

select gameid,palyerid, 
max(case when event_desc='Shot' then count end) as shot,
max(case when event_desc='Miss' then count end) as Miss,
max(case when event_desc='Rebound' then count end) as Rebound,
max(case when event_desc='Foul' then count end) as Foul,
max(case when event_desc='Assist' then count end) as Assist,
------
from table_name group by  gameid,palyerid

关于sql - 创建具有多个字段值的交叉表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55530815/

相关文章:

sql - 如何在 Postgres 中进行昵称搜索

mysql - 按新定义的参数对 SQL 进行分组

mysql - SQL-查询查找具有来自另一个表的最大链接列的特定行的名称

SQL - 根据最近日期更新记录

perl - 使用名称将参数绑定(bind)到 Perl 中的 postgres 查询

php - 从MySQL表中获取特定数据

sql - ORDER BY 更改了具有相同名称的列

postgresql - 同一张表中 COUNT 的 SUM

SQL:从 "nothing"中选择一个数字列表

sql - 找到双方都参与的聊天