第二章 单表查询

use tsql2012

--From
select orderid, custid, empid, orderdate, freight
from sales.orders;

--where
select orderid, custid, empid, orderdate, freight
from sales.orders
where custid=71;

--group by
select empid, year(orderdate) as orderyear
from sales.orders
where custid=71
group by empid, year(orderdate);

--sum count
select empid, year(orderdate) as orderyear,
sum(freight) as totalfreight,
count(*) as numorders
from sales.orders
where custid=71
group by empid, year(orderdate);

--distinct
select
empid,
year(orderdate) as orderyear,
count(distinct custid) as numcusts
from sales.orders
group by empid, year(orderdate);

--having
select empid, year(orderdate) as orderyear, count(*) as numorders
from sales.orders
where custid = 71
group by empid, year(orderdate)
having count(*) > 1;

--select
select orderid, year(orderdate) as orderyear
from sales.orders
where year(orderdate) > 2006;

--distinct
select distinct empid, year(orderdate) as orderyear
from sales.orders
where custid = 71;

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