0

我在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

谢谢

4

1 回答 1

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

假设表名“测试”

于 2012-07-07T20:06:07.493 回答