SQLite GROUP BY 分为两列

问题描述 投票:0回答:1

我有三张桌子:

CREATE TABLE IF NOT EXISTS countable(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS collection(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS count(
    countableId INTEGER NOT NULL,
    collectionId INTEGER NOT NULL,
    count INTEGER NOT NULL
);

我的计划是能够选择两个集合(例如

collection.id = 3 OR collection.id = 4
)并为每个可数获得两个计数。这已经有效了:

+-----------+------------+-------+
| countable | collection | count |
+-----------+------------+-------+
| Example   | Col 3      | 0     |
| Example   | Col 4      | 5     |
| Test      | Col 3      | 1     |
| Test      | Col 4      | 0     |
| Foo       | Col 3      | 0     |
| Foo       | Col 4      | 0     |
| Bar       | Col 3      | 5     |
| Bar       | Col 4      | 3     |
+-----------+------------+-------+

但是,我想要一张没有重复可配置的表,如下所示:

+-----------+--------------+----------------+
| countable | primaryCount | secondaryCount |
+-----------+--------------+----------------+
| Example   | 0            | 5              |
| Test      | 1            | 0              |
| Foo       | 0            | 0              |
| Bar       | 5            | 3              |
+-----------+--------------+----------------+

我尝试使用分组,这很有效:

SELECT
countable.name AS countable,
count AS primaryCount,      <--- This is the Problem
count AS secondaryCount     <--- This is the Problem
FROM count
JOIN countable ON countable.id = count.countableId
JOIN collection ON collection.id = count.collectionId
WHERE collection.id = 3 OR collection.id = 4
GROUP BY countable.id
;

但这只是两次返回 primatyCount

+-----------+--------------+----------------+
| countable | primaryCount | secondaryCount |
+-----------+--------------+----------------+
| Example   | 0            | 0              |
| Test      | 1            | 1              |
| Foo       | 0            | 0              |
| Bar       | 5            | 5              |
+-----------+--------------+----------------+

我也尝试过像这样的group_concat:

SELECT
countable.name AS countable,
group_concat(count)
FROM count
JOIN countable ON countable.id = count.countableId
JOIN collection ON collection.id = count.collectionId
WHERE collection.id = 3 OR collection.id = 4
GROUP BY countable.id
;

返回:

+-----------+---------------------+
| countable | group_concat(count) |
+-----------+---------------------+
| Example   | 0,5                 |
| Test      | 1,0                 |
| Foo       | 0,0                 |
| Bar       | 5,3                 |
+-----------+---------------------+

这是有效的,但是我必须在代码中分割这些值,我想这并不是很理想。是否有一些像 count[0] 和 count[1] 这样的语法或其他解决方案可以在 SQLite 中干净地完成此操作?

sqlite group-by aggregate
1个回答
0
投票

您似乎只想在这里进行条件聚合:

SELECT
    cnt.name AS countable,
    COUNT(CASE WHEN col.id = 3 THEN 1 END) AS primaryCount
    COUNT(CASE WHEN col.id = 4 THEN 1 END) AS secondaryCount
FROM count c
INNER JOIN countable cnt
    ON cnt.id = c.countableId
INNER JOIN collection col ON col.id = c.collectionId
GROUP BY cnt.name;
© www.soinside.com 2019 - 2024. All rights reserved.