- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sql
More file actions
Latest commit
219 lines (199 loc) · 7.5 KB
/
Copy pathtest.sql
File metadata and controls
219 lines (199 loc) · 7.5 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
DROPDATABASE tFood;
CREATEDATABASEIF NOT EXISTS tFood;
USE tFood;
CREATETABLEIF NOT EXISTS weight(
weight varchar(5),
gal int DEFAULT 0,
floz int DEFAULT 0,
quarts int DEFAULT 0,
cups int DEFAULT 0,
lbs int DEFAULT 0,
oz int DEFAULT 0,
g int DEFAULT 0,
PRIMARY KEY(weight)
);
CREATETABLEIF NOT EXISTS tRM(
snid int(7) NOT NULL AUTO_INCREMENT,
category ENUM("Food", "Liquid", "Packaging") NOT NULL,
-- categoryID int,
name varchar(30) NOT NULL,
measurement varchar(5),
weight decimal(5, 2),
pcsPerCs decimal(7,2) DEFAULT 0,
PRIMARY KEY(snid),
CONSTRAINT FK_weight FOREIGN KEY(measurement) REFERENCES weight(weight)
);
CREATETABLEIF NOT EXISTS tIn(
timestampTIMESTAMP DEFAULT CURRENT_TIMESTAMPNOT NULL,
snid int(7) NOT NULL,
debit decimal(7,2) NOT NULL DEFAULT 0,
pcsIn int(4) NOT NULL DEFAULT 0,
CONSTRAINT FK_tIn FOREIGN KEY(snid) REFERENCES tRM(snid)
);
CREATETABLEIF NOT EXISTS tOut(
timestampTIMESTAMP DEFAULT CURRENT_TIMESTAMPNOT NULL,
snid int(7) NOT NULL,
units int(3) DEFAULT 0,
waste int(3) DEFAULT 0,
rnd int(3) DEFAULT 0,
CONSTRAINT FK_tOut FOREIGN KEY(snid) REFERENCES tRM(snid)
);
INSERT INTO weight(weight, lbs, oz, g) VALUES("lbs", 1, 16, 454);
INSERT INTO weight(weight, lbs, oz, g) VALUES("oz", 0, 1, 28.35);
INSERT INTO weight(weight, lbs, oz, g) VALUES("g", 0, 0, 1);
INSERT INTO weight(weight, gal, floz, quarts, cups, oz, g) VALUES("gal", 1, 128, 4, 16, 8, 3632);
INSERT INTO tRM(category, name, measurement, weight, pcsPerCs) VALUES("Food", "Allspice", "lbs", 5, 1);
INSERT INTO tRM(category, name, measurement, weight, pcsPerCs) VALUES("Food", "Thyme", "oz", 20, 1);
INSERT INTO tRM(category, name, measurement, weight, pcsPerCs) VALUES("Liquid", "Cooking Wine", "gal", 1, 3);
INSERT INTO tRM(category, name, measurement, weight, pcsPerCs) VALUES("Packaging", "Vinegar Bottle", "oz", 16, 12);
insert into tIn(snid, debit, pcsIn) values(1, 55, 2);
insert into tIn(snid, debit, pcsIn) values(1, 26.75, 1);
insert into tIn(snid, debit, pcsIn) values(2, 311.88, 12);
insert into tIn(snid, debit, pcsIn) values(2, 623.76, 24);
insert into tIn(snid, debit, pcsIn) values(3, 32.99, 3);
insert into tIn(snid, debit, pcsIn) values(4, 95.88, 12);
insert into tOut(snid, units, waste, rnd) values(1, 2000, 200, 70);
insert into tOut(snid, units, waste, rnd) values(2, 1, 0, 0);
insert into tOut(snid, units, waste, rnd) values(3, 1, 0, 0);
insert into tOut(snid, units, waste, rnd) values(4, 1, 0, 0);
createviewqvt1as (
select
tRM.snid,
SUM(tIn.debit) as debit,
SUM(tIn.pcsIn) as pcsIn,
ROUND(SUM(tIn.debit) /SUM(tIn.pcsIn), 2) as costPerPc,
tRM.weight*weight.g*SUM(tIn.pcsIn) as totalG,
tRM.weight*weight.gas gramsPerPc,
ROUND(SUM(tIn.debit) / (tRM.weight*weight.g*SUM(tIn.pcsIn)), 3) as costPerG
from tRM, tIn, weight
wheretRM.snid=tIn.snidandtRM.measurement=weight.weight
group bytRM.snid
);
createviewqvt2as (
select
tRM.snid,
qvt1.costPerG*SUM(tOut.units+tOut.waste+tOut.rnd) as credit,
SUM(tOut.units+tOut.waste+tOut.rnd) as usedG
from tRM, tOut, qvt1
wheretRM.snid=tOut.snidandtRM.snid=qvt1.snid
group bytRM.snid
);
createviewvtas (
select
tRM.snid,
tRM.category,
tRM.name,
tRM.measurement,
tRM.weight,
tRM.pcsPerCs,
qvt1.debit,
qvt2.credit,
qvt1.pcsIn,
ROUND(SUM(qvt2.usedG/qvt1.gramsPerPc), 3) as pcsOut,
qvt1.costPerPc,
qvt1.gramsPerPc,
qvt1.totalG,
qvt2.usedG,
qvt1.totalG-qvt2.usedGas gramsOnHand,
qvt1.costPerG
from tRM, qvt1, qvt2, weight
wheretRM.snid=qvt1.snidandtRM.snid=qvt2.snidandtRM.measurement=weight.weight
group bytRM.snid
);
-- Raw Materials #131001
CREATETABLEIF NOT EXISTS rawMaterials(
snid int(7) NOT NULL AUTO_INCREMENT,
category ENUM("Canned Food", "Coffee", "Dry Goods", "Dry Herbs", "Fridge:Cheese", "Fridge:Milks", "Fridge:MISC", "Frozen Foods", "Grains & Flours", "Juices", "Labor & Shipping", "Nuts & Seeds", "Oil & Vinegar", "Packaging", "Produce", "Roots", "Sustainable", "Sweeteners") NOT NULL,
-- categoryID int,
name varchar(30) NOT NULL,
lbs decimal(7,2) DEFAULT 0,
oz decimal(7,2) DEFAULT 0,
g decimal(7,2) DEFAULT 0,
inchesPerUnit int(2) DEFAULT 0, -- ozPerUnit
inchesPerPc int(2) DEFAULT 0, -- lbsPerPc
pcsPerCs decimal(7,2) DEFAULT 0,
link varchar(255),
PRIMARY KEY(snid)
-- CONSTRAINT FK_Category FOREIGN KEY (categoryID) REFERENCES categories(snid)
);
CREATETABLEIF NOT EXISTS rmIn(
timestampTIMESTAMP DEFAULT CURRENT_TIMESTAMPNOT NULL,
snid int(7) NOT NULL,
debit decimal(7,2) NOT NULL DEFAULT 0,
pcsIn int(4) NOT NULL DEFAULT 0,
invoiceID varchar(50) NOT NULL DEFAULT 0,
purchaseDate datetime DEFAULT '1970-02-17',
CONSTRAINT FK_rmIn FOREIGN KEY(snid) REFERENCES rawMaterials(snid)
);
CREATETABLEIF NOT EXISTS rmOut(
timestampTIMESTAMP DEFAULT CURRENT_TIMESTAMPNOT NULL,
snid int(7) NOT NULL,
units int(3) DEFAULT 0,
waste int(3) DEFAULT 0,
rnd int(3) DEFAULT 0,
CONSTRAINT FK_rmOut FOREIGN KEY(snid) REFERENCES rawMaterials(snid)
);
INSERT INTO rawMaterials(category, name, lbs, oz, g, inchesPerUnit, inchesPerPc,
pcsPerCs) VALUES('Dry Goods', 'Allspice', 1, 0, 0, 0, 0, 1);
INSERT INTO rmIn(snid, debit, pcsIn, invoiceID, purchaseDate) VALUES(1, 0, 0, "xxxx", "1970-02-17");
INSERT INTO rmIn(snid, debit, pcsIn, invoiceID, purchaseDate) VALUES(1, 26.75, 1, "xxxx", "2026-1-03");
INSERT INTO rmOut(snid, units, waste, rnd) VALUES(1, 0, 0, 0);
INSERT INTO rmOut(snid, units, waste, rnd) VALUES(1, 1, 1, 1);
select
rawMaterials.lbs*16AS ozPerPc,
rawMaterials.lbs*453.6as gPerPc,
ROUND(SUM(rawMaterials.lbs*453.6) /2, 2) as portionPerPc
INTO @ozPerPc, @gPerPc, @portionPerPc
from rawMaterials;
CREATEVIEWIF NOT EXISTS qrm1 AS
SELECT
rawMaterials.snidAS snid,
SUM(rmIn.debit) AS debit,
SUM(rmIn.pcsIn) AS pcsIn,
ROUND(SUM(rmIn.debit) /SUM(rmIn.pcsIn), 2) AS costPerPc,
ROUND(rawMaterials.inchesPerPc/rawMaterials.inchesPerUnit, 0) AS unitsPerPc,
ROUND((rawMaterials.inchesPerPc/rawMaterials.inchesPerUnit) *SUM(rmIn.pcsIn), 0) AS unitsIn,
ROUND((rawMaterials.inchesPerPc/rawMaterials.inchesPerUnit) *rawMaterials.pcsPerCs, 0) AS unitsPerCs,
ROUND(SUM(rmIn.debit) / ((rawMaterials.inchesPerPc/rawMaterials.inchesPerUnit) *SUM(rmIn.pcsIn)), 2) AS costPerUnit
FROM rawMaterials, rmIn
WHERErawMaterials.snid=rmIn.snid
GROUP BYrawMaterials.snid
;
CREATEVIEWIF NOT EXISTS qrm2 AS
SELECT
rawMaterials.snid,
SUM(rmOut.units) AS units,
SUM(rmOut.waste) AS waste,
SUM(rmOut.rnd) AS rnd,
SUM(rmOut.units) +SUM(rmOut.waste) +SUM(rmOut.rnd) AS unitsOut
FROM rawMaterials, rmOut
WHERErawMaterials.snid=rmOut.snid
GROUP BYrawMaterials.snid
;
CREATEVIEWIF NOT EXISTS vRawMaterials AS
SELECT
CONCAT(rawMaterials.snid, "", rawMaterials.category, "", rawMaterials.name) AS description,
qrm1.debitas debit,
qrm1.costPerUnit*qrm2.unitsOutAS credit,
qrm1.costPerUnit*qrm2.unitsAS uDR,
qrm1.costPerUnit*qrm2.wasteAS wDR,
qrm1.costPerUnit*qrm2.rndAS rndDR,
qrm1.costPerPcAS costPerPc,
qrm1.costPerUnitAS costPerUnit,
rawMaterials.inchesPerUnitAS inchesPerUnit,
rawMaterials.inchesPerPcAS inchesPerPc,
rawMaterials.pcsPerCsAS pcsPerCs,
qrm1.unitsPerPcAS unitsPerPc,
qrm1.unitsPerCsAS unitsPerCs,
qrm1.pcsInAS pcsIn,
qrm1.unitsInAS unitsIn,
qrm2.unitsOutAS unitsOut,
qrm1.unitsIn-qrm2.unitsOutAS unitsOH,
qrm2.unitsAS units,
qrm2.wasteAS waste,
qrm2.rndAS rnd
FROM rawMaterials, qrm1, qrm2
WHERErawMaterials.snid=qrm1.snidANDqrm1.snid=qrm2.snid
GROUP BYrawMaterials.snid
;
SELECT"DONE!"AS"";