分组中的第一条:
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. python第十二天, 三元表达式, 函数对象,名称空间与作用域,函数的嵌套定义

    复习 1. 字符串的比较: 2. 函数的参数:形参与实参 3. 实参的分类:位置实参与关键字实参 4. 形参分类: 1.无值位置形参 2.有值位置形参 3.可变长位置形参 4.有无值关键字形参 5.可 ...

  2. (3)润写一个程序(类),用户输入一段英文,然后输出这段英文中所有长度为3个字母的单词井且如果单词如果有连续 复了2次,只输出一个【例: This isis a desk,程序输出 his is a desk】,(提示,有re正则匹配来做)

    import re x = input('Please input a string:') pattern = re.compile(r'\b[a-zA-Z]{3}\b') print(pattern ...

  3. Gym - 101982C Contest Setting (动态规划)

    A group of contest writers have written n problems and want to use k of them in an upcoming contest. ...

  4. 支持向量机SVM原理_python sklearn建模乳腺癌细胞分类器(推荐AAA)

    项目合作联系QQ:231469242 sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?cours ...

  5. NameNode和SecondaryNameNode工作原理剖析

    NameNode和SecondaryNameNode工作原理剖析 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.NameNode中的元数据是存储在那里的? 1>.首先,我 ...

  6. Mysql查询库、表存储量(Size)

    Mysql查询库.表存储量(Size) 1.要查询表所占的容量,就是把表的数据和索引加起来就可以了. SELECT SUM(DATA_LENGTH) + SUM(INDEX_LENGTH) FROM ...

  7. 第4章学习小结_串(BF&KMP算法)、数组(三元组)

    这一章学习之后,我想对串这个部分写一下我的总结体会. 串也有顺序和链式两种存储结构,但大多采用顺序存储结构比较方便.字符串定义可以用字符数组比如:char c[10];也可以用C++中定义一个字符串s ...

  8. CentOS下TensorFlow安装命令

    安装扩展源:sudo yum -y install epel-release 安装python-pip模块:sudo yum install python-pip 直接把whl文件download下来 ...

  9. 在可编辑div的光标下插入html

    function pasteHtmlAtCaret(html, selectPastedContent) {//参数1为要插入的html //参数2为boolean 是否选中插入的html 默认为fa ...

  10. java学习笔记02-Eclipse IDE配置

    Eclipse是java的ide工具,ide是集成开发工具.实际开发中都需要使用ide工具.eclipse下载 java代码在运行时,是需要先进行编译,然后才能运行.可以直接在IDE工具里运行,它会帮 ...