greatest-n-per-group

SQLAlchemy: How to use group_by() correctly (only_full_group_by)?

折月煮酒 提交于 2020-08-26 07:45:05
问题 I'm trying to use the group_by() function of SQLAlchemy with the mysql+mysqlconnector engine: rows = session.query(MyModel) \ .order_by(MyModel.published_date.desc()) \ .group_by(MyModel.category_id) \ .all() It works fine with SQLite, but for MySQL I get this error: [42000][1055] Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column '...' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

How to break ties when comparing columns in SQL

情到浓时终转凉″ 提交于 2020-08-10 23:57:53
问题 I am trying to delete duplicates in Postgres. I am using this as the base of my query: DELETE FROM case_file as p WHERE EXISTS ( SELECT FROM case_file as p1 WHERE p1.serial_no = p.serial_no AND p1.cfh_status_dt < p.cfh_status_dt ); It works well, except that when the dates cfh_status_dt are equal then neither of the records are removed. For rows that have the same serial_no and the date is the same, I would like to keep the one that has a registration_no (if any do, this column also has NULLS

How to break ties when comparing columns in SQL

非 Y 不嫁゛ 提交于 2020-08-10 23:56:13
问题 I am trying to delete duplicates in Postgres. I am using this as the base of my query: DELETE FROM case_file as p WHERE EXISTS ( SELECT FROM case_file as p1 WHERE p1.serial_no = p.serial_no AND p1.cfh_status_dt < p.cfh_status_dt ); It works well, except that when the dates cfh_status_dt are equal then neither of the records are removed. For rows that have the same serial_no and the date is the same, I would like to keep the one that has a registration_no (if any do, this column also has NULLS

SQL Select to make a value appear only once

寵の児 提交于 2020-06-27 08:21:16
问题 For those that have been helping me along the way with this twitter-clone, thank you!! With your help, I've managed to get most things working and finally now to the last steps of the followers function. Right now, I have a dataset with the following fields: username, tweet, date An example of the data could look like: Username Tweet Date kenny hi! 2011-10-07 19:07:00 stan hello 2011-10-05 18:07:00 kenny looks like rain 2011-10-05 17:07:00 stan hello 2011-10-05 14:07:00 cartman authoritay!

Row sorting and selection logic in Python on Sqlite db

邮差的信 提交于 2020-06-16 19:06:02
问题 hello Thanks for taking the time to go through my question. I work in the budget space for a small city and during these precarious time, I am learning some python for maybe in the future helping me with some financial data modelling. We use SAP currently but i also wanted to learn a new language. I need some pointers on where to look for certain answers. for ex, I made a database with a few million records, sorted by date and time. I was able to strip off the data I did not need and now have

Getting Max date from multiple table after INNER JOIN

帅比萌擦擦* 提交于 2020-06-11 10:54:06
问题 I have two following tables table 1) ID | HOTEL ID | NAME 1 100 xyz 2 101 pqr 3 102 abc table 2) ID | BOOKING ID | DEPARTURE DATE | AMOUNT 1 1 2013-04-12 100 2 1 2013-04-14 120 3 1 2013-04-9 90 4 2 2013-04-14 100 5 2 2013-04-18 150 6 3 2013-04-12 100 I want to get reault in mysql such that it take the row from table two with MAX DEPARTURE DATE. ID | BOOKING ID | DEPARTURE DATE | AMOUNT 2 1 2013-04-14 120 5 2 2013-04-18 150 6 3 2013-04-12 100 回答1: SELECT b.ID, b.BookingID, a.Name, b

Query for latest price per seller per product [duplicate]

£可爱£侵袭症+ 提交于 2020-06-01 05:10:51
问题 This question already has answers here : Retrieving the last record in each group - MySQL (27 answers) SQL select only rows with max value on a column [duplicate] (27 answers) Closed 6 days ago . I have a database table which looks as following: id product_id product_name price date seller 1 646 Product 1 1 2020-05-20 Seller-A 2 1554 Product 2 1.50 2020-05-23 Seller-B 3 646 Product 1 2 2020-05-22 Seller-C 4 646 Product 1 2.5 2020-05-23 Seller-A As a result I would like to get the latest info

PostgreSQL MAX and GROUP BY

拈花ヽ惹草 提交于 2020-05-24 08:11:50
问题 I have a table with id , year and count . I want to get the MAX(count) for each id and keep the year when it happens, so I make this query: SELECT id, year, MAX(count) FROM table GROUP BY id; Unfortunately, it gives me an error: ERROR: column "table.year" must appear in the GROUP BY clause or be used in an aggregate function So I try: SELECT id, year, MAX(count) FROM table GROUP BY id, year; But then, it doesn't do MAX(count) , it just shows the table as it is. I suppose because when grouping

Something like SELECT DISTINCT ON, but for N > 1

非 Y 不嫁゛ 提交于 2020-05-17 06:26:12
问题 If I want to get the first row, or the most recent single row on a field in postgres, select distinct on seems great, see this answer. DISTINCT ON is a syntax for wanting exactly 1 entry. But what if I want the N most recent entries? How would I transform this: CREATE VIEW your_view AS SELECT DISTINCT ON (id) * FROM your_table a ORDER BY id, date DESC; But for "Select the most recent n=2 entries per id" rather than "select the most recent n=1 entries per id?" ? I assume it's an group by