这个查询可以缩短吗?源码

时间:2017-11-30 07:05:11

标签: sqlite

请记住,我使用sqlite,所以我不能使用像:

这样的东西
(SELECT Z1, Z2, Z3, DECODE(Z1, 'x', 1, 0) + DECODE(Z2, 'x', 1, 0) + DECODE(Z3, 'x', 1, 0) test FROM InfoP1) 

这是我的疑问:

 String sql = "SELECT  ID, Nume, Prenume,"
            + "InfoP1.Z1, InfoP1.Z2, InfoP1.Z3, "
            + "SUM(InfoP1.Z1 + InfoP1.Z2 + InfoP1.Z3) AS Total,  "            
            + "(SELECT(((CASE WHEN Z1 = 'x' then 1 else 0 END)"
            + "+ (CASE WHEN Z2 = 'x' then 1 else 0 END)"
            + "+ (CASE WHEN Z3 = 'x' then 1 else 0 END))*8) AS Test),"                                      
            + "FROM InfoAn "
            + "LEFT JOIN InfoP1 ON InfoAn.ID = InfoP1.rowid "
            + "GROUP BY ID";

1 个答案:

答案 0 :(得分:0)

在SQLite中,boolean表达式返回01,因此您只需删除解码:

SELECT Z1, Z2, Z3, (Z1 = 'x') + (Z2 = 'x') + (Z3 = 'x') AS test FROM InfoP1