在sqlite中将行数据显示为列

时间:2012-07-03 11:35:34

标签: sqlite

我在 SQLITE

中有一个具有以下结构的数据库表
Name        Count          Type
Roy          3              CA
Roy          2              BT
John         2              CA
John         1              BT

需要一个可以显示上述数据的查询(sqlite)

Name         count        CA       BT   
Roy            5           3        2
John           3           2        1

感谢

1 个答案:

答案 0 :(得分:0)

SQLite中没有透视,但您可以使用大量子查询:

SELECT DISTINCT name,
    (SELECT sum([count]) FROM test tst WHERE tst.name = test.name) as count,
    (SELECT sum([count]) FROM test tst WHERE tst.name = test.name AND type='CA') as CA,
    (SELECT sum([count]) FROM test tst WHERE tst.name = test.name AND type='BT') as BT
    FROM test

假设表名为“test”