从这里下载文件employees.txt,customers.txt,orders.txt
参考文章:http://www.cnblogs.com/wwan/archive/2011/02/24/1964279.html
使用package导入数据:http://www.cnblogs.com/tylerdonet/archive/2011/04/17/2017471.html
简单的聚合
从orders表中选择各个年份共有共有多少客户订购了商品
- 第一种写法,我们可以写成这样
1select YEAR(o.orderdate) orderyear,COUNT(distinct(custid)) numCusts
2from Sales.Orders o
3group by YEAR(o.orderdate)
4go - 第二种写法,
1select orderyear,COUNT(distinct(custid))numCusts
2from (select YEAR(orderdate) as orderyear,custid from sales.orders) as D
3group by orderyear
4go - 第三种写法,
1select orderyear,COUNT(distinct(custid)) numCusts
2from (select YEAR(orderdate),custid from sales.orders) as D(orderyear,custid)
3group by orderyear
4go - 第四种写法,with出场了
1with c as(
2select YEAR(orderdate) orderyear, custid from sales.orders)
3select orderyear,COUNT(distinct(custid)) numCusts from c group by orderyear
4go
指定临时命名的结果集,这些结果集称为公用表表达式 (CTE)。该表达式源自简单查询,并且在单条 SELECT、INSERT、UPDATE、MERGE 或 DELETE 语句的执行范围内定义。该子句也可用在 CREATE VIEW 语句中,作为该语句的 SELECT 定义语句的一部分。公用表表达式可以包括对自身的引用。这种表达式称为递归公用表表达式。
----MSDN
- 第五种写法,也可以借鉴第三种写法,这样使语句更加清楚明了,便于维护
1 with c(orderyear,custid) as(
2 select YEAR(orderdate),custid from sales.orders)
3 select orderyear,COUNT(distinct(custid)) numCusts from c group by c.orderyear
4 go图1
添加计算
- 现在要求要求计算出订单表中每年比上一年增加的客户数目,这个稍微复杂
1 with yearcount as(
2 select YEAR(orderdate) orderyear,COUNT(distinct(custid)) numCusts from sales.orders group by YEAR(orderdate))
3 select cur.orderyear curyear,cur.numCusts curNumCusts,prv.orderyear prvyear,prv.numCusts prvNumCusts,cur.numCusts-prv.numCusts growth
4 from yearcount cur left join yearcount prv on cur.orderyear=prv.orderyear+1
5 go
图2
复杂的计算
- 查找所有美国雇员掌握的所有至少有一个订单的客户的客户id,查询语句如下
1 with TheseEmployees as(
2 select empid from hr.employees where country='USA'),
3 CharacteristicFunctions as(
4 select custid,
5 case when custid in (select custid from sales.orders as o where o.empid=e.empid) then 1 else 0 end as charfun
6 from sales.customers as c cross join TheseEmployees as e)
7 select custid,min(charfun) from CharacteristicFunctions group by custid having min(charfun)=1
8 go
结果如下图3
图3
引用至: http://www.cnblogs.com/tylerdonet/archive/2011/04/18/2020225.html
CREATE TABLE #tempTotalCBM
( Appointid int,
TotalCBM NUMERIC
)
INSERT INTO #tempTotalCBM (Appointid,TotalCBM)
SELECT H.APPOINTID,
CAST(( CASE H.TruckerConfirm WHEN 'D' THEN SUM(D.DELIVERYCBM) ELSE SUM(D.CBM) END ) AS NUMERIC) AS TotalCBM
FROM
(
SELECT APPOINTID , TruckerConfirm
FROM CCC_TAHEADER
WHERE
DATEDIFF(D,RECEIVEDATE,'2010-10-01') <= 0
AND DATEDIFF(D,RECEIVEDATE,GETDATE()) >= 0
) H
INNER JOIN CCC_TADETAIL D
ON D.APPOINTID = H.APPOINTID
GROUP BY H.APPOINTID,H.TruckerConfirm
SELECT M.APPOINTID,M.LOADTYPE,N.TotalCBM,
L_TruckSize3T, L_TruckSize5T, L_TruckSize8T, L_TruckSize10T, L_TruckSize12T,
Size20, Size40, Size40R, Size40HQ, Size45,ReceiveDate
FROM CCC_TAHEADER M
INNER JOIN
#tempTotalCBM N
ON M.APPOINTID = N.Appointid
-- SELECT * FROM #tempTotalCBM
DROP TABLE #tempTotalCBM
with c as (
SELECT H.APPOINTID,
CAST(( CASE H.TruckerConfirm WHEN 'D' THEN SUM(D.DELIVERYCBM) ELSE SUM(D.CBM) END ) AS NUMERIC) AS TotalCBM
FROM
(
SELECT APPOINTID , TruckerConfirm
FROM CCC_TAHEADER
WHERE
DATEDIFF(D,RECEIVEDATE,'2010-10-01') <= 0
AND DATEDIFF(D,RECEIVEDATE,GETDATE()) >= 0
) H
INNER JOIN CCC_TADETAIL D
ON D.APPOINTID = H.APPOINTID
GROUP BY H.APPOINTID,H.TruckerConfirm
)
SELECT M.APPOINTID,M.LOADTYPE,c.TotalCBM,
L_TruckSize3T, L_TruckSize5T, L_TruckSize8T, L_TruckSize10T, L_TruckSize12T,
Size20, Size40, Size40R, Size40HQ, Size45,ReceiveDate
FROM CCC_TAHEADER M
INNER JOIN
c
ON M.APPOINTID = c.Appointid