greatest-n-per-group

How to make a faster greatest-n-per-group query?

北慕城南 提交于 2019-12-08 06:08:50
问题 I am using this query: SELECT district, id FROM adverts ls GROUP BY district, id HAVING ( SELECT count( * ) FROM adverts WHERE district = ls.district AND id > ls.id ) <5 ORDER BY district, id DESC ; LIMIT 0 , 30 It tooks about 35 seconds. Here is the explanation: id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY ls range NULL i_id 5 NULL 16166 Using index for group-by; Using temporary; Using f... 2 DEPENDENT SUBQUERY ilan_genel ref PRIMARY,i_id,i_ozellik_id,i_tip

Get back the second latest date per ID instead of the latest

隐身守侯 提交于 2019-12-08 03:52:28
问题 So I have an issue with SQlite . I have a table results . I want to write a query now that gives me back not the latest, but the row after that. Let's take a look on an example: ID,searchID,hit,time 1,1,3,1-1-2008 1,1,8,1-1-2009 1,1,4,1-1-2010 1,2,9,1-1-2011 1,2,10,1-1-2009 and I want to get back one time per searchID now (the pre-latest): 1,1,8,1-1-2009 1,2,10,1-1-2009 It is really easy to do it with the last time SELECT searchID, hit, max(time) FROM results group BY searchID But I need the

Group by Max

社会主义新天地 提交于 2019-12-08 02:07:39
问题 SELECT tblIssue.SYMB, tblIssue.[PRCE], tblIssue.[Shareholder] FROM tblIssue I am trying to pull the symb and price for the maximum number of shareholder per symb. For example, I would have only 1 line for ASN where the price would be $60.62. SYMB Price Shareholder ASN $0.00 0 ASN $0.00 51 ASN $25.18 0 ASN $25.26 0 ASN $36.00 0 ASN $60.62 231 ASNL $0.00 101 ASR $0.00 4 ASR $0.00 24 ASR $37.17 13 回答1: SELECT i1.* FROM tblIssue i1 LEFT OUTER JOIN tblIssue i2 ON (i1.[SYMB] = i2.[SYMB] AND i1.

How to max(date) and use the in feature in sql server in one query?

寵の児 提交于 2019-12-08 01:10:55
问题 I have a table such as this id color shade date --- ---- ----- ----- 1 red dark 01/01/1990 2 red light 09/16/2013 3 green light 08/15/2010 4 green dark 09/18/2012 5 maroon dark 08/20/2013 6 white dark 08/31/2013 7 white light 08/30/2012 8 purple light 08/20/2010 I wanted entries for each color with the latest date. So I tried doing: select id, color, shade, max(date) from mytable; This didn't work and gave me error: is invalid in the select list because it is not contained in either an

Group only certain rows with GROUP BY

好久不见. 提交于 2019-12-08 00:33:38
问题 SCHEMA I have the following set-up in MySQL database: CREATE TABLE items ( id SERIAL, name VARCHAR(100), group_id INT, price DECIMAL(10,2), KEY items_group_id_idx (group_id), PRIMARY KEY (id) ); INSERT INTO items VALUES (1, 'Item A', NULL, 10), (2, 'Item B', NULL, 20), (3, 'Item C', NULL, 30), (4, 'Item D', 1, 40), (5, 'Item E', 2, 50), (6, 'Item F', 2, 60), (7, 'Item G', 2, 70); PROBLEM I need to select: All items with group_id that has NULL value, and One item from each group identified by

Selecting the latest per group of items [duplicate]

寵の児 提交于 2019-12-07 17:18:39
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Retrieving the last record in each group i have 2 tables products and cost PRODUCT ProdCode - PK ProdName COST Effectivedate - PK RetailCOst Prodcode i tried this query: SELECT a.ProdCOde AS id, MAX(EffectiveDate) AS edate, RetailCOst AS retail FROM cost a INNER JOIN product b USING (ProdCode) WHERE EffectiveDate <= '2009-10-01' GROUP BY a.ProdCode; uhm yah its showing the right effectivedate but the cost on

SQL to get unique rows in Netezza DB

送分小仙女□ 提交于 2019-12-07 16:29:26
问题 I have a table with rows like: id group_name_code 1 999 2 16 3 789 4 999 5 231 6 999 7 349 8 16 9 819 10 999 11 654 But I want output rows like this: id group_name_code 1 999 2 16 3 789 4 231 5 349 6 819 7 654 Will this query help? select id, distinct(group_name_code) from group_table; 回答1: You seem to want: Distinct values for group_name_code and a sequential id ordered by minimum id per set of group_name_code . Netezza has the DISTINCT key word, but not DISTINCT ON () (Postgres feature):

Inner join with 3 tables

余生长醉 提交于 2019-12-07 16:21:26
问题 I'm working with PHP and PDO, and I need to recolect information joining 3 tables: photos albums album_photos The table have the following structure: photos: photo_id (int) path (varchar) nick (varchar) date (timestamp) albums album_id (int) album_name (varchar) nick (varchar) date (timestamp) album_photos album_id (int) photo_id (int) nick (varchar) date (timestamp) So, I want to show all the albums with a max of 5 photos for each one, where the user nick is 'owner' . To be shown as follows:

Select only newest grouped entries

心已入冬 提交于 2019-12-07 12:39:27
I have a table with data like this: +-----------+-------+------+----------+ | timestamp | event | data | moreData | +-----------+-------+------+----------+ | 100000000 | 1 | 10 | 20 | | 100000001 | 1 | 15 | 10 | | 100000002 | 1 | 30 | 30 | | 100000003 | 1 | 5 | 50 | | 100000004 | 2 | 110 | 120 | | 100000005 | 2 | 115 | 110 | | 100000006 | 2 | 130 | 130 | | 100000007 | 2 | 15 | 150 | +-----------+-------+------+----------+ Now I want to select only the newest rows for each event. So in the end I want to have this result set: +-----------+-------+------+----------+ | timestamp | event | data |

Highest per each group

南楼画角 提交于 2019-12-07 07:10:02
问题 It's hard to show my actual table and data here so I'll describe my problem with a sample table and data: create table foo(id int,x_part int,y_part int,out_id int,out_idx text); insert into foo values (1,2,3,55,'BAK'),(2,3,4,77,'ZAK'),(3,4,8,55,'RGT'),(9,10,15,77,'UIT'), (3,4,8,11,'UTL'),(3,4,8,65,'MAQ'),(3,4,8,77,'YTU'); Following is the table foo : id x_part y_part out_id out_idx -- ------ ------ ------ ------- 3 4 8 11 UTL 3 4 8 55 RGT 1 2 3 55 BAK 3 4 8 65 MAQ 9 10 15 77 UIT 2 3 4 77 ZAK