[英]How to get row if count of field lower that number in each row in sql query
Let me try to explain my problem: I have two tables 让我尝试解释我的问题:我有两个表
Table A
|user|type|
|----|----|
|A |1 |
|B |2 |
|A |1 |
|A |1 |
and
Table B
|type|value|
|----|-----|
|1 |2 |
|2 |1 |
I want to get rows if count of each user in table A is less or equal than proper type value from table B. 如果表A中每个用户的计数小于或等于表B中的正确类型值,我想获取行。
So result for this example should be: 因此,此示例的结果应为:
user|user count
----|----------
B |1
Nikola, you did not say if the user with multiple rows in A has always the same type. Nikola,您没有说在A中具有多行的用户是否始终具有相同的类型。 Assuming is has:
假设有:
select user, cnt as 'user count'
from (
select a.user, b.value, count(*) as cnt
from a
join b on a.type=b.type
group by a.user, b.value
having count(*) <= b.value
) as q
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.