greatest-n-per-group

Group only certain rows with GROUP BY

爱⌒轻易说出口 提交于 2019-12-06 06:35:44
SCHEMA I have the following set-up in MySQL database: CREATE TABLE items ( id SERIAL, name VARCHAR(100), group_id INT, price DECIMAL(10,2), KEY items_group_id_idx (group_id), PRIMARY KEY (id) ); INSERT INTO items VALUES (1, 'Item A', NULL, 10), (2, 'Item B', NULL, 20), (3, 'Item C', NULL, 30), (4, 'Item D', 1, 40), (5, 'Item E', 2, 50), (6, 'Item F', 2, 60), (7, 'Item G', 2, 70); PROBLEM I need to select: All items with group_id that has NULL value, and One item from each group identified by group_id having the lowest price. EXPECTED RESULTS +----+--------+----------+-------+ | id | name |

what is the query to return Name and Salary of employee Having Max Salary

不问归期 提交于 2019-12-06 05:42:00
what is the query to return Name and Salary of employee Having Max Salary SELECT Name, Salary FROM Minions WHERE Salary = (SELECT Max(Salary) FROM Minions) Note that this will return more than one row if there is more than one employee who have the same max salary select name, salary from (select * from salary_table order by salary desc limit 1) arpit SELECT FirstName, max(salary) FROM Employees WHERE salary = ( SELECT max(salary) FROM employees ) GROUP BY FirstName working in SQL SERVER 2012 Select e.name, e.salary from employee e where not exists (select salary from employee e1 where e

How do I join to the latest record in the table?

大城市里の小女人 提交于 2019-12-06 04:28:12
问题 What I need done is simple... but its 3am and Im probably overlooking the obvious. Im coding a simple forum. One table stores the forum titles, descriptions, etc, while the other stores the posts. In the forum listing, that shows the list of all forums, I want to grab the latest post in each forum, and display the post subject, poster and post ID, and date. Simple. The only problem is, when I join to the posts table, it joins to the first record in the table, not the last, which would denote

SQL-select top 3 values per group WITH CONDITION

匆匆过客 提交于 2019-12-06 03:44:13
问题 I want to pull out top 3 selling products for different product category per tag. Data looks like this: tag | product_name | product_category | order_count tag1 | product1 | category1 | 100 tag1 | product2 | category2 | 80 tag1 | product3 | category2 | 60 tag1 | product4 | category3 | 50 ...... I know how to pull out top 3 selling products per tag using ROW_NUMBER(), but it will return product1,product2,product3. I don't want product3 because it belongs to the same category as product2. I

How to write“greatest n per group” type query, but with additional conditions?

穿精又带淫゛_ 提交于 2019-12-06 02:12:14
问题 I asked a question about making a "greatest n per group" type query yesterday (at the time not knowing it was called "greatest n per group") except I asked how to get the least per group. The table structure was as follows: type name value ===================== 1 item1 1 1 item2 20 2 item3 0 3 item4 5 3 item5 2 3 item6 50 I received several great answers, and the most helpful one was this: SELECT t1.type, t1.name, t1.value FROM mytable t1 LEFT JOIN mytable t2 ON t1.type = t2.type AND t1.value

SQL: how to select single record for multiple id's on the basis of max datetime?

丶灬走出姿态 提交于 2019-12-06 02:05:29
问题 I have the following SQL table, Id WindSpeed DateTime -------------------------------------- 1 1.1 2009-09-14 16:11:38.383 1 1.9 2009-09-15 16:11:38.383 1 2.0 2009-09-16 16:11:38.383 1 1.8 2009-09-17 16:11:38.383 1 1.7 2009-09-19 16:11:38.382 2 1.9 2009-09-19 16:11:38.383 1 1.6 2009-09-19 16:11:38.383 2 1.2 2009-09-20 16:11:38.383 I want to write a query which will return me the following result set from the above table: Id WindSpeed DateTime -------------------------------------- 1 1.6 2009

SQL to get unique rows in Netezza DB

倾然丶 夕夏残阳落幕 提交于 2019-12-06 01:27:26
I have a table with rows like: id group_name_code 1 999 2 16 3 789 4 999 5 231 6 999 7 349 8 16 9 819 10 999 11 654 But I want output rows like this: id group_name_code 1 999 2 16 3 789 4 231 5 349 6 819 7 654 Will this query help? select id, distinct(group_name_code) from group_table; Erwin Brandstetter You seem to want: Distinct values for group_name_code and a sequential id ordered by minimum id per set of group_name_code . Netezza has the DISTINCT key word, but not DISTINCT ON () (Postgres feature) : https://www.ibm.com/support/knowledgecenter/en/SSULQD_7.2.1/com.ibm.nz.dbu.doc/r_dbuser

Inner join with 3 tables

流过昼夜 提交于 2019-12-06 00:25:01
I'm working with PHP and PDO, and I need to recolect information joining 3 tables: photos albums album_photos The table have the following structure: photos: photo_id (int) path (varchar) nick (varchar) date (timestamp) albums album_id (int) album_name (varchar) nick (varchar) date (timestamp) album_photos album_id (int) photo_id (int) nick (varchar) date (timestamp) So, I want to show all the albums with a max of 5 photos for each one, where the user nick is 'owner' . To be shown as follows: album_name_1: [photo_1] [photo_2] [photo_3] [photo_4] [photo_5] album_name_2: [photo_1] [photo_2]

SQLite: How to SELECT “most recent record for each user” from single table with composite key?

血红的双手。 提交于 2019-12-06 00:01:18
I'm not a database guru and feel like I'm missing some core SQL knowledge to grok a solution to this problem. Here's the situation as briefly as I can explain it. Context: I have a SQLite database table that contains timestamped user event records. The records can be uniquely identified by the combination of timestamp and user ID (i.e., when the event took place and who the event is about). I understand this situation is called a "composite primary key." The table looks something like this (with a bunch of other columns removed, of course): sqlite> select Last_Updated,User_ID from records

MySQL - Join tables, retrieve only Max ID

岁酱吖の 提交于 2019-12-05 18:05:41
问题 I've seen solutions for something similar on other posts, but I've been having an issue applying it to my specific problem. Here is my initial join: SELECT service_note_task, comment_id, comment FROM service_note_task LEFT JOIN service_note_task_comments ON service_note_task.service_note_task_id = service_note_task_comments.service_note_task_id; Which results in: +-----------------------------+------------+--------------+ | service_note_task | comment_id | comment | +-------------------------