更新:結果各組
可以有IS_ACTIVE = TRUE後IS_ACTIVE =假,所以我需要得到最後分配IS_ACTIVE = TRUE,並得到前IS_ACTIVE =虛假記載
我有一個表具有這種結構
id (not pk but each group is incremental), name, grp, is_active (boolean)
像這樣的數據組1
1, name1, group1, true
2, name2, group1, true
3, name3, group1, false
4, name1, group1, false
5, name2, group1, true
6, name3, group1, false <-- this is the next assigned id as the preceding record has an is_active = true
7, (names will differt, group the same and all false)...
點...在同一個表,但是對於第2組
100, name1, group1, true
101, name2, group1, true
102, name3, group1, true
103, name1, group1, true
104, name2, group1, true
105, name3, group1, false <-- this is the next assigned id as the preceding record has an is_active = true
106, (names will differt, group the same and all false)...
我有這個,但它不工作,我將它喜歡
SELECT grp, COUNT(*)
FROM tbl_1
WHERE is_active = false
AND id > (
SELECT id
FROM tbl_1
WHERE is_active = true
ORDER BY id DESC
LIMIT 1
)
GROUP BY grp
所以我想回報是更多的數據:
group1, 6
group2, 105
,但我只得到
group2, 105
對不起,我已經更新的問題,我會給出一個給予好評所有誰正確地回答了前面的問題 –