我可以采用一组不同的 COALESCE 数据吗?

发布于 2024-11-16 11:45:19 字数 261 浏览 3 评论 0原文

我有以下代码,到目前为止它有效:

SELECT  
    [id],
    COALESCE ([Company], [LastName] + ', ' + [FirstName]) as Customer
FROM [some_database].[dbo].[some_table]
ORDER BY Customer

但是,我想使用 DISTINCT 关键字来消除重复的“客户”条目。这可能吗?我尝试了几种不同的方法,但没有效果。

I have the following bit of code, and it works so far:

SELECT  
    [id],
    COALESCE ([Company], [LastName] + ', ' + [FirstName]) as Customer
FROM [some_database].[dbo].[some_table]
ORDER BY Customer

However, I'd like to use the DISTINCT keyword to eliminate duplicate "Customer" entries. Is this possible? I've tried it a few different ways, but to no avail.

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

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

发布评论

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

评论(1

岁月流歌 2024-11-23 11:45:19

如果您有多个同名的客户条目,您必须为给定名称选择一个 ID。以下是选择最近创建的(即 max())的示例,但您可能想要第一个创建的(即 min()):

SELECT  
    Max([id]) as id,
    COALESCE ([Company], [LastName] + ', ' + [FirstName]) as Customer
FROM [some_database].[dbo].[some_table]
GROUP BY 2
ORDER BY Customer

已编辑:< /strong> 抱歉... GROUP BY 2,而不是 1

If you have multiple customer entries with the same name, you have to pick one ID for a given name. Here's an example of picking the most recently created (ie max()), but you may want the first created (ie min()):

SELECT  
    Max([id]) as id,
    COALESCE ([Company], [LastName] + ', ' + [FirstName]) as Customer
FROM [some_database].[dbo].[some_table]
GROUP BY 2
ORDER BY Customer

EDITED: Sorry... GROUP BY 2, not 1

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