sql server数据库,假如有有一张表有两列,ID和姓名,这个排序规则是这样的,当插入姓名与表中原有数据不重复的情况下,会按照ID大小进行排序。如果插入的姓名与原表格数据相同,则按照姓名,ID进行排序,这个怎么写?
select * from tb
${if(count(sql("FRDemo","select DISTINCT 姓名 from tb",1,1))=count(sql("FRDemo","select 姓名 from tb",1,1)),"order by ID","order by 姓名,ID")}
根据去重后长度是否相等判断是否有重复值
我想简单了?
SELECT * FROM TABLE ORDER BY 姓名,ID
------------
with a as (
select 1 [id],'张三' [姓名]
union ALL
select 2 [id],'李四' [姓名]
select 3 [id],'王五' [姓名]
select 4 [id],'赵六' [姓名]
select 5 [id],'王五' [姓名]
select 6 [id],'李四' [姓名]
select 7 [id],'张三' [姓名]
)
SELECT * FROM (
SELECT A.*,B.id [NEW_] FROM A INNER JOIN (
SELECT 姓名,MIN(id) [id] FROM A group by 姓名 ) B ON A.姓名=B.姓名
) T ORDER BY NEW_,姓名,id
case when??
select * from 表 case when A then order by AA else order by BB end
另外在填报的时候,判断一下是否重复,往数据库字段flag里面打个标记