zoukankan      html  css  js  c++  java
  • sql server2012学习笔记

     第2章 T-SQL语句 

     

    实际工作中少用*,因为可能会出现成千上万的数据

    select Top 100 * from [Production].[Product]  选择前一百行数据

    select ProductID, Name, ProductNumber, Color, Size, ListPrice
    from Production.Product

    按信息读取数据

    select ProductID, Name, ProductNumber, Color, Size, ListPrice
    from Production.Product
    order by listprice desc  //安装listprice倒序排序

    asc=ascending order  //正序排序

    第一个按照listprice倒序排序,第二个按照listprice和Name倒序排序

    按照第2个Name正序排序

    isnull(size,'')  空值null变换为单引号‘’里面的数据

    改变列的名称

    select ProductID, Name as ProductName, --using an alias
    'The list price for ' + ProductNumber + ' is $ ' + convert(varchar,ListPrice) +'.' ,--using the concatenation to join character end-to-end.
    'The list price for ' + ProductNumber + ' is $ ' + convert(varchar,ListPrice) +'.' as [Description] --using brackets to let SQL server conside the strin as a column name
    from Production.Product

    在where语句中用>,=,<等字符

    eg:

    select * from [Sales].[SalesOrderHeader]
    where SalesPersonID=275

    select * from [Sales].[SalesOrderHeader]
    where SalesOrderNumber='so43670'  //string类型加单引号

    where语句中使用or或and

    eg:

    select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
    from [Sales].[SalesOrderHeader]
    where SalesPersonID=275 and TotalDue>5000 and Orderdate between '2005-08-01' and '1/1/2006'

    like中使用%号  //%表示可以有,可以没有

    select * from [Production].[Product]
    where name like'Mountain'

    select * from [Production].[Product]
    where name like'%Mountain%' --Wildcard % matches any zero or more characters

    _下划线表示前面有一个不知道的字符

    select * from [Production].[Product]
    where name like'_ountain%'

    Where语句中使用in或not in

    select * from [Production].[Product]
    where color in ('red','white','black')

    select * from [Production].[Product]
    where class not in ('H') -- same as using: <> 'H'//没有H的

    is null 与is not null

    --Topic 10
    select * from [Production].[Product]
    where size is null

    select * from [Production].[Product]
    where size is not null

    or与and的理解

    --Topic 11
    select * from [Production].[Product]
    where color ='white'or color ='black'

    select * from [Production].[Product]
    where color ='white'and color ='black'

    select SalesOrderID,OrderDate,SalesPersonID,TotalDue as TotalSales
    from [Sales].[SalesOrderHeader]
    where (SalesPersonID=275 or SalesPersonID=278) and TotalDue>5000

    工作中常用的聚合函数&经典查询语句实例

    select count(SalesPersonID)     //count--总和
    from [Sales].[SalesOrderHeader]
    where SalesPersonID is not null

    select distinct(SalesPersonID)    //l列出
    from [Sales].[SalesOrderHeader]
    where SalesPersonID is not null

    select count(distinct(SalesPersonID))  //数目
    from [Sales].[SalesOrderHeader]
    where SalesPersonID is not null

    --Topic 13
    select
    Avg(TotalDue) as AverageTotalSales --aggregate functions
    from [Sales].[SalesOrderHeader]

    select
    Avg(TotalDue) as AverageTotalSales
    ,Min(TotalDue) as MinimumTotalSales
    ,Max(TotalDue) as MaximumTotalSales
    ,Sum(TotalDue) as SummaryTotalSales
    from [Sales].[SalesOrderHeader]

    select SalesPersonID,OrderDate,Max(TotalDue) as MaximumTotalSales
    from [Sales].[SalesOrderHeader]
    where SalesPersonID is not null
    group by SalesPersonID,OrderDate
    having Max(TotalDue)>150000  //大于15万的提取出来
    order by SalesPersonID

    ----The classical T-SQL query!!!
    select SalesPersonID,OrderDate,Max(TotalDue) as MaximumTotalSales
    from [Sales].[SalesOrderHeader]
    where SalesPersonID is not null and OrderDate >='2007/1/1'
    group by SalesPersonID,OrderDate
    having Max(TotalDue)>150000
    order by SalesPersonID
    --order by OrderDate desc

     

    我爱学习,学习使我快乐。
  • 相关阅读:
    Python 学习笔记 11.模块(Module)
    Python 学习笔记 8.引用(Reference)
    Python 学习笔记 9.函数(Function)
    Python 学习笔记 6.List和Tuple
    Python 学习笔记 4.if 表达式
    Python 学习笔记 2.自省
    Python 学习笔记 3.简单类型
    Python 学习笔记 7.Dictionary
    Python 学习笔记 5.对象驻留
    Python 学习笔记 10.类(Class)
  • 原文地址:https://www.cnblogs.com/kerven/p/6551547.html
Copyright © 2011-2022 走看看