英文:
How can I find if a column has more than one value per account?
问题
我有一个存储特定经销商车辆的数据库。我正在尝试找出是否有任何帐户中有多个不同制造商的车辆。数据看起来类似于以下内容:
对于所有我的数据,我想找出哪些经销商在"Make"列中有超过一个值且"Type"为"New"。如果我在上述数据上运行查询,结果将为零,因为每个经销商只有一个制造商。
但如果我的数据如下所示:
我的结果将如下所示:
1
2
因为有两个经销商拥有多个制造商。
英文:
I have a database that stores vehicles for specific dealers. I'm trying to find out if there are any accounts that have multiple vehicle Makes in them. The data looks something like this:
ID Name VIN Make Type
1 Joes Ford 1123232 Ford New
1 Joes Ford 3324233 Ford New
1 Joes Ford 4544543 Ford New
2 Mikes Chevy 7645655 Chevy New
2 Mikes Chevy 9865765 Chevy New
2 Mikes Chevy 2222234 Chevy New
For all of my data I want to find out how which dealers have more than one value in the Make column where the Type is New. If I run the query on the above data it would come out to zero because each dealer only has one Make.
But if my data looks like this:
ID Name VIN Make Type
1 Joes Ford 1123232 Ford New
1 Joes Ford 3324233 Volvo New
1 Joes Ford 4544543 Ford New
2 Mikes Chevy 7645655 Chevy New
2 Mikes Chevy 9865765 Acura New
2 Mikes Chevy 2222234 Dodge New
My results would look like this:
ID
1
2
because there are two dealers that have more than one Make.
答案1
得分: 1
SELECT ID
FROM Dealer
GROUP BY ID
HAVING COUNT(DISTINCT MAKE) > 1
英文:
SELECT ID
FROM Dealer
GROUP BY ID
HAVING COUNT(DISTINCT MAKE)>1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论