greatest-n-per-group

mySQL Returning the top 5 of each category

馋奶兔 提交于 2019-11-26 20:25:25
I want to be able to return 5 menuitem per menu. I've tried this several script but had no luck. here are the tables menus ------- menuid int() profileName varchar(35) menuitems ----------- itemid int() name varchar(40) Here what I have now. I'm getting the error message with the script below. Error: Subquery returns more then 1 row. SELECT m.profilename, name FROM menus m WHERE (SELECT name from menuitems s where m.menuid = s.menuid limit 5) Any suggestion is great appreciated. You have to use side effecting variables for this SELECT profilename, name FROM ( SELECT m.profilename, s.name, @r:

Selecting rows ordered by some column and distinct on another

佐手、 提交于 2019-11-26 20:10:54
问题 Related to - PostgreSQL DISTINCT ON with different ORDER BY I have table purchases (product_id, purchased_at, address_id) Sample data: | id | product_id | purchased_at | address_id | | 1 | 2 | 20 Mar 2012 21:01 | 1 | | 2 | 2 | 20 Mar 2012 21:33 | 1 | | 3 | 2 | 20 Mar 2012 21:39 | 2 | | 4 | 2 | 20 Mar 2012 21:48 | 2 | The result I expect is the most recent purchased product (full row) for each address_id and that result must be sorted in descendant order by the purchased_at field: | id |

T-SQL: Selecting Column Based on MAX(Other Column)

不羁岁月 提交于 2019-11-26 19:46:45
I'm hoping there's a simple way to do this without using a sub-query: Scenario: You have "TableA" with columns "Key", "SubKey", and "Value". I need to get the "Value" of the MAX("SubKey") for a given "Key". So if the Table contained the rows: KEY SUBKEY VALUE 1 1 100 1 2 200 1 3 300 For Key = 1, I need the value 300. I was hoping to do something like this: SELECT VALUE FROM TableA WHERE Key = 1 HAVING SubKey = MAX(SubKey) But that's a no-go. Is there a way to do this without doing a 'WHERE SubKey = (subselect for max subkey)'? Using a self join: This will return all the values with subkey

mysql select top n max values

一世执手 提交于 2019-11-26 19:03:58
How can you select the top n max values from a table? For a table like this: column1 column2 1 foo 2 foo 3 foo 4 foo 5 bar 6 bar 7 bar 8 bar For n=2, the result needs to be: 3 4 7 8 The approach below selects only the max value for each group. SELECT max(column1) FROM table GROUP BY column2 Returns: 4 8 For n=2 you could SELECT max(column1) m FROM table t GROUP BY column2 UNION SELECT max(column1) m FROM table t WHERE column1 NOT IN (SELECT max(column1) WHERE column2 = t.column2) for any n you could use approaches described here to simulate rank over partition. EDIT: Actually this article will

How can I select rows with most recent timestamp for each key value?

情到浓时终转凉″ 提交于 2019-11-26 18:53:34
问题 I have a table of sensor data. Each row has a sensor id, a timestamp, and other fields. I want to select a single row with latest timestamp for each sensor, including some of the other fields. I thought that the solution would be to group by sensor id and then order by max(timestamp) like so: SELECT sensorID,timestamp,sensorField1,sensorField2 FROM sensorTable GROUP BY sensorID ORDER BY max(timestamp); This gives me an error saying that "sensorField1 must appear in the group by clause or be

Select a Column in SQL not in Group By

六月ゝ 毕业季﹏ 提交于 2019-11-26 18:10:49
问题 I have been trying to find some info on how to select a non-aggregate column that is not contained in the Group By statement in SQL, but nothing I've found so far seems to answer my question. I have a table with three columns that I want from it. One is a create date, one is a ID that groups the records by a particular Claim ID, and the final is the PK. I want to find the record that has the max creation date in each group of claim IDs. I am selecting the MAX(creation date), and Claim ID (cpe

How do I limit the number of rows per field value in SQL?

坚强是说给别人听的谎言 提交于 2019-11-26 16:46:21
问题 For example, I have a table like this in Hive: 1 1 1 4 1 8 2 1 2 5 3 1 3 2 and I want to only return the first two rows of each unique value of the first column. I want this to be able to limit the amount of data that I transfer from Hive into MySQL for reporting purposes. I'd like a single HiveQL query that gives me this: 1 1 1 4 2 1 2 5 3 1 3 2 回答1: Unluckily mysql doesn't have Analytical Functions. So you have to play with variables. Supposing you have an autoincrement field: mysql> create

How to get the last record per group in SQL

夙愿已清 提交于 2019-11-26 16:34:28
I am facing a rather interesting problem. I have a table with the following structure: CREATE TABLE [dbo].[Event] ( Id int IDENTITY(1,1) NOT NULL, ApplicationId nvarchar(32) NOT NULL, Name nvarchar(128) NOT NULL, Description nvarchar(256) NULL, Date nvarchar(16) NOT NULL, Time nvarchar(16) NOT NULL, EventType nvarchar(16) NOT NULL, CONSTRAINT Event_PK PRIMARY KEY CLUSTERED ( Id ) WITH ( PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON ) ) So the issue is that I have to display this data in a grid. There are two requirements. The

Laravel - Get the last entry of each UID type

对着背影说爱祢 提交于 2019-11-26 14:56:32
问题 I have a table that has 100's of entries for over 1000 different products, each identified by a unique UID. ID UID MANY COLUMNS CREATED AT 1 dqwdwnboofrzrqww1 ... 2018-02-11 23:00:43 2 dqwdwnboofrzrqww1 ... 2018-02-12 01:15:30 3 dqwdwnbsha5drutj5 ... 2018-02-11 23:00:44 4 dqwdwnbsha5drutj5 ... 2018-02-12 01:15:31 5 dqwdwnbvhfg601jk1 ... 2018-02-11 23:00:45 6 dqwdwnbvhfg601jk1 ... 2018-02-12 01:15:33 ... I want to be able to get the last entry for each UID. ID UID MANY COLUMNS CREATED AT 2

SQL: Find the max record per group [duplicate]

人走茶凉 提交于 2019-11-26 14:44:24
Possible Duplicate: Retrieving the last record in each group I have one table, which has three fields and data. Name , Top , Total cat , 1 , 10 dog , 2 , 7 cat , 3 , 20 horse , 4 , 4 cat , 5 , 10 dog , 6 , 9 I want to select the record which has highest value of Total for each Name , so my result should be like this: Name , Top , Total cat , 3 , 20 horse , 4 , 4 Dog , 6 , 9 I tried group by name order by total, but it give top most record of group by result. Can anyone guide me, please? select Name, Top, Total from sometable where Total = (select max(Total) from sometable i where i.Name =