WITH t AS (
SELECT colA, min(colB) AS colB, max(colC) AS colC
FROM myTable
WHERE colA IN ('121', '122','123','124','125','126','127','128','129')
GROUP BY colA
)
SELECT t.*, c.colC_count
FROM t
JOIN (
SELECT colC, count(*) AS colC_count
FROM t
GROUP BY colC
) c ON c.colC = t.colC
說明:
第一子查詢(內WITH
)得到想要的結果,但沒有數列。第二個子查詢(在JOIN
之內)計算每個colC
值重複的所需結果,並將此計數返回到最終結果。
非常有幫助WITH
子句作爲第一個子查詢的結果在兩個地方使用。更多信息:https://www.sqlite.org/lang_with.html
查詢SQLite的3.8.3版本之前:
SELECT t.*, c.colC_count
FROM (
SELECT colA, min(colB) AS colB, max(colC) AS colC
FROM myTable
WHERE colA IN ('121', '122','123','124','125','126','127','128','129')
GROUP BY colA
) t
JOIN (
SELECT colC, count(*) AS colC_count
FROM (
SELECT max(colC) AS colC
FROM myTable
WHERE colA IN ('121', '122','123','124','125','126','127','128','129')
GROUP BY colA
) c
GROUP BY colC
) c ON c.colC = t.colC
UMM的第二個表包含了預期的效果? – Mihai 2014-10-17 20:35:42
你如何選擇colB和colC? – 2014-10-17 20:39:07
我用上面的select語句選擇它。 – adayzdone 2014-10-17 20:39:53