josepablocam / aquery

A scala rewrite of the AQuery system
10 stars 6 forks source link

Group by with avgs does not work #1

Closed ramannanda9 closed 7 years ago

ramannanda9 commented 7 years ago

I am using the latest build.

The query is mentioned below. It works fine when group by stocksymbol is not used, but that is not the intended use.

create table trades(stocksymbol INT, time INT, quantity INT, price INT)
LOAD DATA INFILE "trades.csv"
INTO TABLE trades
FIELDS TERMINATED BY ","

select stocksymbol, avgs(10,price) as avg_price from trades assuming asc time where stocksymbol=1 
group by stocksymbol
INTO OUTFILE "op.csv" 
FIELDS TERMINATED BY ","

And the message that is printed on q console is as follows.

    `trades
    `trades
    k){$[t&77>t:@y;$y;x;-14!'y;y]}

My KDB version is

KDB+ 3.4 2016.06.14 Copyright (C) 1993-2016 Kx Systems
josepablocam commented 7 years ago

For future reference for other users: the issue isn't the avgs, nor the grouping, but rather that writing to csv (or any file for that matter) from aquery only is possible with normalized tables (i.e. no nested arrays)

ramannanda9 commented 7 years ago

Thanks Jose,

Here is the query for future references.

with grouped_averages(stock_symbol,avg_price) as (select stocksymbol, avgs(10,price) as avg_price from trades assuming asc time where stocksymbol=1 
group by stocksymbol) 

select * from flatten(grouped_averages)
INTO OUTFILE "op.csv" 
FIELDS TERMINATED BY ","