简体   繁体   English

我怎样才能 select 具有相同值的记录?

[英]How can I select records which has same value?

I have a table.我有一张桌子。 This table includes customers and products.该表包括客户和产品。 Sample data is is in below.示例数据在下面。

Customer Name顾客姓名 Product产品
John约翰 laptop笔记本电脑
John约翰 telephone电话
John约翰 laptop笔记本电脑
Kevin凯文 laptop笔记本电脑
Kevin凯文 laptop笔记本电脑
Kevin凯文 laptop笔记本电脑

I want to select customer who chose all their products with a laptop so This case I want to select Kevin because he selected laptop his all products.我想要 select 客户,他选择了他们所有产品的笔记本电脑,所以这个案例我想要 select 凯文,因为他选择了笔记本电脑他的所有产品。 I dont want to select john because he selected laptop but also select telephone not all product of john laptop.我不想 select 约翰因为他选择了笔记本电脑而且 select 电话不是约翰笔记本电脑的所有产品。

How can I do this in tsql?我如何在 tsql 中执行此操作?

You can use conditional aggregation in the having clause您可以在 having 子句中使用条件聚合

SELECT [Customer Name]
FROM YourTable t
GROUP BY [Customer Name]
HAVING COUNT(CASE WHEN Product <> 'laptop' THEN 1 END) = 0
-- alternatively
HAVING COUNT(CASE WHEN Product = 'laptop' THEN 1 END) = COUNT(*)
SELECT Name
FROM Customers
WHERE (SELECT Name FROM Customers WHERE Product = 'telephone') != Name
GROUP BY NAME;

This should help you, it might not be the final asnwer but it shall provide enough for you to figure it out in some edge cases这应该对你有帮助,它可能不是最终的答案,但它应该足以让你在某些边缘情况下弄明白

暂无
暂无

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

相关问题 如何将其他表中具有相关记录的SQL记录复制到同一数据库? - How can I copy a SQL record which has related records in other tables to the same database? 如何选择未超过值的记录 - How do I select records where a value has not been exceeded 如果可以在同一日期找到多个记录,如何选择一个值 - How to select a value if multiple records can be found on the same date 如何从具有诸如product,attribute,value列的表中编写选择查询? - How can I write a select query from a table which has columns like product,attribute,value? 如何在同一事务中 SELECT 未提交记录? - How can I SELECT uncommitted records within the same transaction? 当同一个ID有多行但我正在寻找缺少特定值的ID时如何选择ID - How to select id when same id has multiple rows but I am looking for id which are missing a particular value 使用activerecord时,如何根据字段的最高值选择记录? - with activerecord how can I select records based on the highest value of a field? 如何 select 记录与 json 字段数组中的值匹配? - How can I select records that match a value in a json field array? 我如何从最后一个值累积 select 条记录 - How can I select records from the last value accumulated 如何通过比较列A中具有相同值的记录中的列B,C中的值来选择记录? - How do I select records by comparing values in columns B, C among records with same value in column A?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM