![](/img/trans.png)
[英]SQL query for the number of cases when a value of column1 (non-unique) can't be found within any record where column2 meets a basic criteria
[英]Find number of non-unique column values within a group where a second column value is all the same
我有两张桌子。
Table 1:
id_a,
id_b,
id_t
Table 2:
id_t,
name
如果表 2 名称以a
开头,我需要找出任何匹配id_ts
也有匹配id_as
的东西。 如果表 2 名称以b
开头,我需要从任何匹配 id_ts 的行中找出匹配的id_bs
。
我需要知道这些匹配发生了多少次。
表格1
id_a | id_b | id_t |
---|---|---|
1 | 0 | 123 |
1 | 0 | 123 |
2 | 0 | 123 |
0 | 4 | 456 |
0 | 4 | 456 |
0 | 5 | 456 |
0 | 5 | 456 |
0 | 5 | 456 |
0 | 6 | 456 |
0 | 7 | 456 |
表 2
id_t | 姓名 |
---|---|
123 | aaq |
456 | BW |
所以在这个例子中,我想看到这样的结果
id_t | 姓名 | num_non_unique |
---|---|---|
123 | aaq | 1 |
456 | BW | 2 |
我目前的代码是这样的:
SELECT
t2.id_t, t2.name, count(t1.*) AS num_non_unique
FROM
Table 2 AS t2
JOIN Table 1 as t1 ON t2.id_t = t1.id_t
WHERE
(t2.name like 'a%' and t1.id_a in (SELECT id_a FROM t1 GROUP BY id_a, id_t HAVING count(*) > 1))
OR (t2.name like 'b%' AND t1.id_b IN (SELECT id_b FROM t1 GROUP BY id_b, id_t HAVING count(*) > 1))
GROUP BY t1.name, t1.id_t
这目前没有给我想要的结果。 使用此代码,我似乎获得了 id_b 的所有可用行的计数,以及 id_a 的 1 + non_uniques 的计数(因此,对于一个非唯一的,值为 2,否则该列为 1)。
任何帮助表示赞赏!
试试这个代码:
select name,id_ts,sum(count_) "num_non_unique" from
(
select t1.id_a,t1.id_b,t1.id_ts,t2.name, count(*) "count_"
from tab1 t1
inner join tab2 t2 on t1.id_ts=t2.id_t
group by 1,2,3,4
having count(*)=1
)tab
group by 1,2
说明:正如您在问题中提到的,一列值将始终基于名称起始字符相同。 因此,无需检查查询中名称的开头。
简单地说,我们将检查唯一行的计数并使用having count(*)=1
的条件对其进行过滤。
获得唯一行后,只需将其分组即可按name
和id_ts
分组的行数
模式和插入语句:
create table table_1(id_a int, id_b int, id_t int);
insert into table_1 values(1, 0, 123);
insert into table_1 values(1, 0, 123);
insert into table_1 values(2, 0, 123);
insert into table_1 values(0, 4, 456);
insert into table_1 values(0, 4, 456);
insert into table_1 values(0, 5, 456);
insert into table_1 values(0, 5, 456);
insert into table_1 values(0, 5, 456);
insert into table_1 values(0, 6, 456);
insert into table_1 values(0, 7, 456);
create table table_2 (id_t int, name varchar(50));
insert into table_2 values(123, 'aaq');
insert into table_2 values(456, 'bws');
询问:
with case1 as
(
select id_t, id_a
from table_1
group by id_t, id_a
having count(*)=1
),
case2 as
(
select id_t,id_b
from table_1
group by id_t,id_b
having count(*)=1
)
select id_t,name,
(case when t2.name like 'a%' then (select count(*) from case1 where case1.id_t=t2.id_t) when t2.name like 'b%' then (select count(*) from case2 where case2.id_t=t2.id_t) end)num_non_unique
from table_2 as t2
Output:
id_t | 姓名 | num_non_unique |
---|---|---|
123 | aaq | 1 |
456 | BW | 2 |
db<小提琴在这里
如果我理解正确,您需要对table_2
中的每一行进行id_b
table_1
出现一次。
一种方法是:
select t2.*, coalesce(cnt, 0)
from table_2 t2 left join
(select id_a, count(*) as cnt
from (select id_a, id_b
from table_1
group by id_a, id_b
having count(*) = 1
) t1
group by id_a
) t1
on t1.id_a = t2.id_a;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.