有一种简单的方法可以使用一个SQLite查询来计算不同的匹配吗?

时间:2015-07-23 14:00:27

标签: sqlite

我有一个SQLite表,我可以做三个不同的查询,比如这个伪SQL ...

SELECT COUNT(*) FROM myTable WHERE day=today AND colA = "xxx";
SELECT COUNT(*) FROM myTable WHERE day=today AND colB = "yyy";
SELECT COUNT(*) FROM myTable WHERE day=today AND colC = "zzz";

我基本上想知道今天是否有任何匹配(如果是,哪一列)。有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

SELECT
  SUM(O.A) A,
  SUM(O.B) B,
  SUM(O.C) C
FROM (
  SELECT
    CASE WHEN T.colA = 'xxx' THEN 1 ELSE 0 END A,
    CASE WHEN T.colB = 'yyy' THEN 1 ELSE 0 END B,
    CASE WHEN T.colC = 'zzz' THEN 1 ELSE 0 END C
  FROM
    myTable T
  WHERE
    T.day = today
  ) O
;