简体   繁体   English

如何在SQL中有条件地计算值

[英]How to count values conditionally in SQL

I have an index table of entity attribute values that looks like this: +-----------+--------------+----------+-------+ | entity_id | attribute_id | store_id | value | +-----------+--------------+----------+-------+ | 38 | 190 | 1 | 22 | | 38 | 190 | 1 | 23 | | 39 | 190 | 1 | 22 | | 39 | 190 | 1 | 23 | | 39 | 190 | 1 | 42 | | 40 | 190 | 1 | 22 | | 41 | 190 | 1 | 54 | | 42 | 190 | 1 | 54 | | 43 | 190 | 1 | 22 | | 44 | 190 | 1 | 22 | | 45 | 190 | 1 | 54 | +-----------+--------------+----------+-------+ 我有一个实体属性值的索引表,如下所示: +-----------+--------------+----------+-------+ | entity_id | attribute_id | store_id | value | +-----------+--------------+----------+-------+ | 38 | 190 | 1 | 22 | | 38 | 190 | 1 | 23 | | 39 | 190 | 1 | 22 | | 39 | 190 | 1 | 23 | | 39 | 190 | 1 | 42 | | 40 | 190 | 1 | 22 | | 41 | 190 | 1 | 54 | | 42 | 190 | 1 | 54 | | 43 | 190 | 1 | 22 | | 44 | 190 | 1 | 22 | | 45 | 190 | 1 | 54 | +-----------+--------------+----------+-------+ +-----------+--------------+----------+-------+ | entity_id | attribute_id | store_id | value | +-----------+--------------+----------+-------+ | 38 | 190 | 1 | 22 | | 38 | 190 | 1 | 23 | | 39 | 190 | 1 | 22 | | 39 | 190 | 1 | 23 | | 39 | 190 | 1 | 42 | | 40 | 190 | 1 | 22 | | 41 | 190 | 1 | 54 | | 42 | 190 | 1 | 54 | | 43 | 190 | 1 | 22 | | 44 | 190 | 1 | 22 | | 45 | 190 | 1 | 54 | +-----------+--------------+----------+-------+

As you can see, a single entity can have multiple values for a single attribute ( entity_id 38 has value s 22,23) and these values are not unique per entity ( entity_id 38,39 both share value 22). 如您所见,单个实体可以具有单个属性的多个值( entity_id 38具有value s 22,23),并且这些值对于每个实体不是唯一的( entity_id 38,39都共享value 22)。

The first problem to solve is getting the number of distinct entities per value; 要解决的第一个问题是获得每个值的不同实体的数量; this is easily accomplished with: 这很容易实现:

SELECT value, COUNT(entity_id) AS count
FROM catalog_product_index_eav
WHERE attribute_id=190
GROUP BY value;

which results in: +-------+-------+ | value | count | +-------+-------+ | 22 | 5 | | 23 | 2 | | 42 | 1 | | 54 | 3 | +-------+-------+ 结果是: +-------+-------+ | value | count | +-------+-------+ | 22 | 5 | | 23 | 2 | | 42 | 1 | | 54 | 3 | +-------+-------+ +-------+-------+ | value | count | +-------+-------+ | 22 | 5 | | 23 | 2 | | 42 | 1 | | 54 | 3 | +-------+-------+

My question is how can I nest an OR condition in this count, namely: for some specific value Y, for each value X, count the number of entities that have either value X or Y. 我的问题是如何在此计数中嵌入OR条件,即:对于某个特定值Y,对于每个值X,计算具有值X或Y的实体数。

I would like to do this in a single query. 我想在一个查询中执行此操作。 For instance, for attribute_id 190 and value 23, the output from above example should be: +-------+-------+ | value | count | +-------+-------+ | 22 | 5 | # all entities with value 22 happen to have 23 as well | 23 | 2 | that is, one is a subset of the other | 42 | 2 | # intersection is nonempty | 54 | 5 | # sets are disjoint +-------+-------+ 例如,对于attribute_id 190和value 23,上面示例的输出应为: +-------+-------+ | value | count | +-------+-------+ | 22 | 5 | # all entities with value 22 happen to have 23 as well | 23 | 2 | that is, one is a subset of the other | 42 | 2 | # intersection is nonempty | 54 | 5 | # sets are disjoint +-------+-------+ +-------+-------+ | value | count | +-------+-------+ | 22 | 5 | # all entities with value 22 happen to have 23 as well | 23 | 2 | that is, one is a subset of the other | 42 | 2 | # intersection is nonempty | 54 | 5 | # sets are disjoint +-------+-------+

select c1.value, 
( SELECT COUNT(DISTINCT entity_id) as count 
  FROM catalog_product_index_eav 
  where attribute_id=81 
  and (value=c1.value || value=7) ) as count 
FROM catalog_product_index_eav c1 
WHERE attribute_id=81 
GROUP BY c1.value

Try to use 尝试使用

Select C.value, max(C.count) from (
SELECT A.value, (Select COUNT(B.entity_id) from FROM catalog_product_index_eav B
WHERE B.attribute_id=A.attribute_id and (B.value=A.value or B.value=23)) AS count
FROM catalog_product_index_eav A
WHERE A.attribute_id=190) C group by C.value

It gives you both results in single query if it is satisfied your output. 如果满足您的输出,它会在单个查询中为您提供两个结果。

SELECT i.value AS [value], COUNT(i.entity_id) AS [count], 
(SELECT COUNT(b.entity_id) tot FROM catalog_product_index_eav b
        WHERE b.entity_id IN (SELECT v.entity_id FROM 
        catalog_product_index_eav v WHERE v.VALUE = i.value)) AS [total_count]
FROM catalog_product_index_eav i
WHERE i.attribute_id=190
GROUP BY i.value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM