简体   繁体   English

SQL Server:如何在列中选择具有相同值的行,但在另一列上为分组行选择一些精确值

[英]SQL Server : how to select the rows in a table with the same value on a column but some exact values on another column for the grouped rows

I have this table with some sample data: 我有这个表有一些示例数据:

Supplier_ID Product_ID  Stock
-----------------------------
1             272          1
1             123          5
1             567          3
1             564          3
2             272          4
2             12           3
2             532          1
3             123          4
3             272          5

I want to check the suppliers that have both products: 272 and 123 , so the result would be like: 我想查看同时包含两种产品的供应商: 272123 ,结果如下:

Supplier_ID
-----------
1
3

You can use GROUP BY and HAVING : 您可以使用GROUP BYHAVING

SELECT Supplier_ID
FROM your_tab
WHERE Product_ID IN (272, 123)
GROUP BY Supplier_ID
HAVING COUNT(DISTINCT Product_ID) = 2;

LiveDemo

Try this code: 试试这段代码:

SELECT A.Supplier_ID FROM
 (SELECT Supplier_ID FROM Your_Table WHERE Product_ID = 272) AS A
INNER JOIN
 (SELECT Supplier_ID FROM Your_Table WHERE Product_ID = 123) AS B
   ON A.Supplier_ID = B.Supplier_ID

This is how it works using set operations . 这是使用set操作的工作原理。 IMHO a too little used feature of databases. 恕我直言,数据库使用的功能太少了。

select Supplier_ID from table1 where product_id=272
intersect
select Supplier_ID from table1 where product_id=123

it produces as well 它也产生

Supplier_ID
1
3

By the way a distinct is not needed due to intersect delivers distinct rows. 不同不需要因交叉的方式提供不同的行。

http://sqlfiddle.com/#!6/13b11/3 http://sqlfiddle.com/#!6/13b11/3

Try This Code: By Using Row_number() 试试这段代码:使用Row_number()

;WITH cte
     AS (SELECT *,
                Row_number()
                  OVER(
                    partition BY [Supplier_ID]
                    ORDER BY [Supplier_ID]) AS rn
         FROM   #Your_Table
         WHERE  Product_ID IN ( 272, 123 ))
SELECT DISTINCT Supplier_ID
FROM   cte
WHERE  rn > 1 

OUTPUT : 输出

Supplier_ID
1
3

暂无
暂无

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

相关问题 SQL如何获取分组表中表中列的最大值的行 - SQL how to get rows with max value of a column in the table in grouped rows SQL Server-如何检查同一列值的相同表的其他行中是否不存在值? - SQL Server - How to check if a value does not exist in other rows of the same table for same column values? 我如何从 MySQL 表中的 select 行按一列分组,另一列具有所需值 - How do I select rows from a MySQL table grouped by one column with required values in another ORACLE SQL:根据另一列中的相同键值选择基于一列中不同值的多行 - ORACLE SQL: select multiple rows basd on different values in one column for a same key value in another column SQL:选择某列中具有值的行,而该值不包含在另一个表中 - SQL : select rows with value in some column and the value does is not contained in another table Select 具有相同 ID 但在另一列中按第三列分组的不同值的行 - Select rows with same id but different values in another column grouped by a third column 几行值完全相同,但只有一列返回不同的值; 如何获取此值并为此创建另一个列? - Several rows with exact same values but only one column returns different value; How to grab this value and create another column(s) for that? SQL Server:选择行,其中列中的值从值列表更改为另一个值列表 - SQL Server: Select rows where value in column changes from list of values to another list of values SQL Server:排除表中的行如果有另一行具有相同的col值和特定的二级列值 - SQL Server : excluding rows from a table IF there is another row with the same col value and a particular secondary column value 如何选择某些列中具有相同值的所有行 - How to select all rows which have same value in some column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM