1

我在执行时收到 sql server 错误“文本 ntext 和图像数据类型无法比较或排序,除非使用为 null 或类似”

SELECT r.Shift, l.lab_title AS [Assigned Lab], u.user_name AS [L/A],
       CONVERT(varchar(100), t.Time) + '  To ' + CONVERT(varchar(100), h.Time) AS Timing
  FROM table_roaster_time_table AS r
       INNER JOIN table_time AS t ON r.timeId = t.timeId AND r.timeId = t.timeId
       INNER JOIN table_user AS u ON r.user_id = u.user_id
       INNER JOIN table_labs AS l ON r.lab_id = l.lab_id
       INNER JOIN table_time2 AS h ON r.timeId2 = h.timeId2
 GROUP BY r.Shift, l.lab_title, u.user_name

不知道是什么问题。我也使用了聚合函数,只是为了形式,但它不起作用。

4

1 回答 1

2

I'm guessing your r.Shift column is a text or ntext type. Because of this it cannot be used in a group by

One option might be to use a CAST on the suspect column, but you might loose data. Example:

SELECT        r.Shift, l.lab_title AS [Assigned Lab], u.user_name AS [L/A], CONVERT(varchar(100),     t.Time) + '  To ' + CONVERT(varchar(100), h.Time) AS Timing
FROM            table_roaster_time_table AS r INNER JOIN
                     table_time AS t ON r.timeId = t.timeId AND r.timeId = t.timeId INNER JOIN
                     table_user AS u ON r.user_id = u.user_id INNER JOIN
                     table_labs AS l ON r.lab_id = l.lab_id INNER JOIN
                     table_time2 AS h ON r.timeId2 = h.timeId2
GROUP BY CAST(r.Shift as varchar(max), l.lab_title, u.user_name

You could also look at querying the data without the text column and then doing a second query to get the missing column. Example:

with temp as
(
    SELECT        l.lab_title AS [Assigned Lab], u.user_name AS [L/A], CONVERT(varchar(100),     t.Time) + '  To ' + CONVERT(varchar(100), h.Time) AS Timing
    FROM            table_roaster_time_table AS r INNER JOIN
                         table_time AS t ON r.timeId = t.timeId AND r.timeId = t.timeId INNER JOIN
                         table_user AS u ON r.user_id = u.user_id INNER JOIN
                         table_labs AS l ON r.lab_id = l.lab_id INNER JOIN
                         table_time2 AS h ON r.timeId2 = h.timeId2
    GROUP BY l.lab_title, u.user_name
)
SELECT   r.Shift, t.lab_title AS [Assigned Lab], t.user_name AS [L/A] --etc
 FROM            table_roaster_time_table AS r 
 INNER JOIN temp as t
on  r.id = t.id --or whatver

Also, this SQL seems unnecessary:

r.timeId = t.timeId AND r.timeId = t.timeId
于 2012-09-17T20:18:55.247 回答