sql“分组依据”相同的 PersonID,不同的 PersonName。消除重复项

发布于 2024-12-14 11:32:13 字数 702 浏览 0 评论 0 原文

我有一个(相当脏的)数据源(excel),如下所示:

ID |名称 |主题 |等级
123 | 123史密斯,乔·R。数学 | 2.0
123 | 123史密斯,乔·罗德里格斯 |法语 | 3.0
234 | 234美国能源部,玛丽·简·D.|生物学 | 2.5
234 | 234美国能源部,玛丽·简·道森|化学| 2.5
234 | 234美国能源部,玛丽·简|法语 | 3.5

我的应用程序的输出应该如下所示:

Smith, Joe R.
123

MATH | 2.0
FRENCH | 3.0

所以基本上我想做查询(仅针对 ID/Person 父“容器”),例如:

SELECT DISTINCT ID, Name FROM MyTable<br/>

or

SELECT ID, Name FROM MyTable GROUP BY ID


当然以上两种都是无效的,行不通。

我想“合并”相同的 ID,并忽略/截断具有相同 ID/不同名称的其他记录(因为我们都知道他们是同一个人,因为 ID 是我们的标识符,显然这只是一个拼写错误/脏数据) .

这可以通过单个 SELECT 查询来完成吗?

I have a (rather dirty) datasource (excel) that looks like this:

ID | Name | Subject | Grade
123 | Smith, Joe R. | MATH | 2.0
123 | Smith, Joe Rodriguez | FRENCH | 3.0
234 | Doe, Mary Jane D.| BIOLOGY | 2.5
234 | Doe, Mary Jane Dawson| CHEMISTRY | 2.5
234 | Doe, Mary Jane | FRENCH | 3.5

My application's output should look like this:

Smith, Joe R.
123

MATH | 2.0
FRENCH | 3.0

So basically I want to do query (just for the ID/Person parent 'container') something like:

SELECT DISTINCT ID, Name FROM MyTable<br/>

or

SELECT ID, Name FROM MyTable GROUP BY ID

Of course both of the above are invalid and won't work.

I would like to 'combine' the same ID's and ignore/truncate the other records with the same ID/different Name (because we all know they're the same person since ID is our identifier and clearly it's just a typo/dirty data).

Can this be done by a single SELECT query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铁轨上的流浪者 2024-12-21 11:32:14

如果您不太关心名称字段中显示哪个值,请使用 <代码>MAX()MIN()

SELECT    ID,
          MAX(Name) AS Name
FROM      [YourTable]
GROUP BY  ID

这是一个可以使用的工作示例:https://data.stackexchange.com/stackoverflow/q/116699/

If you don't really care which value shows up in the name field, use MAX() or MIN():

SELECT    ID,
          MAX(Name) AS Name
FROM      [YourTable]
GROUP BY  ID

Here's a working example to play with: https://data.stackexchange.com/stackoverflow/q/116699/

故事还在继续 2024-12-21 11:32:14

您可以找到名称的最小值或最大值

SELECT ID, Max(Name)
FROM MyTable
GROUP BY ID

You can find the MIN or MAX Value of Name

SELECT ID, Max(Name)
FROM MyTable
GROUP BY ID
む无字情书 2024-12-21 11:32:14
SELECT A.ID, A.NAME, T.Subject, T.Grade
FROM (SELECT ID, MIN(NAME) AS NAME
      FROM MyTable
      GROUP BY ID) A
LEFT JOIN MyTable T on A.ID = T.ID

会给你类似的东西

123    Smith, Joe R.     Math      2.0
123    Smith, Joe R.     FRENCH    3.0
234    Doe, Mary Jane    BIOLOGY   2.5
234    Doe, Mary Jane    CHEMISTRY 2.5
234    Doe, Mary Jane    FRENCH    3.5
SELECT A.ID, A.NAME, T.Subject, T.Grade
FROM (SELECT ID, MIN(NAME) AS NAME
      FROM MyTable
      GROUP BY ID) A
LEFT JOIN MyTable T on A.ID = T.ID

Will give you something like

123    Smith, Joe R.     Math      2.0
123    Smith, Joe R.     FRENCH    3.0
234    Doe, Mary Jane    BIOLOGY   2.5
234    Doe, Mary Jane    CHEMISTRY 2.5
234    Doe, Mary Jane    FRENCH    3.5
何以笙箫默 2024-12-21 11:32:14

如果您不关心保留哪个名称,则可以使用 MAX()MIN() 聚合来仅选择一个名称:

SELECT ID, MAX(Name) as Name 
FROM MyTable GROUP BY ID

If you don't care which name you keep, you can use a MAX() or MIN() aggregate to pick just one name:

SELECT ID, MAX(Name) as Name 
FROM MyTable GROUP BY ID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文