选择值作为列

时间:2013-05-29 11:02:20

标签: sql postgresql pivot entity-attribute-value crosstab

如果我有一张包含以下内容的表:

game       powerup   used
memory     1         12
memory     2         10
bejeweled  2         88
bejeweled  3         54

... powerup的不同可能值的(数量)未知,有没有办法SELECT这样的

game       1_used  2_used  3_used
memory     12      10
bejeweled          88      54

我对纯SQL或PostgreSQL答案感兴趣。

注意:这是一个大学问题,我目前没有用例。回答它是为了您的享受或为了网站的利益,而不是我的个人利益:)

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT game
,MAX(CASE WHEN powerup = 1 THEN used ELSE NULL END) AS used1
,MAX(CASE WHEN powerup = 2 THEN used ELSE NULL END) AS used2
,MAX(CASE WHEN powerup = 3 THEN used ELSE NULL END) AS used3
FROM Table1
GROUP BY game;

输出:

╔═══════════╦════════╦═══════╦════════╗
║   GAME    ║ USED1  ║ USED2 ║ USED3  ║
╠═══════════╬════════╬═══════╬════════╣
║ bejeweled ║ (null) ║    88 ║ 54     ║
║ memory    ║ 12     ║    10 ║ (null) ║
╚═══════════╩════════╩═══════╩════════╝

See this SQLFiddle

答案 1 :(得分:0)

DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
CREATE TABLE meuk
        ( game varchar
        , powerup INTEGER NOT NULL
        , used INTEGER NOT NULL
        , PRIMARY KEY (game,powerup)
        );

INSERT INTO meuk(game, powerup, used) VALUES
( 'memory' , 1,12)
,( 'memory' , 2,10)
,( 'bejeweled' , 2,88)
,( 'bejeweled' , 3,54)
        ;

 -- using a self-join as
 -- a poor man's pivot.
WITH dom AS (
        SELECT DISTINCT game AS game
        FROM meuk
        )
SELECT m0.game AS game
        , m1.used AS used_1
        , m2.used AS used_2
        , m3.used AS used_3
FROM dom m0
LEFT JOIN meuk m1 ON m1.game = m0.game AND m1.powerup = 1
LEFT JOIN meuk m2 ON m2.game = m0.game AND m2.powerup = 2
LEFT JOIN meuk m3 ON m3.game = m0.game AND m3.powerup = 3
        ;

结果:

   game    | used_1 | used_2 | used_3 
-----------+--------+--------+--------
 bejeweled |        |     88 |     54
 memory    |     12 |     10 |       
(2 rows)

答案 2 :(得分:-1)

CREATE TABLE test_1
        ( game varchar(10)
        , powerup INTEGER NOT NULL
        , used INTEGER NOT NULL
        );

INSERT INTO test_1(game, powerup, used) 
SELECT 'memory' , 1,12 UNION ALL
SELECT 'memory' , 2,10 UNION ALL
SELECT  'bejeweled' , 2,88 UNION ALL
SELECT  'bejeweled' , 3,54 

select * from test_1

DECLARE @COLUMNS varchar(max) 
SELECT @COLUMNS = COALESCE(@COLUMNS+'],[' ,'') +  CAST(powerup as varchar)
FROM test_1
GROUP BY powerup

SET @COLUMNS = '[' + @COLUMNS + ']'

EXECUTE ('select * from test_1
pivot (SUM(used) for powerup IN ('+ @COLUMNS +')) as used_t')