2012-07-03 41 views
0

列我有一個DB表與下面結構SQLITE顯示行數據作爲源碼

Name  Count   Type 
Roy   3    CA 
Roy   2    BT 
John   2    CA 
John   1    BT 

需要一個查詢,可以顯示像下面的上述數據(源碼)

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

感謝

+0

是否提前知道類型的可能值? – Thilo

+0

是的,它是事先知道的 – Amit

回答

0

有SQLite中沒有旋轉,但你可以用很多subquerys的:

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 

假設表名「測試」