Repository navigation
Expand file tree
/
Copy pathpc_tables.sql
More file actions
117 lines (99 loc) · 2.45 KB
/
Copy pathpc_tables.sql
File metadata and controls
117 lines (99 loc) · 2.45 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-- Spencer's fantastic new table for faster queries
----------------------------------------------------------------------------------------
DROP TABLE IF EXISTS pc_UserStateProdCatAmt;
CREATE TABLE pc_UserStateProdCatAmt (
uid INT,
state TEXT,
pid INT,
cid INT,
total INT
);
INSERT INTO pc_UserStateProdCatAmt
SELECT users.id, users.state, products.id, products.cid, SUM(sales.quantity*sales.price)
FROM sales, users, products
WHERE sales.uid = users.id AND sales.pid = products.id
GROUP BY users.id, state, products.id, products.cid;
----------------------------------------------------------------------------------------
DROP TABLE pc_UsersAmt;
CREATE TABLE pc_UsersAmt (
uid INT,
name TEXT,
total INT
);
INSERT INTO pc_UsersAmt
SELECT u.id, u.name, SUM(s.quantity*s.price)
FROM sales s, users u
WHERE s.uid = u.id
GROUP BY u.id, u.name;
DROP TABLE pc_ProdAmt;
CREATE TABLE pc_ProdAmt (
pid INT,
name TEXT,
total INT
);
INSERT INTO pc_ProdAmt
SELECT p.id, p.name, SUM(s.quantity*s.price)
FROM sales s,products p
WHERE s.pid = p.id
GROUP BY p.id, p.name;
DROP TABLE pc_StateAmt;
CREATE TABLE pc_StateAmt (
state TEXT,
total INT
);
INSERT INTO pc_StateAmt
SELECT st.name, SUM(s.quantity*s.price)
FROM sales s, states st, users u
WHERE st.name = u.state
AND u.id = s.uid
GROUP BY st.name;
DROP TABLE pc_UserProdAmt;
CREATE TABLE pc_UserProdAmt (
uid INT,
pid INT,
total INT
);
INSERT INTO pc_UserProdAmt
SELECT uid, pid, SUM(quantity*price)
FROM sales GROUP BY uid, pid;
DROP TABLE pc_UseCatAmt;
CREATE TABLE pc_UseCatAmt (
uid INT,
name TEXT,
cid INT,
total INT
);
INSERT INTO pc_UseCatAmt
SELECT s.uid, u.name, c.id, SUM(s.quantity*s.price)
FROM sales s, categories c, products p, users u
WHERE c.id = p.cid
AND s.pid = p.id
AND s.uid = u.id
GROUP BY s.uid, u.name, c.id;
DROP TABLE pc_StateProdAmt;
CREATE TABLE pc_StateProdAmt (
state TEXT,
pid INT,
total INT
);
INSERT INTO pc_StateProdAmt
SELECT st.name, p.id, SUM(s.quantity*s.price)
FROM sales s, states st, products p, users u
WHERE st.name = u.state
AND s.pid = p.id
AND s.uid = u.id
GROUP BY st.name, p.id;
DROP TABLE pc_StateCatAmt;
CREATE TABLE pc_StateCatAmt (
state TEXT,
cid INT,
total INT
);
INSERT INTO pc_StateCatAmt
SELECT st.name, c.id, SUM(s.quantity*s.price)
FROM sales s, states st, products p, users u, categories c
WHERE st.name = u.state
AND s.pid = p.id
AND s.uid = u.id
AND p.cid = c.id
GROUP BY st.name, c.id;