greatest-n-per-group

Need help with a complex Join statement in SQL

给你一囗甜甜゛ 提交于 2020-01-01 06:26:52
问题 How can you join between a table with a sparse number of dates and another table with an exhaustive number of dates such that the gaps between the sparse dates take the values of the previous sparse date? Illustrative example: PRICE table (sparse dates): date itemid price 2008-12-04 1 $1 2008-12-11 1 $3 2008-12-15 1 $7 VOLUME table (exhaustive dates): date itemid volume_amt 2008-12-04 1 12345 2008-12-05 1 23456 2008-12-08 1 34567 2008-12-09 1 ... 2008-12-10 1 2008-12-11 1 2008-12-12 1 2008-12

PostgreSQL: Returning the most recent rows grouping by a foreign key column

牧云@^-^@ 提交于 2019-12-31 04:51:21
问题 I have a table that looks similar to this: credit +---------+----------------+-------------+--------------+-------------------------+ | id (PK) | person_id (FK) | transaction | total credit | date_time | +---------+----------------+-------------+--------------+-------------------------+ | 345 | 1 | -1.00 | 34.50 | 2018-08-29 12:00:00.000 | | 897 | 1 | 5.45 | 39.95 | 2018-08-29 12:34:00.000 | | 378 | 2 | 0.01 | 0.01 | 2018-08-29 08:00:00.000 | | 789 | 2 | 20.00 | 20.01 | 2018-08-29 09:00:00

PostgreSQL: Returning the most recent rows grouping by a foreign key column

旧街凉风 提交于 2019-12-31 04:51:13
问题 I have a table that looks similar to this: credit +---------+----------------+-------------+--------------+-------------------------+ | id (PK) | person_id (FK) | transaction | total credit | date_time | +---------+----------------+-------------+--------------+-------------------------+ | 345 | 1 | -1.00 | 34.50 | 2018-08-29 12:00:00.000 | | 897 | 1 | 5.45 | 39.95 | 2018-08-29 12:34:00.000 | | 378 | 2 | 0.01 | 0.01 | 2018-08-29 08:00:00.000 | | 789 | 2 | 20.00 | 20.01 | 2018-08-29 09:00:00

Select Earliest Date and Time from List of Distinct User Sessions

若如初见. 提交于 2019-12-31 02:24:11
问题 I have a table of user access sessions which records website visitor activity: accessid, userid, date, time, url I'm trying to retrieve all distinct sessions for userid 1234, as well as the earliest date and time for each of those distinct sessions. SELECT DISTINCT accessid, date, time FROM accesslog WHERE userid = '1234' GROUP BY accessid This gives me the date and time of a random row within each distinct accessid. I've read a number of posts recommending the use of min() and max(), so I

How to use Group By and self-join to return min, max, open, and close daily price restult set?

ぃ、小莉子 提交于 2019-12-30 11:19:05
问题 SOLVED All hail StackOverlow! While I was gone, people left 2 solutions (thanks guys--what is the protocol for handing out karma for two working solutions?) Here is the solution that I came back to post. it is derived from yet ANOTHER StackOver solution: How to fetch the first and last record of a grouped record in a MySQL query with aggregate functions? ...and my adaptation is: SELECT DATE_FORMAT(`DTE`, "%m/%d/%Y") AS trading_day, MIN(`PRICE`) AS min_price, MAX(`PRICE`) AS max_price,

selecting a column based on a minimum value of another column

时光怂恿深爱的人放手 提交于 2019-12-30 10:34:12
问题 I have the following table. test_type | brand | model | band | firmware_version | avg_throughput -----------+---------+--------+------+-----------------+---------------- 1client | Linksys | N600 | 5ghz | 1 | 66.94 1client | Linksys | N600 | 5ghz | 2 | 94.98 1client | Linksys | N600 | 5ghz | 4 | 132.40 1client | Linksys | EA6500 | 5ghz | 1 | 216.46 1client | Linksys | EA6500 | 5ghz | 2 | 176.79 1client | Linksys | EA6500 | 5ghz | 4 | 191.44 I'd like to select the avg_throughput of each model

selecting a column based on a minimum value of another column

梦想的初衷 提交于 2019-12-30 10:34:06
问题 I have the following table. test_type | brand | model | band | firmware_version | avg_throughput -----------+---------+--------+------+-----------------+---------------- 1client | Linksys | N600 | 5ghz | 1 | 66.94 1client | Linksys | N600 | 5ghz | 2 | 94.98 1client | Linksys | N600 | 5ghz | 4 | 132.40 1client | Linksys | EA6500 | 5ghz | 1 | 216.46 1client | Linksys | EA6500 | 5ghz | 2 | 176.79 1client | Linksys | EA6500 | 5ghz | 4 | 191.44 I'd like to select the avg_throughput of each model

Selecting the most common value from relation - SQL statement

瘦欲@ 提交于 2019-12-30 02:14:05
问题 I have a table within my database that has many records, some records share the same value for one of the columns. e.g. | id | name | software | ______________________________ | 1 | john | photoshop | | 2 | paul | photoshop | | 3 | gary | textmate | | 4 | ade | fireworks | | 5 | fred | textmate | | 6 | bob | photoshop | I would like to return the value of the most common occurring piece of software, by using an SQL statement. So in the example above the required SQL statement would return

Mysql select last row for each group

偶尔善良 提交于 2019-12-29 06:36:30
问题 SELECT MAX('time'), c_id, message, state, time FROM message WHERE receive = 1 GROUP BY c_id I have a mysql query, I try to select the last row of each group, but it's not working. c_id is the group. It select the first row of each group now. 回答1: First, you should not escape the column name with single quote since it is not string literal. Second, you can do subquery which separately get the latest time for every c_id and join it back with the original table to get the other columns. SELECT a

SQL - Give me 3 hits for each type only

非 Y 不嫁゛ 提交于 2019-12-28 13:35:35
问题 I have some kind of impossible request :). I have a table where one of the columns is named type . I would like to SELECT 3 records for each type in that column. Is that possible? Note also that I'm using MySQL and Sphinx. UPDATE: Table structure id title type 1 AAAA string1 2 CCCC string2 3 EEEE string2 4 DDDD string2 5 FFFF string2 6 BBBB string2 6 BBBB string2 What I want my MySQL to return is (up to 3 records for each type ordered by title): id title type 1 AAAA string1 6 BBBB string2 2