-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.sql
More file actions
47 lines (39 loc) · 977 Bytes
/
views.sql
File metadata and controls
47 lines (39 loc) · 977 Bytes
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
use sql_invoicing;
create or replace view sales_by_client as
select
client_id,
name,
sum(invoice_total) as total_sales
from clients c
join invoices i
using (client_id)
group by client_id, name;
SELECT *
FROM sql_invoicing.sales_by_client
order by client_id desc;
drop view sales_by_client;
-- Updatable views
-- views are updatable if they don't have
-- distinct, aggregate functions, group by/having, union
create or replace view invoices_with_balance as
select
invoice_id,
number,
client_id,
invoice_total,
payment_total,
invoice_total - payment_total as balance,
invoice_date,
due_date,
payment_date
from invoices
where (invoice_total - payment_total) > 0
with check option;
delete from invoices_with_balance
where invoice_id = 1;
update invoices_with_balance
set due_date = date_add(due_date, interval 2 day)
where invoice_id = 3;
update invoices_with_balance
set payment_total = invoice_total
where invoice_id = 3;