sql server 2008語言基礎 表表示式

2022-01-14 21:35:47 字數 1822 閱讀 8447

--返回每個雇員處理過的訂單的最近日期.

--select empid, max(orderdate) as maxorderdate from sales.orders group

by empid

--封裝上面的結果集, 並和order表連線, 為每個雇員返回其訂單日期最近的訂單資訊.

/*with t as (

select empid, max(orderdate) as maxorderdate from sales.orders group

by empid

)select t.empid, t.maxorderdate, o.orderid, o.custid

from t inner

join sales.orders o on t.empid=o.empid and o.orderdate=t.maxorderdate

*/--為每個訂單按照orderdate, orderid的順序來計算其行號.

--select orderid, orderdate, custid, empid, row_number() over(order

by orderdate, orderid) rownum

--from sales.orders

/*with t as (

select orderid, orderdate, custid, empid, row_number() over(order

by orderdate, orderid) rownum

from sales.orders

)select * from t where rownum>10 and rownum<=20

*/--使用cte遞迴來設計乙個解決方案, 返回領導id為9 的管理鏈.

/*with t as (

select empid,mgrid,firstname,lastname from hr.employees where empid=9

union

allselect e.empid,e.mgrid,e.firstname,e.lastname from hr.employees e inner

join t on e.empid=t.mgrid

)select * from t

*/--返回每個雇員每年處理的總訂貨量

/*create

view [dbo].[v_1]

asselect

top (100) percent sales.orders.empid, year(sales.orders.orderdate) as orderyear, sum(sales.orderdetails.qty) as totalqty

from sales.orderdetails inner

join

sales.orders on sales.orderdetails.orderid = sales.orders.orderid

group

by sales.orders.empid, year(sales.orders.orderdate)

order

by sales.orders.empid, orderyear

*/--使用上面這個檢視, 返回每個雇員每年處理過的連續總訂貨量

select *,(select

sum(totalqty) from v_1 v1 where v1.empid=v2.empid and v1.orderyear<=v2.orderyear) from dbo.v_1 v2

order

by empid,orderyear

sql server 2008語言基礎 集合

declare t table n int insert into t values 0 1 2 3 4 5 6 7 8 9 select from t 返回在2008年1月有訂單活動,而在08年2月沒有訂單活動的客戶和雇員.select custid,empid from sales.orders...

Sql Server 2008 收縮日誌

收縮日誌 alter database dnname set recovery with no wait goalter database dnname set recovery 簡單模式 gouse dnname godbcc shrinkfile n dnname log 11,truncate...

徹底解除安裝sql server2008

微軟的開發工具在按裝和解除安裝時都讓人頭疼,只能是裝在c盤,裝在其他盤時最容易出事 在重新按裝的時候一定要把以前的例項解除安裝完才行。要不就會出錯。在解除安裝sql server後,其實還沒有完成,還要把登錄檔資訊完全刪乾淨,下面就將教您徹底刪除sql server登錄檔的方法,供您參考。在解除安裝...