greatest-n-per-group

How do I limit a LEFT JOIN to the 1st result in SQL Server?

我与影子孤独终老i 提交于 2019-12-04 22:28:42
I have a bit of SQL that is almost doing what I want it to do. I'm working with three tables, a Users, UserPhoneNumbers and UserPhoneNumberTypes. I'm trying to get a list of users with their phone numbers for an export. The database itself is old and has some integrity issues. My issue is that there should only ever be 1 type of each phone number in the database but thats not the case. When I run this I get multi-line results for each person if they contain, for example, two "Home" numbers. How can I modify the SQL to take the first phone number listed and ignore the remaining numbers? I'm in

Get the minimum non zero value across multiple columns

▼魔方 西西 提交于 2019-12-04 21:41:10
Let's say I have the following table: CREATE TABLE numbers ( key integer NOT NULL DEFAULT 0, number1 integer NOT NULL DEFAULT 0, number2 integer NOT NULL DEFAULT 0, number3 integer NOT NULL DEFAULT 0, number4 integer NOT NULL DEFAULT 0, CONSTRAINT pk PRIMARY KEY (key), CONSTRAINT nonzero CHECK (key <> 0) ) What I want to retrieve is the minimum value from a given key of all 4 numbers, but ignoring those that are zero. I started with something like this when I figured that I'll have problem with the zeros: SELECT LEAST(number1, number2, number3, number4) FROM numbers WHERE key = 1 For instance,

Return records distinct on one column but order by another column

醉酒当歌 提交于 2019-12-04 19:44:37
I am building a Rails 3 app with a pretty standard message model. I would like to return the most recently created message records for each unique conversation_id. It seems like a fairly simple task, but I have not been able to code or find a working solution. Admittedly, I am not super SQL savvy either (as I have gotten by with mainly Active Record queries thus far). Here is what I'm trying to accomplish. Sample messages table: | id | sender_id | receiver_id | conversation_id | subject | body | created_at | | 1 | * | * | 1 | * | * | 16:01 | | 2 | * | * | 2 | * | * | 17:03 | | 3 | * | * | 1 |

SQL - Select 'n' greatest elements in group

陌路散爱 提交于 2019-12-04 19:01:22
The SQL MAX aggregate function will allow you to select the top element in a group. Is there a way to select the top n elements for each group? For instance, if I had a table of users that held their division rank, and wanted the top two users per division ... Users userId | division | rank 1 | 1 | 1 2 | 1 | 2 3 | 1 | 3 4 | 2 | 3 I would want the query to somehow return users 2,3,4 If it matters, I'm using MySQL. select * from users as t1 where (select count(*) from users as t2 where t1.division = t2.division and t2.rank > t1.rank) <2 order by division,rank SELECT * FROM ( SELECT u1.userid, u1

Select the top 1 row from each group

点点圈 提交于 2019-12-04 18:42:27
问题 I have a table that lists the versions of software that are installed: id | userid | version | datetime ----+--------+---------+------------------------ 111 | 75 | 10075 | 2013-03-12 13:40:58.770 112 | 75 | 10079 | 2013-03-12 13:41:01.583 113 | 78 | 10065 | 2013-03-12 14:18:24.463 114 | 78 | 10079 | 2013-03-12 14:22:20.437 115 | 78 | 10079 | 2013-03-12 14:24:01.830 116 | 78 | 10080 | 2013-03-12 14:24:06.893 117 | 74 | 10080 | 2013-03-12 15:31:42.797 118 | 75 | 10079 | 2013-03-13 07:03:56.157

Select top N rows out of M groups

佐手、 提交于 2019-12-04 14:49:32
I have this table: CREATE TABLE IF NOT EXISTS `catalog_sites` ( `id` int(10) unsigned NOT NULL auto_increment, `cat_id` int(10) unsigned NOT NULL, `date` datetime NOT NULL, `url` varchar(255) NOT NULL, `title` varchar(255) NOT NULL, `description` varchar(255) NOT NULL, `keywords` varchar(255) NOT NULL, `visited` int(10) unsigned NOT NULL, `shown` int(10) unsigned NOT NULL, `meta_try` int(1) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `url` (`url`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; I think my problem is simple, but cant seem to find an appropriate solution.. So, this is a

How do I take a DISTINCT ON subquery that is ordered by a separate column, and make it fast?

萝らか妹 提交于 2019-12-04 14:35:38
(AKA - With a query and data very similar to question " Selecting rows ordered by some column and distinct on another ", how can I get it to run fast). Postgres 11. I have table prediction with (article_id, prediction_date, predicted_as, article_published_date) that represents the output from a classifier over a set of articles. New articles are frequently added to a separate table (Represented by the FK article_id ), and new predictions are added as we tune our classifier. Sample data: | id | article_id | predicted_as | prediction_date | article_published_date | 1009381 | 362718 | negative |

What is the best way to select the first two records of each group by a “SELECT” command?

旧时模样 提交于 2019-12-04 13:48:50
问题 For instance I have the following table: id group data 1 1 aaa 2 1 aaa 3 2 aaa 4 2 aaa 5 2 aaa 6 3 aaa 7 3 aaa 8 3 aaa What is the best way to select the first two records of each group by a "SELECT" command? If there is no good way to do so, what routine do you suggest?(in PHP) (model outcome) 1 1 aaa 2 1 aaa 3 2 aaa 4 2 aaa 6 3 aaa 7 3 aaa I knew that cross-joining by a.id >= b.id in a sub-query can be working but I am looking for a more scalable solution that can be applied on a table with

mysql select 2 row from each group by

冷暖自知 提交于 2019-12-04 13:40:38
i have 2 table with this structure Products id title ----------------- 1 sample 1 2 sample 2 3 sample 3 4 sample 4 5 sample 5 6 sample 6 gallery id typeid name ------------------------------- 1 1 sample for 1 2 1 sample for 1 3 1 sample for 1 4 2 sample for 2 5 2 sample for 2 7 2 sample for 2 8 3 sample for 3 9 3 sample for 3 10 3 sample for 3 11 4 sample for 4 12 4 sample for 4 13 5 sample for 5 14 5 sample for 5 and iwant this for lists of id eg(1,2,3) id typeid name --------------------- 1 1 sample for 1 1 2 sample for 1 2 4 sample for 2 2 5 sample for 2 3 8 sample for 3 3 9 sample for 3

Rails - Distinct ON after a join

做~自己de王妃 提交于 2019-12-04 13:28:05
问题 I am using Rails 4.2 with PostgreSQL. I have a Product model and a Purchase model with Product has many Purchases . I want to find the distinct recently purchased products. Initially I tried: Product.joins(:purchases) .select("DISTINCT products.*, purchases.updated_at") #postgresql requires order column in select .order("purchases.updated_at DESC") This however results in duplicates because it tries to find all tuples where the pair ( product.id and purchases.updated_at ) has a unique value.