-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_generator.py
executable file
·82 lines (72 loc) · 2.47 KB
/
query_generator.py
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
from ast import AsyncFunctionDef
class query_generator:
def generate_count_query(table, count_column, grouping_column = None):
"""Generate an SQL query that returns the count of one column grouped by another.
Parameters
----------
table : str,
identifier_column : str,
grouping_column : str,
count_column : str,
Returns
----------
A dict:
query: A string containing the SQL query that will satisfy the requirements."""
if grouping_column == None:
return {
"query": f"""
SELECT COUNT({count_column}) FROM {table}
"""
}
return {
"query": f"""
SELECT {grouping_column}, {count_column}, COUNT({grouping_column})
FROM {table}
GROUP BY {grouping_column}, {count_column}"""
}
def generate_sum_query(table, sum_column, grouping_column = None):
query = ""
if grouping_column == None:
query = {
"query": f"""
SELECT SUM({sum_column}) FROM {table}
"""
}
else:
query = {
"query": f"""
SELECT {grouping_column}, SUM({sum_column}) FROM {table}
GROUP BY {grouping_column}
"""
}
return query
def generate_average_query(table, average_column, grouping_column = None):
if grouping_column == None:
query = {
"query": f"""
SELECT COUNT({average_column}),
SUM({average_column}) ,
MIN({average_column}),
MAX({average_column})
FROM {table}
"""
}
else:
query = {
"query": f"""
SELECT {grouping_column},
COUNT({average_column}),
SUM({average_column}),
MIN({average_column}),
MAX({average_column})
FROM {table}
GROUP BY {grouping_column}
"""
}
return query
def generate_generic_query(table, column):
return {
"query": f"""
SELECT {column} from {table};
"""
}