分组中的第一条:
select * from
(
select row_number() over(partition by 列1,列2,... order by 列1,列2,...) as rownum -- 排序并分组
, * -- 所需显示的字段
from 表
) as T
where T.rownum = 1 显示聚合以外的列:
SELECT  a.examroomnum ,
         a.positionnum ,
         a.人数 ,
         a.suminterviewscore ,
         a.avginterviewscore ,
         b.Department
 FROM    SELECT    examroomnum ,
                     positionnum ,
                     COUNT(*) AS 人数 ,
                     SUM(interviewscore) suminterviewscore ,
                     AVG(interviewscore) avginterviewscore
           FROM      examinee
           GROUP BY  examroomnum ,
                     positionnum
         ) a
         LEFT JOIN examinee b ON a.examroomnum = b.examroomnum
                                 AND a.positionnum = b.positionnum
 
having字句的使用:

HAVING语句通常与GROUP BY语句联合使用,用来过滤由GROUP BY语句返回的记录集。

HAVING语句的存在弥补了WHERE关键字不能与聚合函数联合使用的不足。

语法:

SELECT column1, column2, ... column_n, aggregate_function (expression)FROM tablesWHERE predicatesGROUP BY column1, column2, ... column_nHAVING condition1 ... condition_n;

同样使用本文中的学生表格,如果想查询平均分高于80分的学生记录可以这样写:

SELECT id, COUNT(course) as numcourse, AVG(score) as avgscore

FROM student

GROUP BY id

HAVING AVG(score)>=80;

在这里,如果用WHERE代替HAVING就会出错。

使用示例************************************************************************************

计算每种物料入库总数大于当前库存的最早一笔入库的时间

select productid,inamount,DATEDIFF(day,operatedate,getdate()) from(
select row_number() over(partition by productid order by operatedate) as rownum,*
from (select p.productid,p.inamount,k.operatedate from
(select productid,isnull(sum(amount),0) as inamount from P_ProductBillSumTab a with(nolock)
where tagid in ('1101','1102','1105','1106') group by productid having isnull(sum(amount),0)>(select sum(storage) as storate from p_productbatchtab b with(nolock)
where productid=a.productid)) p
left join P_ProductBillSumTab k with(nolock) on k.productid=p.productid) as mm) as T
where T.rownum = 1 order by operatedate

****************************************************************************************************

select *,DATEDIFF(day,t.operatedate,getdate()) from (
(select row_number() over(partition by productid order by samount) as rownum,* from(
select aa.*,c.storage from (
select productid,amount,operatedate,
(select SUM(amount) from product_inlist b where b.productid=a.productid and b.id>=a.id) as samount
from product_inlist a)aa
left join product_storage c on c.productid=aa.productid
where samount>c.storage
) k )) as T where T.rownum = 1
order by operatedate desc

sql 选取分组中的第一条,显示聚合以外的列,having字句的使用的更多相关文章

  1. linq中分组查询而且获取每个分组中的第一条记录,数据用于分页绑定

    LINQ分组取出第一条数据 Person1: Id=1, Name="Test1" Person2: Id=1, Name="Test1" Person3: I ...

  2. sql 选取每个分组中的第一条数据

    --1.创建测试表Create Table #Order1( OrderName varchar(50), RequestDate datetime, OrderCount int)-- 插入测试数据 ...

  3. 160804、oracle查询:取出每组中的第一条记录

    oracle查询:取出每组中的第一条记录按type字段分组,code排序,取出每组中的第一条记录 方法一: select type,min(code) from group_info group by ...

  4. [MSSQL]找出一天数据中从第一条数据开始每累加1小时的数据

    用Sql Server找出一天数据中从第一条数据开始每累加1小时的数据 -- ============================================= -- Author: Alle ...

  5. postgresql分组后获取第一条数据

    -- 根据编号分组取第一条数据 select * from table t where t.no=(select max(no) from table t1 where t1.no=t.no) -- ...

  6. [转帖]SQL Server 索引中include的魅力(具有包含性列的索引)

    SQL Server 索引中include的魅力(具有包含性列的索引) http://www.cnblogs.com/gaizai/archive/2010/01/11/1644358.html 上个 ...

  7. sql语句查询表中重复字段以及显示字段重复条数

    今天跟大家分享两条SQL语句,是关于查询某表中重复字段以及显示该字段的重复条数. 1.select * from 表名 where 列名 in (select 列名 from 表名 group by ...

  8. SQL 查询每组的第一条记录

    CREATE TABLE [dbo].[test1]( [program_id] [int] NULL, [person_id] [int] NULL ) ON [PRIMARY] /*查询每组分组中 ...

  9. Sqlserver 如何获取每组中的第一条记录

    在日常生活方面,我们经常需要记录一些操作,类似于日志的操作,最后的记录才是有效数据,而且可能它们属于不同的方面.功能下面,从数据库的术语来说,就是查找出每组中的一条数据. 例子 我们要从上面获得的有效 ...

随机推荐

  1. 升级AndroidStudio3.4问题汇总

    1.Could not get unknown property 'bootClasspath' for object of type org.gradle.api.tasks.compile.Com ...

  2. (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  3. 利用java内部静态类实现懒汉式单例

    /** * @Description: 利用键值模式控制service * @Author: zhanglifeng * @Date: 2019年 04月 28日 14:41 **/ public c ...

  4. python验证卡普耶卡(D.R.Kaprekar)6174猜想

    1955年,卡普耶卡(D.R.Kaprekar)对4位数字进行了研究,发现一个规律: 对任意各位数字不相同的4位数,使用各位数字能组成的最大数减去能组成的最小数,对得到的差重复这个操作,最终会得到61 ...

  5. Springboot集成Quartz

    之前学习过spring的定时任务 :https://www.cnblogs.com/slimshady/p/10112515.html 本文主要学习记录下springboot使用quartz 1.   ...

  6. Tomcat系列(1)——Tomcat安装配置

    核心步骤 1. 安装JAVA(因为tomcat依赖于java) 配置:JAVA_HOME D:\Program Files (x86)\Java\jdk1.7.0 path  %JAVA_HOME%\ ...

  7. [源码分析]读写锁ReentrantReadWriteLock

    一.简介 读写锁. 读锁之间是共享的. 写锁是独占的. 首先声明一点: 我在分析源码的时候, 把jdk源码复制出来进行中文的注释, 有时还进行编译调试什么的, 为了避免和jdk原生的类混淆, 我在类前 ...

  8. DirectX11--实现一个3D魔方(3)

    前言 (2019/1/9 09:23)上一章我们主要讲述了魔方的旋转,这个旋转真是有毒啊,搞完这个部分搭键鼠操作不到半天应该就可以搭完了吧... (2019/1/9 21:25)啊,真香 有人发这张图 ...

  9. addEventListener解决多个window.onscroll共存的2个方法

    方法1.(注意第一个和第二个的先后次序) window.onscroll=function(){console.log('第一个');} var oldMethod = window.onscroll ...

  10. luogu P5324 [BJOI2019]删数

    传送门 不如先考虑暴力,能删的序列首先有\(1,2,3...n\),还有就是升序排序后从后往前放数,第\(i\)位要么放\(i\),要么放\(i+1\)位置的数,例如\(1,2,4,4,5,6,9,9 ...