CH3
Presenter: yipo
2019-05-31
不能改變設計時…
18
VIEW
無法改變這個
↓
CustomerSales
SalesID | CustFirstName | CustLastName | Address | City | Phone | PurchaseDate | ModelYear | Model | SalesPerson |
---|---|---|---|---|---|---|---|---|---|
1 | Amy | Bacock | 111 Dover Lane | Chicago | 312-222-1111 | 2016-02-14 | 2016 | Mercedes R231 | Mariam Castro |
2 | Tom | Frank | 7453 NE 20th St. | Bellevue | 425-888-9999 | 2016-03-15 | 2016 | Land Rover | Donald Ash |
3 | Debra | Smith | 3223 SE 12th Pl. | Seattle | 206-333-4444 | 2016-01-20 | 2016 | Toyota Camry | Bill Baker |
4 | Barney | Killjoy | 4655 Rainier Ave. | Auburn | 253-111-2222 | 2015-12-22 | 2016 | Subaru Outback | Bill Baker |
5 | Homer | Tyler | 1287 Grady Way | Renton | 425-777-8888 | 2015-11-10 | 2016 | Ford Mustang GT Convertible | Mariam Castro |
6 | Tom | Frank | 7435 NE 20th St. | Bellevue | 425-888-9999 | 2015-05-25 | 2015 | Cadillac CT6 Seda | Jessica Robin |
以 檢視表 呈現正規化後的樣子
CREATE VIEW vCustomers AS
SELECT DISTINCT cs.CustFirstName, cs.CustLastName,
cs.Address, cs.City, cs.Phone
FROM CustomerSales AS cs;
CREATE VIEW vAutomobileModels AS
SELECT DISTINCT cs.ModelYear, cs.Model
FROM CustomerSales AS cs;
CREATE VIEW vEmployees AS
SELECT DISTINCT cs.SalesPerson
FROM CustomerSales AS cs;
挑選欄位 並 DISTINCT
,正規化為三個資料表。
vCustomers
CustFirstName | CustLastName | Address | City | Phone |
---|---|---|---|---|
Amy | Bacock | 111 Dover Lane | Chicago | 312-222-1111 |
Barney | Killjoy | 4655 Rainier Ave. | Auburn | 253-111-2222 |
Debra | Smith | 3223 SE 12th Pl. | Seattle | 206-333-4444 |
Homer | Tyler | 1287 Grady Way | Renton | 425-777-8888 |
Tom | Frank | 7435 NE 20th St. | Bellevue | 425-888-9999 |
Tom | Frank | 7453 NE 20th St. | Bellevue | 425-888-9999 |
便容易發現錯誤,並在原資料表修正。
無法改變這個
↓
Assignments
ID | DrawingNumber | Predecessor_1 | Predecessor_2 | Predecessor_3 | Predecessor_4 | Predecessor_5 |
---|---|---|---|---|---|---|
1 | LO542B2130 | LS01847409 | LS02390811 | LS02390813 | LS02390817 | LS02390819 |
2 | LO426C2133 | LS02388410 | LS02495236 | LS02495238 | LS02495241 | LS02640008 |
3 | LO329W2743-1 | LS02388418 | LS02640036 | LS02388418 | NULL | NULL |
4 | LO873W1842-2 | LS02388419 | LS02741454 | LS02741456 | LS02769388 | NULL |
5 | LO690W1960-1 | LS02742130 | NULL | NULL | NULL | NULL |
6 | LO217W1855-1 | LS02388421 | LS02769390 | NULL | NULL | NULL |
以 檢視表 呈現正規化後的樣子
CREATE VIEW vDrawings AS
SELECT a.ID AS DrawingID, a.DrawingNumber
FROM Assignments AS a;
CREATE VIEW vPredecessors AS
SELECT 1 AS PredecessorID, a.ID AS DrawingID,
a.Predecessor_1 AS Predecessor
FROM Assignments AS a
WHERE a.Predecessor_1 IS NOT NULL
UNION
…
UNION
SELECT 5, a.ID, a.Predecessor_5
FROM Assignments AS a
WHERE a.Predecessor_5 IS NOT NULL;
挑選重複欄位 並 UNION
DISTINCT
和 UNION
INSTEAD OF
觸發器CREATE TRIGGER view_insert
INSTEAD OF INSERT ON my_view
FOR EACH ROW
EXECUTE FUNCTION view_insert_row();
有效能疑慮
CREATE VIEW vActiveCustomers AS
SELECT c.CustomerID, c.CustFirstName, c.CustLastName,
c.CustFirstName + ' ' + c.CustLastName AS CustFullName
FROM Customers AS c
WHERE EXISTS (
SELECT NULL
FROM Orders AS o
WHERE o.CustomerID = c.CustomerID
AND o.OrderDate > DATEADD(MONTH, -6, GETDATE()));
近半年下單的客人
CREATE VIEW vCustomerStatistics AS
SELECT o.CustomerID, COUNT(o.OrderNumber) AS OrderCount,
SUM(o.OrderTotal) AS GrandOrderTotal,
MAX(o.OrderDate) AS LastOrderDate
FROM Orders AS o
GROUP BY o.CustomerID;
客人的統計資料
CREATE VIEW vActiveCustomerStatistics AS
SELECT a.CustomerID, a.CustFirstName, a.CustLastName,
s.LastOrderDate, s.GrandOrderTotal
FROM vActiveCustomers AS a
INNER JOIN vCustomerStatistics AS s
ON a.CustomerID = s.CustomerID;
近半年下單客人的統計資料 (但沒 OrderCount)
SELECT c.CustomerID, c.CustFirstName, c.CustLastName,
s.LastOrderDate, s.GrandOrderTotal
FROM Customers AS c
INNER JOIN (
SELECT o.CustomerID,
SUM(o.OrderTotal) AS GrandOrderTotal,
MAX(o.OrderDate) AS LastOrderDate
FROM Orders AS o
GROUP BY o.CustomerID) AS s
ON c.CustomerID = s.CustomerID
WHERE EXISTS (
SELECT NULL FROM Orders AS o
WHERE o.CustomerID = c.CustomerID
AND o.OrderDate > DATEADD(MONTH, -6, GETDATE()));
最佳化程序產生的結果
19
Extract, Transform, Load (ETL)
匯入外部資料的工具
AUTO INCREMENT
(或不要主鍵)大部份系統皆提供有類似工具
(或其他開源或付費工具)
20
SUMMARY
建立額外資料表來 匯總 (summary) 原始資料表
ENABLE QUERY OPTIMIZATION
CREATE SUMMARY TABLE SalesSummary AS (
SELECT
…
)
DB2
CREATE MATERIALIZED VIEW SalesSummary
…
Oracle
GROUP BY
)INNER JOIN
(??COUNT(*)
以使用 REFRESH IMMEDIATE
(??MATERIALIZED VIEW
) 達成來避免維護、管理的成本
INSERT INTO
陳述資料Orders
OrderID | CustomerID | Amount | Price | Count | Total |
---|---|---|---|---|---|
1 | 506 | 5 | $10 | NULL | NULL |
2 | 506 | 1 | $80 | NULL | NULL |
3 | 506 | 2 | $60 | NULL | NULL |
4 | 506 | NULL | NULL | 3 | $250 |
想像中的樣子 (不確定…)
21
UNION
整理資料從 Excel 來的試算表
SalesSummary
Category | OctQuantity | OctSales | NovQuantity | NovSales | DecQuantity | DecSales | JanQuantity | JanSales | FebQuantity | FebSales |
---|---|---|---|---|---|---|---|---|---|---|
Accessories | 930 | $61,165.40 | 923 | $60,883.03 | 987 | $62,758.14 | 1223 | $80,954.76 | 979 | $60,242.47 |
Bikes | 413 | $536,590.50 | 412 | $546,657.00 | 332 | $439,831.50 | 542 | $705,733.50 | 450 | $585,130.50 |
Car racks | 138 | $24,077.15 | 96 | $16,772.05 | 115 | $20,137.05 | 142 | $24,794.75 | 124 | $21,763.30 |
Clothing | 145 | $5,903.20 | 141 | $5,149.96 | 139 | $4,937.74 | 153 | $5,042.62 | 136 | $5,913.98 |
Components | 286 | $34,228.55 | 322 | $35,451.79 | 265 | $27,480.22 | 325 | $35,181.97 | 307 | $32,828.02 |
Skateboards | 164 | $60,530.06 | 203 | $89,040.58 | 129 | $59,377.20 | 204 | $79,461.30 | 147 | $61,125.19 |
Tires | 151 | $4,356.91 | 110 | $3,081.24 | 150 | $4,388.55 | 186 | $5,377.60 | 137 | $3,937.70 |
SELECT Category, OctQuantity, OctSales
FROM SalesSummary
不同月份資料要分開查詢
UNION
將各月份查詢合併SELECT Category, OctQuantity, OctSales
FROM SalesSummary
UNION
SELECT Category, NovQuantity, NovSales
FROM SalesSummary
UNION
…
Category | OctQuantity | OctSales |
---|---|---|
Accessories | 930 | $61,165.40 |
Accessories | 923 | $60,883.03 |
Accessories | 987 | $62,758.14 |
… | … | … |
Tires | 150 | $4,388.55 |
Tires | 186 | $5,377.60 |
Tires | 137 | $3,937.70 |
分不清是何月
SELECT
增加月份欄位 ⑵ 善用 AS
更名欄位SELECT Category, 'Oct' AS SalesMonth,
OctQuantity AS Quantity, OctSales AS SalesAmt
FROM SalesSummary
UNION
SELECT Category, 'Nov', NovQuantity, NovSales
FROM SalesSummary
UNION
…
Category | SalesMonth | Quantity | Sales |
---|---|---|---|
Accessories | Oct | 930 | $61,165.40 |
Accessories | Nov | 923 | $60,883.03 |
Accessories | Dec | 987 | $62,758.14 |
… | … | … | … |
Tires | Dec | 150 | $4,388.55 |
Tires | Jan | 186 | $5,377.60 |
Tires | Feb | 137 | $3,937.70 |
ORDER BY
排序資料SELECT Category, 'Oct' AS SalesMonth,
OctQuantity AS Quantity, OctSales AS SalesAmt
FROM SalesSummary
UNION
…
UNION
SELECT Category, 'Jan', JanQuantity, JanSales
FROM SalesSummary
UNION
SELECT Category, 'Feb', FebQuantity, FebSales
FROM SalesSummary
ORDER BY SalesMonth, Category;
(欄位的名稱沒差)
UNION ALL
不排除重複,
速度較快。
END;