不同的 SQL 语句错误
我正在尝试执行一个语句,仅根据 legacy_call_id__c
字段选择不同的记录,但我不知道该怎么做。我收到错误,
消息 8163,级别 16,状态 3,第 1 行 text、ntext 或图像数据 类型不能选择为 DISTINCT。”
SELECT DISTINCT Name, CreatedDate, clientId__c, completedDate__c
, Legacy_Call_ID__c, Description_Short__c
FROM Customers.dbo.Incident__c
WHERE Description_Short__c LIKE 'New Hire%'
OR Description_Short__c LIKE 'Term%'
如果这有影响的话,我正在使用 SQL Server 2008R2。谢谢!
I'm trying to do a statement to only select distinct records based on the legacy_call_id__c
field, but I'm not sure how to do that. I am getting an error,
Msg 8163, Level 16, State 3, Line 1 The text, ntext, or image data
type cannot be selected as DISTINCT."
SELECT DISTINCT Name, CreatedDate, clientId__c, completedDate__c
, Legacy_Call_ID__c, Description_Short__c
FROM Customers.dbo.Incident__c
WHERE Description_Short__c LIKE 'New Hire%'
OR Description_Short__c LIKE 'Term%'
I'm using SQL Server 2008R2 if that makes a difference. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的选择查询中可能有一个
text
或ntext
类型的列,这是distinct
不允许的。将列类型更改为varchar(max)
。或者直接在查询中进行转换编辑
不完全确定,但类似这样的方法可能会起作用:
You probably have a column of type
text
orntext
in your select query, that is not allowed withdistinct
. Change the column type tovarchar(max)
instead. Or cast it directly in the queryEDIT
Not completly sure but something like this might work:
由于格式原因,无法选择其中一个字段。请尝试
在 select 语句中使用 a 。 IE->
将 nvarchar 更改为它应该是的任何字段。和它一起玩,你最终就会让它走起来。这解决了我选择的问题,如果它解决了您的问题,请添加代表。
One of your fields can't be selected because of the format they are in. Try using a
in your select statement. IE->
Change nvarchar to whatever field it should be. Play with it and you will get it to go eventually. This solved my select issues, add rep if it solves yours.
请参阅下面的查询。当您这样做时,您应该考虑将数据类型永久更改为 varchar(max) 而不是文本。
See query below. While you're at it, you should look into permanently changing the datatype to varchar(max) instead of text.
您的方式使用distinct 将为您提供所有组合列的不同值,而不仅仅是
legacy_call_id__c
。您可以使用row_number()
函数获取某一字段上具有不同值的行。在这里,您将获得每个legacy_call_id__c
的CreateDate
最高值的行。Using distinct the way you do will give you the distinct values for all columns combined, not only for
legacy_call_id__c
. You can use therow_number()
function to get rows with distinct on one field. Here you will get the rows for the highest value ofCreateDate
for eachlegacy_call_id__c
.