sqlserver数据列如何判断每天的最大值
表中列:ID、DateTime、Date、XXX、Mark DataTime列 就是 DateTime时间格式 Date列 为当天日期的8位数字字符串(如:20130925) XXX 为每天从第一条数据到最后一条数据的序号,从1开始 ID = Date+XXX(如:20130925001、20130925002、20130925003)
问题:用什么语句给每天最大XXX这行数据标记 Mark 值为 1
知道有个这:max(XXX) over (partition by Date) 但是不知道怎么用,还有什么方法?
--方法一 ;with Tmp as ( select Date,max(ID) as MaxID from [Table] group by Date) update [Table] set Mark = 1 from [Table] A inner join Tmp B ON A.Date = B.Date AND A.id = B.MaxID --方法二 ;with Tmp as ( select row_number() over (partition by Date order by XXX desc ) as Row Date,ID from [Table] ) update [Table] set Mark = 1 from [Table] A inner join Tmp B ON A.Date = B.Date AND A.id = B.MaxID and b.Row = 1
--最简单的 select right(convert(varchar(16),max(ID)),len(convert(varchar(16),max(ID))) -8) as MaxID from [Table] group by Date
|