从另外两列的特定行条件中查找第三列的值。

huangapple go评论58阅读模式
英文:

Finding value from a third column based on specific row criteria of 2 other columns

问题

以下是您要求的翻译部分:

编辑于2023年3月6日。 添加了更多信息。根据本帖子中的反馈提供了解决方案。谢谢!!

在下面提供的示例中,基于将筛选出两列上的行的条件,我希望再次筛选出它们以获得CacheUID(第三列)

下面的代码将允许您重新创建以下结果集:

CacheUID FilterCriteriaName CriteriaValue
1 Product 4
2 Product 4
2 Product 6
3 Product 4
3 Product 6
3 Product 8
4 Equipment 16
4 Equipment 3
5 Equipment 4
5 Equipment 6
5 Product 6
5 Product 4

基于不同的行组合(按FilterCriteriaName和CriteriaValue),我需要检索对应的CacheUID。FilterCriteriaName和CriteriaValue对应于筛选信息。以下是两个示例:

情景1:
想要在基于Product 4和Product 6的筛选的情况下检索正确的CacheUID(仅1个)。Product 4和6的组合可以在CacheUID 2、3和5中找到。但仅CacheUID 2包含仅这些标准。

情景2:
想要在基于Product 4、Product 6和Equipment 6的筛选的情况下检索正确的CacheUID(仅1个)。Product 4可以在CacheUID 1、2、3和5中找到。Product 6可以在CacheUID 2、3、4和5中找到。Equipment 6可以在CacheUID 5中找到。但是,只有CacheUID 5中包含Product 4、6和Equipment 6的组合。

以下是我对每种情况需要的结果:

情景1:

CacheUID
2

情景2:

CacheUID
5

运行下面的查询,将创建正确的数据以涵盖列出的情况,并现在包含最后两个SELECT中的解决方案。

英文:

EDITED ON March 6, 2023. Added more information. Also providing the solution based on feedback from people on this thread. Thanks!!

In the example provided below, based on conditions that will filter out rows on two columns, I want to filter them out again to get the CacheUID (third column)

The code below will allow you to recreate the following resultset:

CacheUID FilterCriteriaName CriteriaValue
1 Product 4
2 Product 4
2 Product 6
3 Product 4
3 Product 6
3 Product 8
4 Equipment 16
4 Equipment 3
5 Equipment 4
5 Equipment 6
5 Product 6
5 Product 4

Based on different combination of rows (per FilterCriteriaName and CriteriaValue), I need to retrieve what is the corresponding CacheUID. The FilterCriteriaName and CriteriaValue corresponds to filter information. Here are two examples:

Scenario 1:
Want to retrieve the correct CacheUID (and only 1) when filter is based on Product 4 AND Product 6.The combination of Product 4 and 6 can be found in CacheUID 2, 3 and 5. But ONLY CacheUID 2 contains ONLY these criteria.

Scenario 2:
Want to retrieve the correct CacheUID (and only 1) when filter is based on Product 4 AND Product 6 AND Equipment 6.Product 4 can be found in CacheUID 1, 2, 3 and 5. Product 6 can be found in CacheUID 2, 3, 4 and 5. Equipment 6 can be found in CacheUID 5, But, the combination of Product 4 and 6 and Equipment 6 can be found only in CacheUID 5.

And here are the result I need for each scenario

Scenario 1:

CacheUID
2

Scenario 2:

CacheUID
5

Run the query below that will create the right data to cover the scenarios listed and now contains the solution in the last two SELECT.


CREATE TABLE #TestTable
(
	CacheUID [bigint] NOT NULL,
	FilterCriteriaName VARCHAR(50) NOT NULL,
	CriteriaValue [bigint] NOT NULL,
	UID [bigint] IDENTITY(1,1) NOT NULL
)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Product', 1, 4)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Product', 2, 4)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Product', 2, 6)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Product', 3, 4)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Product', 3, 6)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Product', 3, 8)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Equipment', 4, 16)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Equipment', 4, 3)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Equipment', 5, 4)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Equipment', 5, 6)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Product', 5, 6)

INSERT	#TestTable (FilterCriteriaName, CacheUID, CriteriaValue)
VALUES	('Product', 5, 4)

SELECT	CacheUID, FilterCriteriaName, CriteriaValue
FROM	#TestTable

-----------------------------------------------
-- Scenario 1: Filter on two products (4, 6) --
-----------------------------------------------

SELECT		'Scenario 1 - Only 4 or 6', CacheUID
FROM		#TestTable
GROUP BY	CacheUID
HAVING		COUNT(CASE WHEN FilterCriteriaName = 'Product' AND CriteriaValue = 4 THEN 1 END) > 0 AND			-- Product 4 present
			COUNT(CASE WHEN FilterCriteriaName = 'Product' AND CriteriaValue = 6 THEN 1 END) > 0 AND			-- Prodct 6 present
			COUNT(CASE WHEN FilterCriteriaName = 'Product' AND CriteriaValue NOT IN (4, 6) THEN 1 END) = 0 AND  -- Only product 4 or 6
			COUNT(CASE WHEN FilterCriteriaName = 'Equipment' AND CriteriaValue > 0 THEN 0 END) = 0

---------------------------------------------------------------------
-- Scenario 2: Filter on two products (4, 6) and one equipment (6) --
---------------------------------------------------------------------

SELECT		'Scenario 2 - Only Product 4, 6 Present + Equipment 6', CacheUID
FROM		#TestTable
GROUP BY	CacheUID
HAVING		COUNT(CASE WHEN FilterCriteriaName = 'Product' AND CriteriaValue = 4 THEN 1 END) > 0 AND			-- Product 4 present
			COUNT(CASE WHEN FilterCriteriaName = 'Product' AND CriteriaValue = 6 THEN 1 END) > 0 AND			-- Product 6 present
			COUNT(CASE WHEN FilterCriteriaName = 'Product' AND CriteriaValue NOT IN (4, 6) THEN 1 END) = 0 AND  -- Only Product 4 or 6
			Count(CASE WHEN FilterCriteriaName = 'Equipment' AND CriteriaValue = 6 THEN 1 END) = 1				-- Equipment 6 present


DROP TABLE #TestTable

Thanks!

答案1

得分: 1

Your query is almost correct, and can be made to work by adding a third assertion in the HAVING clause which excludes any criteria value which is not 4 or 6:

SELECT CacheUID
FROM #TestTable
WHERE FilterCriteriaName = 'Product'
GROUP BY CacheUID
HAVING COUNT(CASE WHEN CriteriaValue = 4 THEN 1 END) > 0 AND         -- 4 present
       COUNT(CASE WHEN CriteriaValue = 6 THEN 1 END) > 0 AND         -- 6 present
       COUNT(CASE WHEN CriteriaValue NOT IN (4, 6) THEN 1 END) = 0;  -- only 4 or 6
英文:

Your query is almost correct, and can be made to work by adding a third assertion in the HAVING clause which excludes any criteria value which is not 4 or 6:

<!-- language: sql -->

SELECT CacheUID
FROM #TestTable
WHERE FilterCriteriaName = &#39;Product&#39;
GROUP BY CacheUID
HAVING COUNT(CASE WHEN CriteriaValue = 4 THEN 1 END) &gt; 0 AND         -- 4 present
       COUNT(CASE WHEN CriteriaValue = 6 THEN 1 END) &gt; 0 AND         -- 6 present
       COUNT(CASE WHEN CriteriaValue NOT IN (4, 6) THEN 1 END) = 0;  -- only 4 or 6

huangapple
  • 本文由 发表于 2023年3月4日 00:47:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定