我有一个包含多列的表,但是我关心的两列可以称为Alpha和Beta。还有第三个,Gamma,它是Alpha和Beta之间的比较。我们将简化它,并说Gamma是Alpha和Beta之间的区别,所以我有这样的东西:
Alpha Beta Gamma
----- ---- -----
1 A (some answer)
1 B (some answer)
1 C (some answer)
2 A (some answer)
2 B (some answer)
2 C (some answer)
3 A (some answer)
3 B (some answer)
3 C (some answer)
4 A (some answer)
4 B (some answer)
4 C (some answer)
此时内容的Gamma值并不重要。
因此,假设我最终在Beta版中有了一个新值“ D”。我有1D和3D,但没有2D和4D。
我可以在Sqlite中通过查询找到哪些Alpha值还没有对应的D值吗?例如,在此表中:
Alpha Beta Gamma
----- ---- -----
1 A (some answer)
1 B (some answer)
1 C (some answer)
1 D (some answer)
2 A (some answer)
2 B (some answer)
2 C (some answer)
3 A (some answer)
3 B (some answer)
3 C (some answer)
3 D (some answer)
4 A (some answer)
4 B (some answer)
4 C (some answer)
由于我需要每个Alpha值都具有相应的Beta值,因此这似乎是我需要做的事情:
最终,我需要填写表格,以便每个Alpha值对于Beta中的每个正则值都有一个匹配的Beta值。
(注意:第二张表后的部分已更改,以提供我认为我需要做的特定步骤的列表。)
答案 0 :(得分:1)
你能做吗
1)对于问题(a)的答案:
Select beta from test
GROUP BY beta
having count(alpha) < (select count(distinct alpha) from test);
2)问题(b):
Select alpha from test
GROUP BY alpha
having count(beta) < (select count(distinct beta) from test);
尝试以下输入:
Create table test (alpha int, beta varchar(2));
insert into test values (1, "A");
insert into test values (1, "B");
insert into test values (1, "C");
insert into test values (1, "D");
insert into test values (2, "A");
insert into test values (2, "B");
insert into test values (2, "C");
insert into test values (3, "A");
insert into test values (3, "B");
insert into test values (3, "C");
insert into test values (3, "D");
insert into test values (4, "A");
insert into test values (4, "B");
insert into test values (4, "C");
答案 1 :(得分:0)
计算每个Alpha的Beta数量:
SELECT Alpha, COUNT(*)
FROM MyTable
GROUP BY Alpha;
然后取最大值,并检索所有较小的计数:
WITH Counts(Alpha, Count) AS (
SELECT Alpha, COUNT(*)
FROM MyTable
GROUP BY Alpha
)
SELECT Alpha
FROM Counts
WHERE Count < (SELECT MAX(Count)
FROM Counts);