场景:sql server 2008 drop table ID CREATE TABLE ID ( id ,) not null, code int , D date, PRIMARY KEY (id) ) ,getdate()) ,getdate()) ,getdate()) ,getdate()) ,'2017-08-02') ,'2017-08-01') select * from ID 目标: select COUNT(*) from ID group by code 产生code列唯
select * from ( select last_comment, row_number() over(partition by employeeid,roadline,stationname order by logindate desc) rn from reocrd ) t where t.rn <=1 这段的意思是,将reocrd表根据员工工号( employeeid),线路(,roadline),站点名称(stationname)分组后,取登录日期(logindate) 最大的那
select gr,num,dt,(select bys from test where gr=b.gr and dt=b.dt) bys from ( select gr,count(0) num,max(dt) dt from test group by gr ) b //如果有重复项,可用如下语句(针对Mysql的limit,Oracle 可用 rownum<2) select gr,num,dt,(select bys from test where gr=b.gr and dt=b.d
获取分组后取某字段最大一条记录 方法一:(效率最高) select * from test as a where typeindex = (select max(b.typeindex) from test as b where a.type = b.type ); 方法二:(效率次之) select a.* from test a, (select type,max(typeindex) typeindex from test group by type) b where a.type = b
django获取指定列的数据 model一般都是有多个属性的,但是很多时候我们又只需要查询特定的某一个,这个时候可以用到values和values_list [values()](https://docs.djangoproject.com/en/1.9/ref/models/querysets/#values) values()¶ values(*fields)¶ Returns a QuerySet that returns dictionaries, rather than model i
1.取时间最新的记录 不分组有重复(多条CreateTime一样的都是最新记录) select * from test t where pid in ( select PId from Test t where time=(select max(time) from Test t1 where t1.PId=t.PId) group by Pid ) and time=(select max(time) from Test t1 where t1.PId=t.PId) 2.分组后取时间最新的记录
-- 根据编号分组取第一条数据 select * from table t where t.no=(select max(no) from table t1 where t1.no=t.no) -- 根据编号分组后取第一条数据 SELECT * FROM (SELECT ROW_NUMBER() OVER (partition BY no ORDER BY no) rowId,* from table) t WHERE rowId=1
/* 分组之后拼接字符串 */ ;with t as( SELECT WorkflowId,Remark FROM dbo.OperatorAutomationProcess GROUP BY WorkflowId,Remark ) , H AS( SELECT WorkflowId, -- 分组的主键 STUFF( ( SELECT '_'+ Remark -- 要拼接的列 FROM t a WHERE b.WorkflowId = a.WorkflowId FOR XML PATH('')
CREATE TABLE students (course varchar(10), stu_name varchar(10), city varchar(10), score int ) insert into students values('数学','Jack','Tianjin',80); insert into students values('数学','Jordan','Tianjin',80); insert into students values('数学','James','B