I am using alasql in node.js, and I cannot get a join to work.
Here you have dummy data:
x = [
{ date: 20180501, price: 23, product: 'x' },
{ date: 20180501, price: 46, product: 'y' },
{ date: 20180502, price: 29, product: 'x' },
{ date: 20180502, price: 50, product: 'y' },
{ date: 20180503, price: 22, product: 'x' },
{ date: 20180503, price: 43, product: 'y' },
{ date: 20180504, price: 21, product: 'x' },
{ date: 20180504, price: 43, product: 'y' },
{ date: 20180505, price: 26, product: 'x' },
{ date: 20180505, price: 48, product: 'y' }]
I would like to get, for each day, the ratio between the price of product y and product y. So, my desired output is:
desiredOutput = [
{ date: 20180501, price_ratio: 46/23},
{ date: 20180502, price_ratio: 50/29},
{ date: 20180503, price_ratio: 43/22},
{ date: 20180504, price_ratio: 43/21},
{ date: 20180505, price_ratio: 48/26}]
I am attempting to get this with the following query:
alasql("select date, price_y/price_x as price_ratio from (select date, price as price_y from ? where product='y') as y join (select date, price as price_x from ? where product='x') as x on x.date=y.date", [x,x])
But I can't get it to work. It doesn't crash or anything, but I only get price_x, not the ratio. This is what I get:
[
{ date: 20180501, price_x: 23 },
{ date: 20180502, price_x: 29 },
{ date: 20180503, price_x: 22 },
{ date: 20180504, price_x: 21 },
{ date: 20180505, price_x: 26 } ]
I can get the desired result by running and storing each subquery separately, and then performing the join using those objects, but I would like to know how to do it with just nested subqueries, in a single call.
Any help would be appreciated!