如何找出一列中是否有一个帐户有多个值?

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

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

huangapple
  • 本文由 发表于 2023年6月15日 03:14:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476867.html
匿名

发表评论

匿名网友

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

确定