第二章 单表查询

T-SQL

use TSQL2012

--Order by

select empid, firstname, lastname, country

from HR.Employees

order by hiredate

--Top

select top(5) orderid, orderdate, custid, empid

from sales.orders

order by orderdate desc;

--Top Percent

select top(1) percent orderid, orderdate, custid, empid

from sales.orders

order by orderdate desc;

--Top With Ties

select top(5) with ties orderid, orderdate, custid, empid

from sales.orders

order by orderdate desc;

--Over Partition By

Select orderid,

custid,

val,

sum(val) over() as totalvalue,

sum(val) over(partition by custid) as custtotalvalue,

count(custid) over(partition by custid) as custtotalorders

from sales.ordervalues;

--Over

select orderid, custid, val,

100. * val / sum(val) over() as pctall,

100. * val / sum(val) over(partition by custid) as pctcust

from sales.ordervalues;

--Over Row_Number Rank Dense_Rank Ntile

select top(10) orderid, custid, val,

Row_Number() over(order by val) as rownum,

rank() over(order by val) as val_rank,

dense_rank() over(order by val) as val_dense_rank,

ntile(300) over(order by val) as val_ntile

from sales.ordervalues

order by val;

--Over Row_Number

select orderid, custid, val,

row_Number() over(partition by custid order by val) as rownum

from sales.ordervalues

order by custid, val;

--Distinct Over

select distinct val, row_number() over(order by val) as rownum

from sales.ordervalues;

--group by over

select val, row_number() over(order by val) as rownum

from sales.ordervalues

group by val;

--In

select orderid, empid, orderdate

from sales.orders

where orderid in (10248, 10249, 10250);

--Between

select orderid, empid, orderdate

from sales.orders

where orderid between 10300 and 10310;

--Like

Select empid, firstname, lastname

from hr.employees

where lastname like N'D%'

--*

select orderid, productid, qty, unitprice, discount,

qty*unitprice*(1-discount) as val

from sales.orderdetails

--Case

select productid, productname, categoryid,

case categoryid

when 1 then 'First'

when 2 then 'Second'

when 3 then 'Third'

else 'Unknown'

end as categoryname

from production.products;

--ntile top case

select top(10) orderid, custid, val,

case ntile(250) over(order by val)

when 1 then 'low'

when 2 then 'Medium'

when 3 then 'High'

end as titledesc

from sales.ordervalues

order by val;

--case

select orderid, custid, val,

case

when val < 1000.00 then 'Less than 1000'

when val between 1000.00 and 3000.00 then 'Between 1000 and 3000'

when val >3000.00 then 'More than 3000'

else 'Unknown'

end as valuecategory

from sales.ordervalues;

--is null

--"= null" is different from "is null"

select custid, country, region, city

from sales.customers

where region is null;

--<> is null

select custid, country, region, city

from sales.customers

where region is null or region <> N'WA'

select empid, firstname, lastname

from hr.employees

where lastname = N'davis';

-- +

select empid, firstname + N' ' + lastname as fullname

from hr.employees;

set concat_null_yields_null off;

-- + null

Select custid, country, region, city,

country + N',' + region + N',' + city as location

from sales.customers;

set concat_null_yields_null on;

--coalesce

select custid, country, region, city,

country + N',' + coalesce(region, N'$$$$$$') + N',' + city as location

from sales.customers;

--substring

select substring(N'abcdefg', 1, 3) as abc;

--left

select left(N'abcdefg', 3) as abc;

--right

select right(N'abcdefg', 4) as defg;

--len result:11

select len(N' abcdefg ') as str_length;

--datalength result:30

select datalength(N' abcdefg ') as str_datalength;

--charindex result:6, 14

select charindex(' ', 'Itzik Ben-Gan ') as idx;

select charindex(' ', 'Itzik Ben-Gan ', 7) as idx;

--patindex

select patindex('%[0-9]%', 'abcd123efgh') as idx;

--replace

select replace('1-A 2-B', '-', ':') as string;

select empid, lastname,

len(lastname)- len(replace(lastname, 'e', '')) as numoccur

from hr.employees;

--replicate

select replicate(N'abc',2) as abcabc;

select supplierid,

right(replicate('0', 9) + cast(supplierid as varchar(10)),10) as strsupplierid

from production.suppliers;

--stuff

select stuff('xyz', 2, 1, 'abc') as xabcz;

--upper

select upper('Itzik Ben-Gan') as name;

--lower

select lower('Itzik Ben-Gan') as name;

--rtrim

select rtrim(' abc ') as newstr;

--ltrim

select ltrim(' abc ') as newstr;

-- %

select empid, lastname

from hr.employees

where lastname like N'D%';

-- _

select empid, lastname

from hr.employees

where lastname like N'_e%'

-- []

select empid, lastname

from hr.employees

where lastname like N'[ABC]%'

-- [ - ]

select empid, lastname

from hr.employees

where lastname like N'[A-E]%'

-- [^ - ]

select empid, lastname

from hr.employees

where lastname like N'[^A-E]%'

select

getdate() as [GETDATE],

current_timestamp as [CURRENT_TIMESTAMP],

GETUTCDATE() AS [GETUTCDATE],

sysdatetime() as [SYSDATETIME],

sysutcdatetime() as [SYSUTCDATETIME],

SYSDATETIMEOFFSET() AS [SYSDATETIMEOFFSET];

--CAST

select cast('20090212' as date) as currentdate;

--convert

select convert(char(8), current_timestamp, 112) as [datetime] ;

--day month year

select

day('20090212') as theday,

month('20090212') as themonth,

year('20090212') as theyear;

exec sys.sp_tables;

你可能感兴趣的:(查询)