greatest-n-per-group

Average of latest N records per group

天大地大妈咪最大 提交于 2019-11-30 08:17:29
问题 My current application calculates a point average based on all records for each user: SELECT `user_id`, AVG(`points`) AS pts FROM `players` WHERE `points` != 0 GROUP BY `user_id` The business requirement has changed and I need to calculate the average based on the last 30 records for each user. The relevant tables have the following structure: table: players; columns: player_id, user_id, match_id, points table: users; columns: user_id The following query does not work, but it does demonstrate

Select latest row for each group from oracle

六月ゝ 毕业季﹏ 提交于 2019-11-30 07:10:50
问题 I have a table with user comments in a guestbook. Columns are: id, user_id, title, comment, timestamp. I need to select the latest row for each user. I have tried to do this with group by but havent managed it because i cant select anything else in the same query where i group by user_id: SELECT user_id, MAX(ts) FROM comments GROUP BY user_id for example in this query i cant add to also select columns id, tilte and comment. How can this be done? 回答1: You can use analytic functions SELECT *

Top N per Group Sql problem in mysql

吃可爱长大的小学妹 提交于 2019-11-30 06:06:24
问题 Please I am having a problem querying for the Top N per category from a data set resembling the one shown below. I have see various thread on this but I am having problem adapting their query to my specific problem. +----+---------------------------------+-------+ | ID | Prod |Cat Id | +----+---------------------------------+-------+ | 1 | kntrn | 1 | | 2 | kntrn e | 1 | | 3 | e spl | 1 | | 4 | spl php | 1 | | 5 | php cicarredgtal | 1 | | 6 | cicarredgtal servecounterstrike | 1 | | 7 |

How to make a SQL query for last transaction of every account?

不打扰是莪最后的温柔 提交于 2019-11-30 05:19:08
Say I have a table "transactions" that has columns "acct_id" "trans_date" and "trans_type" and I want to filter this table so that I have just the last transaction for each account. Clearly I could do something like SELECT acct_id, max(trans_date) as trans_date FROM transactions GROUP BY acct_id; but then I lose my trans_type. I could then do a second SQL call with my list of dates and account id's and get my trans_type back but that feels very cludgy since it means either sending data back and forth to the sql server or it means creating a temporary table. Is there a way to do this with a

MAX function in where clause mysql

ぃ、小莉子 提交于 2019-11-30 04:19:25
How can I use max() function in where clause of a mysql query, I am trying: select firstName,Lastname,MAX(id) as max where id=max; this is giving me an error: Unknown column 'max' in 'where clause' Any Help? Thanks in advance. You can't reference the result of an aggregate function (e.g. MAX() ) in a WHERE clause of the same query. The normative pattern for solving this type of problem is to use an inline view, something like this: SELECT t.firstName , t.Lastname , t.id FROM mytable t JOIN ( SELECT MAX(mx.id) AS max_id FROM mytable mx ) m ON m.max_id = t.id This is just one way to get the

Nested Select statement in MYSQL join

无人久伴 提交于 2019-11-30 03:51:56
SELECT * FROM A JOIN B ON B.ID = A.ID AND B.Time = (SELECT max(Time) FROM B B2 WHERE B2.ID = B.ID) I am trying to join these two tables in MYSQL. Don't pay attention to that if the ID is unique then I wouldn't be trying to do this. I condensed the real solution to paint a simplified picture. I am trying to grab and join the table B on the max date for a certain record. This procedure is getting run by an SSIS package and is saying B2.ID is an unknown column. I do things like this frequently in MSSQL and am new to MYSQL. Anyone have any pointers or ideas? I do this type of query differently,

MySQL Select Statement DISTINCT for Multiple Columns

风格不统一 提交于 2019-11-30 03:41:08
问题 I am currently trying to construct a somewhat tricky MySQL Select Statement. Here is what I am trying to accomplish: I have a table like this: data_table uniqueID stringID subject 1 144 "My Subject" 2 144 "My Subject - New" 3 144 "My Subject - Newest" 4 211 "Some other column" Bascially, what I'd like to do is be able to SELECT/GROUP BY the stringID (picture that the stringID is threaded) and not have it duplicated. Furthermore, I'd like to SELECT the most recent stringID row, (which in the

MySQL return first row of a joined table

旧城冷巷雨未停 提交于 2019-11-30 03:12:27
问题 I have two tables (country & ducks) where the country table has every country in the world and the ducks table has a list of ducks with a country_id field to link to the main country. I'm trying to get a list of only countries with at least one duck in it and with that a single matching record from the ducks table for the highest rated duck within that country. So far I have: SELECT * FROM country c INNER JOIN ducks d ON c.id = d.country_id ORDER BY c.country ASC, d.rating DESC This returns a

SQL Left Join first match only

▼魔方 西西 提交于 2019-11-30 01:19:30
I have a query against a large number of big tables (rows and columns) with a number of joins, however one of tables has some duplicate rows of data causing issues for my query. Since this is a read only realtime feed from another department I can't fix that data, however I am trying to prevent issues in my query from it. Given that, I need to add this crap data as a left join to my good query. The data set looks like: IDNo FirstName LastName ... ------------------------------------------- uqx bob smith abc john willis ABC john willis aBc john willis WTF jeff bridges sss bill doe ere sally

one-to-many query selecting all parents and single top child for each parent

爷,独闯天下 提交于 2019-11-29 23:07:59
There are two SQL tables: Parents: +--+---------+ |id| text | +--+---------+ | 1| Blah | | 2| Blah2 | | 3| Blah3 | +--+---------+ Childs +--+------+-------+ |id|parent|feature| +--+------+-------+ | 1| 1 | 123 | | 2| 1 | 35 | | 3| 2 | 15 | +--+------+-------+ I want to select with single query every row from Parents table and for each one single row from Childs table with relation "parent"-"id" value and the greatest "feature" column value. In this example result should be: +----+------+----+--------+---------+ |p.id|p.text|c.id|c.parent|c.feature| +----+------+----+--------+---------+ | 1 |