subquery

Django & Postgres - percentile (median) and group by

别来无恙 提交于 2020-05-28 07:27:27
问题 I need to calculate period medians per seller ID (see simplyfied model below). The problem is I am unable to construct the ORM query. Model class MyModel: period = models.IntegerField(null=True, default=None) seller_ids = ArrayField(models.IntegerField(), default=list) aux = JSONField(default=dict) Query queryset = ( MyModel.objects.filter(period=25) .annotate(seller_id=Func(F("seller_ids"), function="unnest")) .values("seller_id") .annotate( duration=Cast(KeyTextTransform("duration", "aux"),

Unsupported subquery type cannot be evaluated

自作多情 提交于 2020-05-17 06:22:26
问题 The inner query is giving problem. MERGE INTO evaluation_fact eval_fact USING (SELECT $27 tim_id , $28 dt_id , $13 w_id FROM @REPORTING_MERGE_STAGE/reporting-data-ingest/Evaluation-IT.csv) csv_data ON eval_fact.w_id = csv_data.w_id WHEN matched THEN UPDATE SET tim_id = (SELECT day_id FROM rom dates_dim d WHERE d.day_id = csv_data.tim_id), dt_id = (SELECT time_id FROM time_dim t WHERE t.time_name = csv_data.dt_id) WHEN NOT matched THEN INSERT (tim_id, dt_id) VALUES ( (SELECT day_id FROM dates

Unsupported subquery type cannot be evaluated

邮差的信 提交于 2020-05-17 06:22:06
问题 The inner query is giving problem. MERGE INTO evaluation_fact eval_fact USING (SELECT $27 tim_id , $28 dt_id , $13 w_id FROM @REPORTING_MERGE_STAGE/reporting-data-ingest/Evaluation-IT.csv) csv_data ON eval_fact.w_id = csv_data.w_id WHEN matched THEN UPDATE SET tim_id = (SELECT day_id FROM rom dates_dim d WHERE d.day_id = csv_data.tim_id), dt_id = (SELECT time_id FROM time_dim t WHERE t.time_name = csv_data.dt_id) WHEN NOT matched THEN INSERT (tim_id, dt_id) VALUES ( (SELECT day_id FROM dates

Complex nested aggregations to get order totals

限于喜欢 提交于 2020-05-17 03:03:35
问题 I have a system to track orders and related expenditures. This is a Rails app running on PostgreSQL. 99% of my app gets by with plain old Rails Active Record call etc. This one is ugly. The expenditures table look like this: +----+----------+-----------+------------------------+ | id | category | parent_id | note | +----+----------+-----------+------------------------+ | 1 | order | nil | order with no invoices | +----+----------+-----------+------------------------+ | 2 | order | nil | order

Complex nested aggregations to get order totals

元气小坏坏 提交于 2020-05-17 03:01:35
问题 I have a system to track orders and related expenditures. This is a Rails app running on PostgreSQL. 99% of my app gets by with plain old Rails Active Record call etc. This one is ugly. The expenditures table look like this: +----+----------+-----------+------------------------+ | id | category | parent_id | note | +----+----------+-----------+------------------------+ | 1 | order | nil | order with no invoices | +----+----------+-----------+------------------------+ | 2 | order | nil | order

Postgres find all rows in database tables matching criteria on a given column

眉间皱痕 提交于 2020-04-16 03:18:09
问题 I am trying to write sub-queries so that I search all tables for a column named id and since there are multiple tables with id column, I want to add the condition, so that id = 3119093 . My attempt was: Select * from information_schema.tables where id = '3119093' and id IN ( Select table_name from information_schema.columns where column_name = 'id' ); This didn't work so I tried: Select * from information_schema.tables where table_name IN ( Select table_name from information_schema.columns

Query to see the MAX value in Oracle

三世轮回 提交于 2020-03-25 18:56:05
问题 I have the following N:N table (which stores the people boardings) in my Oracle Database (it is about an airport): CREATE TABLE boardings( Passport VARCHAR2(8), Day DATE, Flight VARCHAR2(8), LuggageWeight NUMBER(4,2), CONSTRAINT pk PRIMARY KEY(Passport, Day, Flight)); And I would like to make a query in order to see for each flight, which has been the day that has transported the highest amount of weight (keep in mind that a same flight, as RY-1234-VY for example, can make different travels

I need an JPA/SQL expert: EXISTS query on an Inner Join returns wrong result

微笑、不失礼 提交于 2020-03-25 16:11:59
问题 I have three tables and want to: Select all students from the first table, that have at least one connection to the school in district '999' in the second table and at least one connection to the teacher with social_number '101' and at least one to the teacher with number '103' in the third table. The tables are connected through the second table. I created an online sql compiler to show the problem: http://tpcg.io/FIoO79xi This query works fine and as expected, until I add the third EXISTS

I need an JPA/SQL expert: EXISTS query on an Inner Join returns wrong result

ぐ巨炮叔叔 提交于 2020-03-25 16:07:40
问题 I have three tables and want to: Select all students from the first table, that have at least one connection to the school in district '999' in the second table and at least one connection to the teacher with social_number '101' and at least one to the teacher with number '103' in the third table. The tables are connected through the second table. I created an online sql compiler to show the problem: http://tpcg.io/FIoO79xi This query works fine and as expected, until I add the third EXISTS

MySql Query to Avoiding Negative Balance

不想你离开。 提交于 2020-03-05 02:30:32
问题 Is that possible to avoid negative balance using MySql query? I have the following MySql table: trx_no trx_date Opening debit credit 1 2019-10-01 200 0 100 2 2019-10-02 200 0 100 3 2019-10-03 200 100 0 4 2019-10-03 200 400 0 5 2019-10-03 200 0 200 6 2019-10-04 200 0 100 7 2019-10-05 200 0 400 with this query: SELECT trx_no, trx_date, Opening, debit, credit, Opening + (SELECT SUM(t2.credit - t2.debit) FROM MyTable t2 WHERE t2.trx_no <= t1.trx_no) AS balance FROM MyTable t1 ORDER BY trx_no; I