Last active
November 24, 2017 09:15
-
-
Save alexesDev/be5f924e4b75a2259b17b3ea5a93c0aa to your computer and use it in GitHub Desktop.
Выборка заказов из PG для обучения нейронной сети (тормознутый вариант, потому что генерирует много дубльданных для удобства)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- array_agg не поддерживает массивы | |
create aggregate array_agg_mult(anyarray) ( | |
SFUNC = array_cat, | |
STYPE = anyarray, | |
INITCOND = '{}' | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- для каждого продукта, заказанного за 2 часа, выбрать корзины без самого продукта | |
with items as ( | |
select product_id, order_id from order_items where created_at > now() - '2 hour'::interval | |
), order_products as ( | |
select order_id, array_agg(product_id) as product_ids from items group by order_id | |
) | |
select product_id, array_agg_mult(array_remove(product_ids, product_id)) | |
from items i, order_products o | |
where i.product_id = any(o.product_ids) | |
group by product_id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select json_agg(json_build_object('product_id', product_id, 'variants', variants)) | |
from ( | |
-- main.sql | |
with items as ( | |
select product_id, order_id from order_items where created_at > now() - '2 hour'::interval | |
), order_products as ( | |
select order_id, array_agg(product_id) as product_ids from items group by order_id | |
) | |
select product_id, array_agg_mult(array_remove(product_ids, product_id)) as variants | |
from items i, order_products o | |
where i.product_id = any(o.product_ids) | |
group by product_id | |
) t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cat neutron_json.sql | psql -t $DB_NAME > data.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment