6.select_top_查询表顶部多少记录

USE SQLSERVER;
SELECT * FROM EMP;

--筛选顶部的记录或列的记录
--top关键字

--备注:目前不会讲分页,因为子句还没学

--查询表中前5个条数据
    select top 5 * from emp;

--查询数据库中35%前的记录数
    select top 35 percent * from emp;

    select top 15 percent * from emp;

--查询comm不重复数据的前三条记录
    select distinct top 3 comm from emp;

--查询部门,comm不重复数据的前三条记录
    select distinct top 3 deptno, comm from emp;


--错误的搭配
--select distinct top 10 deptno from emp;
--select top 10 distinctdeptno from emp;
--distinct修饰的是整个select查找的结果集

--SELECT语法
SELECT [ ALL | DISTINCT ] [ topSubclause ] aliasedExpr 
       [{ , aliasedExpr }] FROM fromClause [ WHERE whereClause ] [ GROUP BY groupByClause [ HAVING havingClause ] ] [ ORDER BY orderByClause ]


/*
    注意:
        percent:                    是百分比 和 前面值合起来 取上限
        top和distinct一起使用时:    distinct修饰的是结果集,而top修饰结果集的前几条记录
    
    语法:
        select top VALUE (percent) * from table;

    语意:
        select top VALUE (percent) * from table;        
        来自 table 中 顶部记录VALUE条    数据, 进行显示

        例如:
            select distinct top 30 percent comm, deptno from emp;
        含义:
            查询 唯一 来自 emp 中 comm字段, 最后将 顶部的字段 30%(取上限) 进行输出
            
            来自 emp 表 查询 不重复comm, deptno字段组合 前 30%(取上限) 
    
    顺序的问题:
        select 
                3.    distinct(字段组合 F1, F2... )
                4.    top NUMBER percent F1, F2... 
                1.    from TABLE
                2.    where 条件....
    
*/        
                

 

posted @ 2016-07-04 13:53  Mr_Target  阅读(986)  评论(0编辑  收藏  举报