-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL View.sql
More file actions
124 lines (90 loc) · 2.98 KB
/
Copy pathSQL View.sql
File metadata and controls
124 lines (90 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use salesdb;
-- Database Architecture: Physical level, Logical level, View level
-- Physical level(Internal layer): Actual data is storedin physical storage
-- Database Administrator works in this layer, (Datafiles, Partituions, Logs, Blocks, Caches)
-- Logical level(Conceptual): Appli dev (data eng) Stuctures the data, Tables, Relationships, Views, Indexes, Procedures
-- View level(External: Users(Business Analytics, Power BI, End users)
-- 3absractions lecels of the databases
-- View: Virtual table on the the result set of a query, without storing the data in database
-- Generally SQL Queries
-- Central Query Logic: Store central , complex query logic inthe database for access by multiple queries
-- reducing project complexity(Storedin databse -> All analysts can use it to reduce the queries)
-- Find the running total sales of each month
with CTE_monthly_summary as(
select
month(orderdate) ordermonth,
sum(sales) totalsales,
count(orderid) totalorders,
sum(quantity) totalquantity
from orders
group by month(orderdate))
select
ordermonth,
totalsales,
sum(totalsales) over(order by ordermonth) as RunningTotal
from CTE_monthly_summary;
create view salesdb.v_monthly_summary as(
select
month(orderdate) ordermonth,
sum(sales) totalsales,
count(orderid) totalorders,
sum(quantity) totalquantity
from orders
group by month(orderdate));
select
*
from v_monthly_summary;
select
ordermonth,
totalsales,
sum(totalsales) over(order by ordermonth) as RunningTotal
from v_monthly_summary;
-- T-SQL: Transact SQL is an extension of SQL that adds programming features
-- Use case: Hide Complexity
-- Provide a view that combines the deytails from orders, products, customers and employees
create view v_order_details as(
select
o.orderid,
concat(coalesce(c.firstname,''), " ",coalesce(c.lastname,'')) customername,
concat(coalesce(e.firstname,''), " ",coalesce(e.lastname,'')) employeename,
e.department,
c.country customercountry,
p.product,
p.category,
o.orderdate,
o.quantity,
o.sales
from orders o
left join products p on o.productid = p.productid
left join customers c on o.customerid = c.customerid
left join employees e on o.salespersonid = e.employeeid);
select
*
from v_order_details;
-- Usecase: Data Security
-- Provide a view for EU sales teamthat commbines details from all tables
-- and excludes data related to the USA
create view v_GER_Ord_details as(
select
o.orderid,
concat(coalesce(c.firstname,''), " ",coalesce(c.lastname,'')) customername,
concat(coalesce(e.firstname,''), " ",coalesce(e.lastname,'')) employeename,
e.department,
c.country customercountry,
p.product,
p.category,
o.orderdate,
o.quantity,
o.sales
from orders o
left join products p on o.productid = p.productid
left join customers c on o.customerid = c.customerid
left join employees e on o.salespersonid = e.employeeid
where c.country != 'USA'
);
select
*
from v_GER_Ord_details;
-- Use Case: Flexibility & Dynamic
-- Usecase: Multi - Languages
-- Use Case: Virtual Marts in Data Warehouse