--1-1 建立輔助表dbo.nums
set nocount on;
use tsqlfundamentals2008;
goif object_id('dbo.nums','u') is not null
drop table dbo.nums
create table dbo.nums(n int not null, constraint pk_n primary key(n))
declare @i as int =1;
begin tran
while @i<=100000
begin
insert into dbo.nums values(@i);
set @i=@i+1;
endcommit tran
set nocount off;
--1-2 寫一條查詢語句把所有雇員記錄複製5次
select empid, firstname, lastname, n
from hr.employees cross join dbo.nums
where n < 6
--1-3 寫乙個查詢,為每個雇員和從2023年6月12日至2023年6月16日範圍內的每天返回一行.
select empid, dateadd(day, n-1, n'20090612') as dt
from hr.employees cross join dbo.nums
where n <= datediff(day, n'20090612', n'20090616') + 1
--2 返回來自美國的客戶,並為每個客戶返回其訂單總數和商品交易總數量
select c.custid, count(distinct o.orderid) as numorders,
sum(od.qty)
from sales.customers c join sales.orders o
on c.custid=o.custid
join sales.orderdetails od
on o.orderid = od.orderid
where c.country = n'usa'
group by c.custid
--3 返回客戶及其訂單資訊,包括沒有下過任何訂單的客戶.
select c.custid, c.companyname, o.orderid, o.orderdate
from sales.customers c
left outer join sales.orders o
on c.custid = o.custid
--4 返回沒有下過訂單的客戶
select c.custid, c.companyname
from sales.customers c
left outer join sales.orders o
on c.custid = o.custid
where o.orderid is null
--5 返回在2023年2月12日下過訂單的客戶,以及他們的訂單
select c.custid, c.companyname, o.orderid, o.orderdate
from sales.customers c join sales.orders o
on c.custid = o.custid and o.orderdate >= n'20070212' and o.orderdate < n'20070213'
--6 返回在2023年2月12日下過訂單的客戶,以及他們的訂單.同時返回2023年2月12日沒有下過訂單的客戶.
select c.custid, c.companyname, o.orderid, o.orderdate
from sales.customers c
left join sales.orders o
on c.custid = o.custid and o.orderdate >= n'20070212' and o.orderdate < n'20070213'
--7 返回所有的客戶的資訊,並根據客戶是否在2023年2月12日下過訂單,再為每個客戶返回一列yes/no值。
select distinct c.custid, c.companyname,
case
when o.orderid is null then 'no'
else 'yes'
end as hasorderon20070212
from sales.customers c left outer join sales.orders o
on c.custid = o.custid and o.orderdate >= n'20070212' and o.orderdate < n'20070213'
《演算法》 第三章 查詢
在第三章裡面,並不是一開始就講的查詢,第一節中首先介紹的是符號表。符號表,其實就是儲存了鍵值對的一種資料結構,鍵值對用於將乙個鍵和乙個值聯絡起來。符號表支援兩種操作 插入 put 即將一組新的鍵值對存入表中 查詢 get 即根據給定的鍵得到對應的值。下面看一下書中關於符號表的應用以及api 書中對於...
javaoop 第三章 多型(2)
本章目標 a。掌握多型的優勢和應用場合 b。子類重寫父類的方法 c。掌握instanceof運算子的使用 一丶多型 什麼是多型?多型是具有表現多種形態的能力的特徵。同乙個實現介面,使用不同示例而執行不同的操 作。同樣的行為,但結果不同。同乙個引用型別,使用不同的例項而執行不同的操作。父類做引用,子類...
記憶體管理2《第三章》
虛擬儲存器的基本概念 所謂虛擬儲存器,是指具有請求調入功能和頁面置換功能,能從邏輯上對記憶體進行擴充的一 種儲存器系統 虛擬儲存的特徵 多次性,對換性,虛擬性 實現虛擬記憶體的軟硬體支援 最佳置換演算法 optimal 在最長時間內不再被訪問的。先進先出頁面置換演算法 fifo 淘汰最先進入記憶體的...