forked from makmanalp/sqlalchemy-crosstab-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
59 lines (47 loc) · 1.83 KB
/
example.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
from crosstab import crosstab
from sqlalchemy import Table, Column, Text, Integer, MetaData, distinct, select
#create Engine and bind
from sqlalchemy import create_engine
engine = create_engine("postgresql+psycopg2:///dbname")
engine.echo = True
m = MetaData()
m.bind = engine
#Set up the sample source data
raw = Table('raw', m, Column('country', Text),
Column('year', Integer),
Column('quantity', Integer),
Column('unrelated_field', Text))
data = [
('India', 2009, 100, "foo"),
('India', 2010, 150, "foo"),
('India', 2011, 200, "foo"),
('Czechoslovakia', 2008, 200, "foo"),
('Czechoslovakia', 2010, 400, "foo")
]
raw.create()
for line in data:
raw.insert().values(line).execute()
#Define the input table
crosstab_input = \
select([ raw.c.country,
raw.c.year,
raw.c.quantity])
#Define the categories. For us, this is 2008, 2009, 2010 etc.
categories = \
select([distinct(raw.c.year)])
#or you could fake the values like so:
#categories = select(['*']).select_from('(VALUES (2008), (2009), (2010), (2011)) x')
#Define the return row types. The fact that we're defining a table is a
#formality, it's just easier to do it that way. It won't ever get created.
ret_types = Table('ct', m, Column('country', Text),
Column('y1', Integer),
Column('y2', Integer),
Column('y3', Integer),
Column('y4', Integer),
)
#Finally, the crosstab query itself. Has the input query, the category query and the return types.
q = select(['*']).select_from(crosstab(crosstab_input, ret_types, categories=categories))
#Ta-daaa!
print [x for x in engine.execute(q)]
#cleanup
raw.drop()