greatest-n-per-group

Trying to join tables and select by one distinct max value

Hi, I am trying to pull multiple columns from 3 joined tables, but I want the result set to contain only one distinct "data entry" per p.id (the pet id which is a foreign key in the data entry table). The issue i have is that there could be two data entries, numbered 1 and 2, belonging to a pet - the query has to just pick the data entry...

Group-wise-max - hierarchical data query help

Hi, the first part of this problem was solved by some good help on here yesterday, but I've been struggling today to complete the query I need. I am trying to pull multiple columns from 5 joined tables based on some conditions, but I want the result set to contain only one distinct "data entry" per p.id (the pet id which is a foreign key...

SQL - How to join top ordered results from pictures table with product table ?

I have a products table, and a pictures table as such: Products id name model Pictures id pid url order The picture for each pid with the highest order is the cover picture. There can be more than 1 picture per product. I want to join this result when I query the products, so it would show these r...

Find the top value for each parent

I'm sure this is a common request but I wouldn't know how to ask for it formally. I encountered this a long time ago when I was in the Army. A soldier has multiple physical fitness tests but the primary test that counts in the most recent. The soldier also has multiple marksmanship qualifications but only the most recent qualification...

MySQL Refactoring with GROUP BY and HAVING

Hi, I'm doing some refactoring work on PHP/MySQL code and stumbled across an interesting problem, and as of right now, I can't find a proper solution for it. I have this table: CREATE TABLE example ( id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, userID INT UNSIGNED NOT NULL, timestamp INT UNSIGNED NOT N...

SQL: one to many, select just one.

I am writing a small tool for managing the people registered to an association. I have a table USERS and a table CARDS. Each user can have more than one card (because cards expire). With one query I would like to extract ALL user information plus ALL card information only for the most recent card (field: ISSUEDATE). Here is a very bas...

MySQL LIMIT in a Correllated Subquery

I have a correlated subquery that will return a list of quantities, but I need the highest quantity, and only the highest. So I tried to introduce an order by and a LIMIT of 1 to achieve this, but MySQL throws an error stating it doesn't yet support limits in subqueries. Any thoughts on how to work around this? SELECT Product.Name, Prod...

Finding data according to relevency and then take 2 result from diff domain

I am having so much domain name in my DB, for example www.yahoo.com/duniya.html www.yahoo.com/hero.html www.123musiq.com/ www.123musiq.com/home.html www.123musiq.com/horo.html www.123musiq.com/yuva.html www.sensongs.com/ www.sensongs.com/hindi.html www.sensongs.com/yuva.html www.sensongs.com/duniya.html www.sensongs.pk/duniya1.html i ...

Mysql access main table inside right joined subquery

SELECT id,region FROM ads RIGHT JOIN (SELECT * FROM ads a2 WHERE a2.region=ads.region LIMIT 4) AS regions ON regions.id=ads.id Says "unknown column ads.region". How to access main table column region? I need to fetch 4 rows for each region ...

Mysql alternative for LIMIT inside subquery in mysql 5.1.49

SELECT student_id FROM `students` AS s1 WHERE student_id IN (SELECT s2.student_id FROM `students` AS s2 WHERE s1.year_of_birth = s2.year_of_birth LIMIT 10) Can't process this query on my server. It drops errors, that says that this version of mysql doesn't support limit inside subqueries etc(ERROR 1235). Is there any solu...

Getting only the default image for a property

I am trying to retrieve a list of properties and to do this I have to tie together 4 tables. The tables are: property, property_type, state, and photo. The problem is getting ONLY the default image for a property. The photo with the lowest rank number should be the default. So if there are 2 photos for property 10, than the one with t...

Cannot write mysql query i want

Hi, I have a table (Counts) with many entries like this: ID | userid | count | entrydate where ID is an autoinc PK, userid refers to my Users table, count is a random integer, and entrydate is a unix timestamp. I also have a table called Listmembers: listid | userid where both fields together are PK, and listid refers to some lis...

Getting the Next Available Row

How can I get a List all the JobPositionNames having the lowest jobPositionId when ContactId = 1 Tablel : | JobPositionId | JobPositionName | JobDescriptionId | JobCategoryId | ContactId --------------------------------------------------------------------------------- 1 | Audio Cables | 1 | 1 ...

What is a fast way of joining two tables and using the first table column to "filter" the second table?

Hello! I am trying to develop a SQL Server 2005 query but I'm being unsuccessful at the moment. I trying every different approach that I know, like derived tables, sub-queries, CTE's, etc, but I couldn't solve the problem. I won't post the queries I tried here because they involve many other columns and tables, but I will try to explain ...

Getting a MySQL group by query to display the row in that group with the highest value

I'm trying to figure out how to query my database so that it will essentially first ORDER my results and then GROUP them... This question seems to be slightly common and I have found examples but I still don't quite grasp the 'how' to do this and use the examples in my own situation.... So all help is definitely appreciated. Here are my...

A mysql query that gives the most recent revision for each entity

I have my data split across two tables. One is the entity table where it describes the basics of the entity, and the revision table where the data of an entity resides. If I know the entity I can query for the most recent version: SELECT * FROM `entities` JOIN (SELECT * FROM `revisions` WHERE `entity_fk` = ? ...

Finding rows with the maximum

Hi - I have a pretty simple table in SQLite, with the following schema: CREATE TABLE IF NOT EXISTS Palettes (id INTEGER PRIMARY KEY AUTOINCREMENT, class TEXT, count INTEGER, name TEXT); These represent color palettes, and several palettes can have the same name, but different counts (i.e. sizes). What I wa...

mysql left join ascending

I have a query: SELECT reply.id, reply.message, reply.userid, reply.date, medal.id, medal.url, medal.name, user.id, user.name AS username FROM posts AS reply LEFT JOIN users AS user ON reply.userid = user.id LEFT JOIN medals AS medal ON medal.userid ...

How to filter mysql duplicated id rows and retrieve the one with latest timestamp?

I have a mysql table with entries like this: id name value date 1 results1 1000000 2010-06-02 01:31:12 2 results2 600000 2010-09-03 05:42:54 1 results1 1200000 2010-09-06 02:14:36 How can I SELECT all and filter multiple rows that have the same id, selecting only the one with latest date? The "date" column d...

Query to get the max of the max records

Hello, I Have a table with the following columns: patient_id, visit_id, and visit_date. How can I write a query to get the max(visit_id) on the most recent visit date for each patient? (several visit_id could occur on the same date for the same patient) basically, I want to end up with NO duplicate patient ID's. Thanks. ...