Saturday, 15 March 2014

sql - Macros in MySQL queries -



sql - Macros in MySQL queries -

for info analysis purposes, i'm doing similar queries aggregate info along various dimensions. dimensions utilize alter fields i'm aggregating same. in practice, i'm doing

select key1, key2, ... sum(agg_field1), avg(agg_field2), count(distinct agg_field3) ... ... ...

i have lot of aggregations , handful if replace list of aggregations kind of macros. utilize word "macro" since reminds me c/c++ macros. imagining:

select key1, key2, ..., my_aggregations ... ...

this have lot of advantages

queries me more clear it easy share aggregations other users on same db queries faster write

you can develop 1 function can pass parameter , using while loop can build query dynamically. if want execute query can develop store procedure , prepare query dynamically , execute it.

you can pass parameter info how many aggregation field want , distict keyword need of it. can utilize while loop , build query.

by prepare statement can execute dynamic queries http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html

mysql sql

No comments:

Post a Comment