-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathr_add_temp_tables.sql
More file actions
executable file
·64 lines (62 loc) · 2.75 KB
/
Copy pathr_add_temp_tables.sql
File metadata and controls
executable file
·64 lines (62 loc) · 2.75 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
-- temporary table is session specific; so no collitions and is autodropped after the session is closed.
-- this reduces the query code length a lot.
-- sqlpage closes the session AFTER a request is served.
-- Quick refresh fails at create temporary table. That is because
-- connection is reused from the pool, but session is different.
-- We can safely drop if exists without impacting concurrent series
-- and proceed.
DROP TABLE IF EXISTS filtered;
CREATE TEMPORARY TABLE filtered AS SELECT e.*
FROM expense e
WHERE DATE(e.dt) BETWEEN
DATE($start) AND DATE($end) AND e.category IN (
SELECT value AS category FROM JSON_EACH($category) WHERE $category <> '' AND IFNULL($exclude,'') = ''
UNION
SELECT DISTINCT(category) FROM expense WHERE IFNULL($category,'') = ''
UNION
SELECT DISTINCT(x.category) FROM expense x WHERE $category <> '' AND IFNULL($exclude, '') <> ''
AND x.category NOT IN (SELECT value AS category FROM JSON_EACH($category))
)
AND (($payee <> '' AND EXISTS (SELECT 1 FROM payees WHERE payee MATCH $payee AND id=e.id )) OR ($payee = ''))
;
DROP TABLE IF EXISTS filtered_p;
CREATE TEMPORARY TABLE filtered_p AS SELECT e.*
FROM expense e
WHERE DATE(e.dt) BETWEEN $pstart AND $pend
AND e.category IN (
SELECT value AS category FROM JSON_EACH($category) WHERE $category <> '' AND ifnull($exclude,'') = ''
UNION
SELECT DISTINCT(category) FROM expense WHERE IFNULL($category,'') = ''
UNION
SELECT DISTINCT(x.category) FROM expense x WHERE $category <> '' AND IFNULL($exclude, '') <> ''
AND x.category NOT IN (SELECT value AS category FROM JSON_EACH($category))
)
AND (($payee <> '' AND EXISTS (SELECT 1 FROM payees WHERE payee MATCH $payee AND id=e.id )) OR ($payee = ''))
;
-- FIXME: Categories should've a lookup value for grouping in db instead of this temp table and a gui to tag Categories
-- according to people's data
DROP TABLE IF EXISTS category_classification;
CREATE TEMPORARY TABLE category_classification (
category TEXT
, classification TEXT
);
INSERT INTO category_classification (category, classification) VALUES
('Tax', 'Government')
, ('TDS', 'Government')
, ('Hire', 'Essential')
, ('Fuel', 'Essential')
, ('Grocery', 'Essential')
, ('Dinner', 'Essential')
, ('Telephone', 'Essential')
, ('Medicine', 'Essential')
, ('Car', 'Vehicles')
, ('Bike', 'Vehicles')
, ('School', 'Future')
, ('Insurance', 'Future')
, ('Interest', 'Returns')
, ('Reconcile', 'Returns')
, ('Salary', 'Essential')
, ('Fun', 'Often')
, ('Gifts', 'Often')
, ('Clothes', 'Often')
;