greatest-n-per-group

ms access, need to get all rows with a distinct column

一曲冷凌霜 提交于 2021-02-19 08:13:05
问题 I have a table called "parts" which stores information on electrical connectors including contacts, backshells etc. all parts that are part of an assembly have a value in a column called "assemblyID". There is also a column called "partDefID", in this column connectors will have a value of 2, contacts will be 3. I need to get rows for all connectors that have a unique assemblyID. It's easy to get rows that represent connectors just by selecting rows with a partDefID of 2 but this will return

SQL get all of the users last search

半城伤御伤魂 提交于 2021-02-11 15:02:35
问题 I can easily get the users searches, but I would like to get all of the users LAST search. For example, the SQL code below will get this specific users last search. select uid, itemDescription, date from SEARCH WHERE date BETWEEN '2020-03-01' AND '2020-03-30' AND uid = "000-000-000-000-000" ORDER BY date DESC LIMIT 1 If I try to edit this and remove the uid = "" , this wont work since it will limit to one search. If I remove the LIMIT 1 also and add a GROUP BY uid , its not picking up the

Get ranking of words over date based on frequency in PostgreSQL

。_饼干妹妹 提交于 2021-02-11 12:59:06
问题 I have a database that stores twitter data: Create Table tweet( ID BIGINT UNIQUE, user_ID BIGINT, created_at TIMESTAMPTZ, tweet TEXT; I'm trying to write a query that goes through the words in tweet for all rows gets the frequency of each word, and returns the top ten most frequent words along with the words' ranking over each date. Example: ("word1":[1,20,22,23,24,25,26,27,28,29,30,29,28,27,26,25,26,27,28,29,30,29,28,29,28,27,28,29,30,30,...], 'word2' [...]) My current query gets the top ten

How to return only latest record on join [duplicate]

℡╲_俬逩灬. 提交于 2021-02-09 02:45:37
问题 This question already has answers here : Fetch the row which has the Max value for a column (35 answers) Closed 5 years ago . I'm joining tables. I only want to return one record from the joining table, based on a date field. Here's a simplified fiddle of what I've done so far: http://sqlfiddle.com/#!3/be0cdd/2 My tables: CUSTOMER | CustomerID | -------------- | 1 | PURCHASE | PurchaseID | CustomerID | ProductID | CreateDate | ArchiveFlag | ----------------------------------------------------

Select data within one month prior to each user's last record

喜夏-厌秋 提交于 2021-02-08 11:25:41
问题 Assume I have a table called "Diary" like this: | id | user_id | recorded_at | record | |----|---------|--------------------------|--------| | 20 | 50245 |2017-10-01 23:00:14.765366| 89 | | 21 | 50245 |2017-12-05 10:00:33.135331| 97 | | 22 | 50245 |2017-12-31 11:50:23.965134| 80 | | 23 | 76766 |2015-10-06 11:00:14.902452| 70 | | 24 | 76766 |2015-10-07 22:40:59.124553| 81 | For each user I want to retrieve the latest row and all rows within one month prior to that. In other words, for user_id

Calculating follower growth over time for each influencer

血红的双手。 提交于 2021-02-08 07:01:47
问题 I have a table with influencers and their follower counter for each day: influencer_id | date | followers 1 | 2020-05-29 | 7361 1 | 2020-05-28 | 7234 ... 2 | 2020-05-29 | 82 2 | 2020-05-28 | 85 ... 3 | 2020-05-29 | 3434 3 | 2020-05-28 | 2988 3 | 2020-05-27 | 2765 ... Let's say I want to calculate how many followers each individual influencer has gained in the last 7 days and get the following table: influencer_id | growth 1 | <num followers last day - num followers first day> 2 | " 3 | " As a

Calculating follower growth over time for each influencer

社会主义新天地 提交于 2021-02-08 07:01:07
问题 I have a table with influencers and their follower counter for each day: influencer_id | date | followers 1 | 2020-05-29 | 7361 1 | 2020-05-28 | 7234 ... 2 | 2020-05-29 | 82 2 | 2020-05-28 | 85 ... 3 | 2020-05-29 | 3434 3 | 2020-05-28 | 2988 3 | 2020-05-27 | 2765 ... Let's say I want to calculate how many followers each individual influencer has gained in the last 7 days and get the following table: influencer_id | growth 1 | <num followers last day - num followers first day> 2 | " 3 | " As a

SQL: getting the max value of one column and the corresponding other columns [duplicate]

有些话、适合烂在心里 提交于 2021-02-02 03:42:27
问题 This question already has answers here : Fetch the row which has the Max value for a column (35 answers) Closed 4 years ago . ID| tag | version -----+-----+----- 1| A | 10 2| A | 20 3| B | 99 3| C | 30 3| F | 40 desired output: 1 A 10 2 A 20 3 B 99 How can I get the max version of every ID and the corresponding tag for that version? Speed is important (I have around 28m rows) so a nested Select won't do it. Also a simple Group by ID with a max(version) doesn't work because I also need the

SQL: getting the max value of one column and the corresponding other columns [duplicate]

喜欢而已 提交于 2021-02-02 03:40:14
问题 This question already has answers here : Fetch the row which has the Max value for a column (35 answers) Closed 4 years ago . ID| tag | version -----+-----+----- 1| A | 10 2| A | 20 3| B | 99 3| C | 30 3| F | 40 desired output: 1 A 10 2 A 20 3 B 99 How can I get the max version of every ID and the corresponding tag for that version? Speed is important (I have around 28m rows) so a nested Select won't do it. Also a simple Group by ID with a max(version) doesn't work because I also need the

Return the highest count record

我的未来我决定 提交于 2021-01-29 16:43:33
问题 The data I am working on looks like below- A_ID B_ID count 123 abcd 1000 123 aaaa 2000 123 aaaa 3000 456 null 50 456 bbbb 6000 456 cccc 450 I want to be able to extract the B_id that has the highest count for a given A_id The result should look like- A_ID B_ID count 123 aaaa 3000 456 bbbb 6000 How to achieve this result? 回答1: One option is to filter with a subquery: select t.* from mytable t where t.count = (select max(t1.count) from mytable t1 where t1.a_id = t.a_id) You can also use window