greatest-n-per-group

Join two tables where table A has a date value and needs to find the next date in B below the date in A

一笑奈何 提交于 2019-12-02 12:41:46
I got this table "A": | id | date | =================== | 1 | 2010-01-13 | | 2 | 2011-04-19 | | 3 | 2011-05-07 | | .. | ... | and this table "B": | date | value | ====================== | 2009-03-29 | 0.5 | | 2010-01-30 | 0.55 | | 2011-08-12 | 0.67 | Now I am looking for a way to JOIN those two tables having the "value" column in "B" mapped to the dates in "A". The tricky part for me here is that table "B" only stores the change date and the new value. Now when I need this value in table "A" the SQL needs to look back what date is the next below the date it is asking the value for. So in the

How to get minimum date by each records from multiple records

岁酱吖の 提交于 2019-12-02 11:27:13
I would like to get the minimum date of each record in my table having multiple entry of date with one primary key. Take a look at my table: CaseNo Entry_date ABC-001 2/12/13 ABC-002 2/09/13 ABC-001 1/01/13 ABC-001 1/31/13 ABC-002 1/01/13 ABC-003 2/01/12 ABC-003 2/18/13 I want to have this result: CaseNo Entry_date Min_date ABC-001 2/12/13 1/01/13 ABC-002 2/09/13 1/09/13 ABC-001 1/01/13 1/01/13 ABC-001 1/31/13 1/01/13 ABC-002 1/09/13 1/09/13 ABC-003 2/01/12 2/01/13 ABC-003 2/18/13 2/01/13 I want to get the minimum date of each CaseNo recorded on my table. I tried this code: Select CaseNo,Entry

Selecting best row in each group based on two columns [duplicate]

眉间皱痕 提交于 2019-12-02 10:13:15
This question already has an answer here: SQL select only rows with max value on a column [duplicate] 27 answers Suppose we have the following table, where each row represents a submission a user made during a programming contest, id is an auto-increment primary key, probid identifies the problem the submission was made to, score is the number of points the submission earned for the problem, and date is the timestamp when the submission was made. Each user can submit as many times as they want to the same problem: +----+----------+--------+-------+------------+ | id | username | probid | score

SQL Server Join with Latest 2 Entries

守給你的承諾、 提交于 2019-12-02 09:49:49
问题 I know the title of the post is bad but hear me out. A question like this arose the other day at work, and while I found a way around it, the problem still haunts me. Lets assume Stackoverflow has only 3 tables. Users ( username ) Comments ( comment, creationdate ) UsersCommentsJoin , this is the join table between the first 2 tables. Now lets say I want to make a query that would return the all the users with the last 2 most recent comments. So the result set would look like this. |username|

How can i put IF ELSE CONDITION in Oracle query?

只愿长相守 提交于 2019-12-02 09:06:55
I have a table, A and B which are shown below, Table A: id idB name faculty B: id name Table B has 2 records as below. SELECT * FROM B; 1, 1, 'First' 2, 2, 'Second' Table A has 8 records as below. SELECT * FROM A; 1, 1, A, IT 2, 1, B, IT 3, 1, C, IT 4, 1, D, Medicine 5, 1, E, Medicine 6, 1, F, Business 7, 1, G, Business 8, 1, H, IT 9, 2, A, Medicine 10, 2, B, Medicine 11, 2, C, Medicine 12, 2, D, Medicine 13, 2, E, Medicine 14, 2, F, Medicine 15, 2, G, Business 16, 2, H, Medicine My question is: How can I select data from table B where faculty should be IT and if there are multiple it should

MySQL find top results for each group

…衆ロ難τιáo~ 提交于 2019-12-02 08:59:45
I have searched a lot, but I can't find an answer or something near. I have a table like this: id int(11) NO PRI auto_increment attribute_id int(11) YES user_id int(11) YES result int(11) YES x10 int(11) YES I need about 5 results from each attribute_id . attribute_id can be any number, so at first, I need to check for the attribute_id , and next I have to check for the results for each attribute_id. I can't find out how I do this without any programming language other than MySQL, but I know you can. Example: attribute_id result 1 200 1 149 1 123 2 322 2 321 2 300 3 ... 3 ... And so on. SELECT

Find second highest record from oracle db [duplicate]

两盒软妹~` 提交于 2019-12-02 08:47:45
This question already has an answer here: How to get second largest or third largest entry from a table 12 answers I have the following data: id date mia 1 1/1/2017 3 1 1/2/2017 1 1 1/3/2017 2 2 1/4/2017 1 2 1/5/2017 4 2 1/6/2017 6 . . . . and so on. If I give input as id=1 I should fetch record of 2017-02-01 and if input id=2 then record of 2017-05-01 i.e record of previous month of the highest date. You could use: SELECT * FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY id ORDER BY mia DESC) AS rn FROM table) sub WHERE rn = 2; you may not define a column named as date , instead i use date_ .

How do I join 4 tables on mysql select statement?

China☆狼群 提交于 2019-12-02 08:37:33
I have 4 tables in MySQL to join. Example $select = $db->query("SELECT * FROM ads WHERE ad_pic='1' AND ad_status = '1' ORDER BY ad_id DESC LIMIT 0,4"); while ($fetch = $db->fetch($select)) { $iquery = $db->query("SELECT * FROM images WHERE img_ads_id = '" . intval($fetch['ad_id']) . "' AND img_status = '1' LIMIT 1"); $thumb = $db->fetch($iquery); $uselect = $db->query("SELECT * FROM users WHERE user_id = '".intval($fetch['ad_userid'])."' AND user_status = '1' LIMIT 1"); $ufetch = $db->fetch($uselect); $cselect = $db->query("SELECT * FROM category WHERE cat_slug = '".safe_func($fetch['ad

How to find previous record [n-per-group max(timestamp) < timestamp]?

醉酒当歌 提交于 2019-12-02 08:30:50
I have a large table containing time series sensor data. Large is anything from a few thousand up to 10M record divided amongst various channels being monitored. For a certain sensor type I need to calculate the time interval between the current and previous reading, i.e. find the largest timestamp prior to the current one. The obvious approaches come to mind, each measured on Core i5 for a channel of 40k entries: Correlated subquery SELECT collect.*, prev.timestamp AS prev_timestamp FROM data AS collect LEFT JOIN data AS prev ON prev.channel_id = collect.channel_id AND prev.timestamp = (

show last comment which just 1 comment per user [duplicate]

扶醉桌前 提交于 2019-12-02 08:03:29
This question already has an answer here: Select the 3 most recent records where the values of one column are distinct 8 answers I have a normal comments table: | id | comment | date | user | post | status | I want to fetch my 10 last comments with just 1 comment per user, I mean something like this: I have this data: | id | comment | date | user | post | | 1 | text1 | 2016-04-01| 1 | 1 | | 2 | text2 | 2016-04-02| 2 | 1 | | 3 | text3 | 2016-04-03| 1 | 2 | | 4 | text4 | 2016-04-04| 4 | 3 | | 5 | text5 | 2016-04-04| 3 | 5 | | 6 | text6 | 2016-04-05| 2 | 4 | | 7 | text7 | 2016-04-07| 5 | 3 | | 8