不同的 SQL 语句错误

发布于 2024-12-01 11:34:00 字数 480 浏览 0 评论 0原文

我正在尝试执行一个语句,仅根据 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 技术交流群。

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

发布评论

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

评论(4

破晓 2024-12-08 11:34:00

您的选择查询中可能有一个 textntext 类型的列,这是 distinct 不允许的。将列类型更改为 varchar(max)。或者直接在查询中进行转换

SELECT DISTINCT 
  Name, 
  CreatedDate, 
  clientId__c, 
  completedDate__c, 
  Legacy_Call_ID__c, 
  CAST(Description_Short__c AS VarChar(max)) --Assuming that is the Text/nText column
FROM Customers.dbo.Incident__c
WHERE Description_Short__c LIKE 'New Hire%'
  OR Description_Short__c LIKE 'Term%'

编辑

不完全确定,但类似这样的方法可能会起作用:

 SELECT 
        Name, 
        CreatedDate, 
        clientId__c, 
        completedDate__c, 
        Legacy_Call_ID__c, 
        Description_Short__c
    FROM 
        Customers.dbo.Incident__c As tblOuter
    WHERE
        EXISTS(
            SELECT
                NULL
            FROM 
                Customers.dbo.Incident__c as tblInner
            WHERE 
                (tblInner.Description_Short__c LIKE 'New Hire%' OR
                tblInner.Description_Short__c LIKE 'Term%') AND
                tblInner.Legacy_Call_ID__c = tblOuter.Legacy_Call_ID__c
            GROUP BY
                tblInner.Legacy_Call_ID__c
            HAVING
                MAX(tblInner.CreatedDate) = tblOuter.CreatedDate
            )

You probably have a column of type text or ntext in your select query, that is not allowed with distinct. Change the column type to varchar(max) instead. Or cast it directly in the query

SELECT DISTINCT 
  Name, 
  CreatedDate, 
  clientId__c, 
  completedDate__c, 
  Legacy_Call_ID__c, 
  CAST(Description_Short__c AS VarChar(max)) --Assuming that is the Text/nText column
FROM Customers.dbo.Incident__c
WHERE Description_Short__c LIKE 'New Hire%'
  OR Description_Short__c LIKE 'Term%'

EDIT

Not completly sure but something like this might work:

 SELECT 
        Name, 
        CreatedDate, 
        clientId__c, 
        completedDate__c, 
        Legacy_Call_ID__c, 
        Description_Short__c
    FROM 
        Customers.dbo.Incident__c As tblOuter
    WHERE
        EXISTS(
            SELECT
                NULL
            FROM 
                Customers.dbo.Incident__c as tblInner
            WHERE 
                (tblInner.Description_Short__c LIKE 'New Hire%' OR
                tblInner.Description_Short__c LIKE 'Term%') AND
                tblInner.Legacy_Call_ID__c = tblOuter.Legacy_Call_ID__c
            GROUP BY
                tblInner.Legacy_Call_ID__c
            HAVING
                MAX(tblInner.CreatedDate) = tblOuter.CreatedDate
            )
陈甜 2024-12-08 11:34:00

由于格式原因,无法选择其中一个字段。请尝试

CONVERT(nvarchar, Name) 

在 select 语句中使用 a 。 IE->

SELECT DISTINCT CONVERT(nvarchar, Name) as 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%'

将 nvarchar 更改为它应该是的任何字段。和它一起玩,你最终就会让它走起来。这解决了我选择的问题,如果它解决了您的问题,请添加代表。

One of your fields can't be selected because of the format they are in. Try using a

CONVERT(nvarchar, Name) 

in your select statement. IE->

SELECT DISTINCT CONVERT(nvarchar, Name) as 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%'

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.

故事与诗 2024-12-08 11:34:00

请参阅下面的查询。当您这样做时,您应该考虑将数据类型永久更改为 varchar(max) 而不是文本。

SELECT DISTINCT Name, CreatedDate, clientId__c, completedDate__c
  , Legacy_Call_ID__c, convert(varchar(max), Description_Short__c)
  FROM Customers.dbo.Incident__c
  WHERE convert(varchar(max), Description_Short__c) LIKE 'New Hire%'
    OR convert(varchar(max), Description_Short__c) LIKE 'Term%'

See query below. While you're at it, you should look into permanently changing the datatype to varchar(max) instead of text.

SELECT DISTINCT Name, CreatedDate, clientId__c, completedDate__c
  , Legacy_Call_ID__c, convert(varchar(max), Description_Short__c)
  FROM Customers.dbo.Incident__c
  WHERE convert(varchar(max), Description_Short__c) LIKE 'New Hire%'
    OR convert(varchar(max), Description_Short__c) LIKE 'Term%'
幸福不弃 2024-12-08 11:34:00

根据legacy_call_id__c字段选择不同的记录

您的方式使用distinct 将为您提供所有组合列的不同值,而不仅仅是legacy_call_id__c。您可以使用 row_number() 函数获取某一字段上具有不同值的行。在这里,您将获得每个 legacy_call_id__cCreateDate 最高值的行。

SELECT Name, 
       CreatedDate, 
       clientId__c, 
       completedDate__c, 
       Legacy_Call_ID__c, 
       Description_Short__c
FROM (       
      SELECT Name, 
             CreatedDate, 
             clientId__c, 
             completedDate__c, 
             Legacy_Call_ID__c, 
             Description_Short__c,
             row_number() OVER(
                               PARTITION BY Legacy_Call_ID__c 
                               ORDER BY CreatedDate DESC
                              ) AS rn
      FROM Customers.dbo.Incident__c
      WHERE Description_Short__c LIKE 'New Hire%'
        OR Description_Short__c LIKE 'Term%'
     ) AS T
WHERE T.rn = 1

select distinct records based on the legacy_call_id__c field

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 the row_number() function to get rows with distinct on one field. Here you will get the rows for the highest value of CreateDate for each legacy_call_id__c.

SELECT Name, 
       CreatedDate, 
       clientId__c, 
       completedDate__c, 
       Legacy_Call_ID__c, 
       Description_Short__c
FROM (       
      SELECT Name, 
             CreatedDate, 
             clientId__c, 
             completedDate__c, 
             Legacy_Call_ID__c, 
             Description_Short__c,
             row_number() OVER(
                               PARTITION BY Legacy_Call_ID__c 
                               ORDER BY CreatedDate DESC
                              ) AS rn
      FROM Customers.dbo.Incident__c
      WHERE Description_Short__c LIKE 'New Hire%'
        OR Description_Short__c LIKE 'Term%'
     ) AS T
WHERE T.rn = 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文