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.
但是,我想使用独特的关键字来消除重复的“客户”条目。这是可能的吗?我试过几种不同的方法,但都没有用。
1 个解决方案
#1
5
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()
):
如果您有多个同名的客户条目,则必须为给定名称选择一个ID。这里有一个选择最近创建的(ie max()))的示例,但是您可能希望第一个创建(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
编辑:对不起……一组2,不是1
#1
5
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()
):
如果您有多个同名的客户条目,则必须为给定名称选择一个ID。这里有一个选择最近创建的(ie max()))的示例,但是您可能希望第一个创建(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
编辑:对不起……一组2,不是1