greatest-n-per-group

SQL - Postgres - return maximum value for latest date [duplicate]

。_饼干妹妹 提交于 2019-12-24 19:17:04
问题 This question already has an answer here : SQL query using WHERE IN clause (1 answer) Closed last year . This might be simple but I'm new to SQL and couldn't find how to do this exactly. I have following table: My requirement is follows: I need, for each Frequency (monthly & weekly), pick latest Date & its maximum Version of that latest Date. Then select records for all Dim & both Frequencies where Date and Version are same as the earlier picked. For example: There will single latest date &

SQL - Postgres - return maximum value for latest date [duplicate]

折月煮酒 提交于 2019-12-24 19:13:20
问题 This question already has an answer here : SQL query using WHERE IN clause (1 answer) Closed last year . This might be simple but I'm new to SQL and couldn't find how to do this exactly. I have following table: My requirement is follows: I need, for each Frequency (monthly & weekly), pick latest Date & its maximum Version of that latest Date. Then select records for all Dim & both Frequencies where Date and Version are same as the earlier picked. For example: There will single latest date &

SQL Left join: selecting the last records in a one-to-many relationship

本小妞迷上赌 提交于 2019-12-24 18:17:00
问题 I have a customer table, and a detail table. I want to want to pull a record for every customer in the table and show the latest detail data on that customer where applicable. Currently my where clause is filtering out customers. I have tried moving the where clause into the left outer join, but I have not been able to get the desired result. It does not seem to filter the product at all when I run the query. SELECT cust.Customer , cust.Company , inv.Date , inv.Product , inv.Units , inv

Select all forums and get latest post too.. how?

我们两清 提交于 2019-12-24 18:05:15
问题 I am trying to write a query to select all my forums and get the corresponding latest post (including the author)... but I failed. This is my structure: forums forum_threads forum_posts ---------- ------------- ----------- id id id parent_forum (NULLABLE) forum_id content name user_id thread_id description title user_id icon views updated_at created_at created_at updated_at last_post_id (NULLABLE) This is my current query: SELECT forum.id, forum.name, forum.description, forum.icon, post_user

Select all forums and get latest post too.. how?

送分小仙女□ 提交于 2019-12-24 18:05:06
问题 I am trying to write a query to select all my forums and get the corresponding latest post (including the author)... but I failed. This is my structure: forums forum_threads forum_posts ---------- ------------- ----------- id id id parent_forum (NULLABLE) forum_id content name user_id thread_id description title user_id icon views updated_at created_at created_at updated_at last_post_id (NULLABLE) This is my current query: SELECT forum.id, forum.name, forum.description, forum.icon, post_user

MySQL JOIN Multiple Tables With LIMIT Last Table By DateTime per result

限于喜欢 提交于 2019-12-24 16:57:14
问题 I got an interesting one. I have 3 tables I am joining. The last table I joined, I want to only see the latest entry by date and not double up my results with the same user and just have different login times. Example (this is a sample table only): SELECT a.user_id a.user_name, b.department, c.last_logon_date_time, c.computer_name FROM table1 a LEFT OUTER JOIN table2 b ON a.user_id = b.user_id LEFT OUTER JOIN table3 c ON c.user_id = c.user_id Below will give me the results I am looking for

MySQL JOIN Multiple Tables With LIMIT Last Table By DateTime per result

点点圈 提交于 2019-12-24 16:55:24
问题 I got an interesting one. I have 3 tables I am joining. The last table I joined, I want to only see the latest entry by date and not double up my results with the same user and just have different login times. Example (this is a sample table only): SELECT a.user_id a.user_name, b.department, c.last_logon_date_time, c.computer_name FROM table1 a LEFT OUTER JOIN table2 b ON a.user_id = b.user_id LEFT OUTER JOIN table3 c ON c.user_id = c.user_id Below will give me the results I am looking for

Mysql. Order locations and join listings randomly

时光怂恿深爱的人放手 提交于 2019-12-24 16:20:13
问题 I have two tables: locations and listings. locations id title address latitude longitude listings id location info status SELECT locations.title, locations.address, ( 3959 * acos( cos( radians('".$center_lat."') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('".$center_lng."') ) + sin( radians('".$center_lat."') ) * sin( radians( latitude ) ) ) ) AS distance FROM locations ORDER BY distance This will list the locations in order by location with the users provided

SQL two criteria from one group-by

Deadly 提交于 2019-12-24 12:34:59
问题 I have a table with some "functionally duplicate" records - different IDs, but the 4 columns of "user data" (of even more columns) are identical. I've got a query working that will select all records that have such duplicates. Now I want to select, from each group of duplicates, first any of them that have column A not null - and I've verified from the data that there are at most 1 such rows per group - and if there are none in this particular group, then the minimum of column ID . How do I

Filtering Database Results to Top n Records for Each Value in a Lookup Column

若如初见. 提交于 2019-12-24 09:04:05
问题 Let's say I have two tables in my database. TABLE:Categories ID|CategoryName 01|CategoryA 02|CategoryB 03|CategoryC and a table that references the Categories and also has a column storing some random number. TABLE:CategoriesAndNumbers CategoryType|Number CategoryA|24 CategoryA|22 CategoryC|105 .....(20,000 records) CategoryB|3 Now, how do I filter out this data? So, I want to know what the 3 smallest numbers are out of each category and delete the rest. The end result would be like this: