1

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

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 答案感兴趣。

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

4

3 回答 3

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) ║
╚═══════════╩════════╩═══════╩════════╝

看到这个 SQLFiddle

于 2013-05-29T11:13:15.803 回答
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)
于 2013-05-29T11:12:45.890 回答
-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')
于 2013-05-29T11:56:19.057 回答