sql根据非主键字段去重

原sql:

SELECT *
FROM Sys_TaskAndLTI
WHERE Term_ID = '27426f87-5cd2-4f4d-8153-ed9a71db860a'
AND Group_ID = '49e166de-7921-414e-bb91-5e42c19feffe'
AND Group_Info_ID = '4b3f6a77-e87c-499a-bfc7-8326a4c7f2ce'

查询结果为:

sql根据非主键字段去重_第1张图片

需要根据Task_ID去重,但Task_ID为非主键字段,修改后的sql为:

SELECT MAX(aa.TaskAndLTI_ID) TaskAndLTI_ID,MAX(aa.Group_Info_ID) Group_Info_ID,MAX(aa.Group_ID) Group_ID,
MAX(aa.TaskAndLTI_Content) TaskAndLTI_Content,MAX(aa.LP_Hour_ID) LP_Hour_ID,MAX(aa.CreatTime) CreatTime,
MAX(aa.Path) Path, MAX(aa.CompoundState) CompoundState,MAX(aa.Uptime) Uptime,MAX(aa.Term_ID) Term_ID,
MAX(aa.Subject_ID) Subject_ID,MAX(aa.Plate_ID) Plate_ID,MAX(aa.Task_ID) Task_ID,MAX(aa.MergeNumber) MergeNumber,
MAX(aa.Reserve) Reserve,MAX(aa.TaskAndLTI_Title1) TaskAndLTI_Title1,MAX(aa.TaskAndLTI_Title2) TaskAndLTI_Title2,
MAX(aa.VideoPath) VideoPath,
MAX(aa.PathHtml) PathHtml,
MAX(aa.LocalFileName) LocalFileName,
--MAX(aa.HtmlContent) HtmlContent,
MAX(CONVERT(NVARCHAR(1000), aa.HtmlContent)) HtmlContent,
--MAX(aa.RejectReason) RejectReason,
--MAX(aa.Reflect) Reflect,
MAX(aa.OverRecord) OverRecord,
MAX(aa.FinalsubTime) FinalsubTime,
MAX(aa.TextSubState) TextSubState,MAX(aa.VideoSubState) VideoSubState,MAX(aa.MicroSubState) MicroSubState,MAX(aa.ReflectState) ReflectState
FROM 
	(SELECT *
	FROM Sys_TaskAndLTI
	WHERE Term_ID = '27426f87-5cd2-4f4d-8153-ed9a71db860a'
		AND Group_ID = '49e166de-7921-414e-bb91-5e42c19feffe'
		AND Group_Info_ID = '4b3f6a77-e87c-499a-bfc7-8326a4c7f2ce') aa 
		group by aa.Task_ID 
		having count(aa.Task_ID)>=1

其中sql执行报错:操作数数据类型 text 对于 max 运算符无效。

则将

MAX(aa.HtmlContent) HtmlContent

修改为:
MAX(CONVERT(NVARCHAR(1000), aa.HtmlContent)) HtmlContent,

你可能感兴趣的:(数据库)