greatest-n-per-group

SQL Server - pull X random records per state

泄露秘密 提交于 2020-01-03 08:14:05
问题 I have a table with records for each zip code in the united states. For the purposes of displaying on a map, I need to select X random records per state. How would I go about doing this? 回答1: Use: WITH sample AS ( SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.state ORDER BY NEWID()) AS rank FROM ZIPCODES t) SELECT s.* FROM sample s WHERE s.rank <= 5 回答2: SELECT * FROM ZipCodes ORDER BY NEWID() 来源: https://stackoverflow.com/questions/3998573/sql-server-pull-x-random-records-per-state

Order result form Mysql group by

戏子无情 提交于 2020-01-03 03:32:11
问题 Ok i got this table +-----+-----------------------------+-------------+-------------+----------+---------+ | id | title | created | updated | category | content | +-----+-----------------------------+---------------------+---------------------+----+ | 423 | What If I Get Sick and Die? | 2008-12-30 | 2009-03-11 | angst | NULL | | 524 | Uncle Karl and the Gasoline | 2009-02-28 | NULL | humor | NULL | | 537 | Be Nice to Everybody | 2009-03-02 | NULL | advice | NULL | | 573 | Hello Statue | 2009

MySQL - Max() return wrong result

不问归期 提交于 2020-01-03 03:09:06
问题 I tried this query on MySQL server (5.1.41)... SELECT max(volume), dateofclose, symbol, volume, close, market FROM daily group by market I got this result: max(volume) dateofclose symbol volume close market 287031500 2010-07-20 AA.P 500 66.41 AMEX 242233000 2010-07-20 AACC 16200 3.98 NASDAQ 1073538000 2010-07-20 A 4361000 27.52 NYSE 2147483647 2010-07-20 AAAE.OB 400 0.01 OTCBB 437462400 2010-07-20 AAB.TO 31400 0.37 TSX 61106320 2010-07-20 AA.V 0 0.24 TSXV As you can see, the maximum volume is

Min value from Database in MySQL

♀尐吖头ヾ 提交于 2020-01-02 23:10:35
问题 Am trying to find the min value from past 30 days, in my table there is one entry for every day, am using this query SELECT MIN(low), date, low FROM historical_data WHERE name = 'bitcoin' ORDER BY STR_TO_DATE(date,'%d-%m-%Y') DESC LIMIT 7 But this value not returing the correct value. The structure of my table is Table structure And table data which is store is like this Table data style Now what i need is to get the minimum low value. But my query not working it give me wrong value which

Min value from Database in MySQL

為{幸葍}努か 提交于 2020-01-02 23:10:13
问题 Am trying to find the min value from past 30 days, in my table there is one entry for every day, am using this query SELECT MIN(low), date, low FROM historical_data WHERE name = 'bitcoin' ORDER BY STR_TO_DATE(date,'%d-%m-%Y') DESC LIMIT 7 But this value not returing the correct value. The structure of my table is Table structure And table data which is store is like this Table data style Now what i need is to get the minimum low value. But my query not working it give me wrong value which

SQLite: return only top 2 results within each group

眉间皱痕 提交于 2020-01-02 04:25:35
问题 I checked other solutions to similar problems, but sqlite does not support row_number() and rank() functions or there are no examples which involve joining multiple tables, grouping them by multiple columns and returning only top N results for each group at the same time. Here's the code i run db = sqlite3.connect('mydb') cursor = db.cursor() cursor.execute( ''' CREATE TABLE orders( id INTEGER PRIMARY KEY, product_id INTEGER, client_id INTEGER ) ''' ) cursor.execute( ''' CREATE TABLE clients(

Grouping by a Top N in MySQL

大兔子大兔子 提交于 2020-01-01 19:21:11
问题 There are a lot of SQL Top N questions on stackoverflow but I can't seem to find one that matches the situation I'm having. I would like to perform some grouping within a top n query. My data looks like this (obviously with fake values). MY_DATE IP_ADDRESS 1/1/09 999.999.999.999 1/1/09 999.999.999.999 1/1/09 999.999.999.998 ... a lot more rows The date range for the table covers several months and has many thousands of rows per month. What I would like to do is have a single query tell me

select top n record from each group sqlite

吃可爱长大的小学妹 提交于 2020-01-01 11:02:44
问题 I am trying to select top 2 records from a database table result that looks like this SubjectId | StudentId | Levelid | total ------------------------------------------ 1 | 1 | 1 | 89 1 | 2 | 1 | 77 1 | 3 | 1 | 61 2 | 4 | 1 | 60 2 | 5 | 1 | 55 2 | 6 | 1 | 45 i tried this query SELECT rv.subjectid, rv.total, rv.Studentid, rv.levelid FROM ResultView rv LEFT JOIN ResultView rv2 ON ( rv.subjectid = rv2.subjectid AND rv.total <= rv2.total ) GROUP BY rv.subjectid, rv.total, rv.Studentid HAVING

select top n record from each group sqlite

核能气质少年 提交于 2020-01-01 11:01:47
问题 I am trying to select top 2 records from a database table result that looks like this SubjectId | StudentId | Levelid | total ------------------------------------------ 1 | 1 | 1 | 89 1 | 2 | 1 | 77 1 | 3 | 1 | 61 2 | 4 | 1 | 60 2 | 5 | 1 | 55 2 | 6 | 1 | 45 i tried this query SELECT rv.subjectid, rv.total, rv.Studentid, rv.levelid FROM ResultView rv LEFT JOIN ResultView rv2 ON ( rv.subjectid = rv2.subjectid AND rv.total <= rv2.total ) GROUP BY rv.subjectid, rv.total, rv.Studentid HAVING

select top n record from each group sqlite

让人想犯罪 __ 提交于 2020-01-01 11:01:06
问题 I am trying to select top 2 records from a database table result that looks like this SubjectId | StudentId | Levelid | total ------------------------------------------ 1 | 1 | 1 | 89 1 | 2 | 1 | 77 1 | 3 | 1 | 61 2 | 4 | 1 | 60 2 | 5 | 1 | 55 2 | 6 | 1 | 45 i tried this query SELECT rv.subjectid, rv.total, rv.Studentid, rv.levelid FROM ResultView rv LEFT JOIN ResultView rv2 ON ( rv.subjectid = rv2.subjectid AND rv.total <= rv2.total ) GROUP BY rv.subjectid, rv.total, rv.Studentid HAVING