473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Selecting a record for each unique ID

Hi everyone,

I'm having a problem writing out the SQL statement and unfortunately, I can't even produce you a working draft version simply because I cannot wrap my head around how to start it. I think its best if I show how the data looks first and then the question.

PersonID EncounterID Value
16099 E3434 23
16099 E3434 24
16099 A1232 5
13567 E1533 1
13567 E1560 15
23432 A3245 2

Ok, so the question is.. how do i write the query such that the result only shows for each personID, the lowest value so the result I am looking for is

PersonID EncounterID Value
16099 A1232 5
13567 E1533 1
23432 A3245 2

Ultimately, it is the EncounterID I am after but for each unique PersonID there is.

If anyone can help out, that would be great! I've search the site for some ideas but I'm not even sure of what to look for, so if this has been discussed, please let me know so that I don't waste anyone's time.

Thank you!
Nov 27 '06 #1
3 1500
NeoPa
32,556 Expert Mod 16PB
Try this (you need to modify it to fill in the correct info that wasn't provided) :
Expand|Select|Wrap|Line Numbers
  1. SELECT PersonID,
  2.   Mid(Min(Format([Value],'00000') & [EncounterID]),6) AS MinEncounter, 
  3.   Min([Value]) AS MinVal
  4. FROM YourTable
  5. GROUP BY PersonID
Nov 27 '06 #2
That's exactly what I am looking for! Thank you so much. Now to check help for what "Mid" does. Thanks again :D


Try this (you need to modify it to fill in the correct info that wasn't provided) :
Expand|Select|Wrap|Line Numbers
  1. SELECT PersonID,
  2.   Mid(Min(Format([Value],'00000') & [EncounterID]),6) AS MinEncounter, 
  3.   Min([Value]) AS MinVal
  4. FROM YourTable
  5. GROUP BY PersonID
Nov 27 '06 #3
NeoPa
32,556 Expert Mod 16PB
That's exactly what I am looking for! Thank you so much. Now to check help for what "Mid" does. Thanks again :D
I so like that attitude.
Often it's "How do I use Mid?" or "What do I have to do with my fingers to make the code come up on my computer?".
So I can be a little prone to exaggeration (or hyperbole for the purists).
Nov 27 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Ben Gribaudo | last post by:
Hello! I am trying to query a MySQL table and retrieve either the earliest or the latest message in each thread. The messages are in a table with MsgID being the primary key and fThreadID...
6
by: throat.wobbler.mangrove | last post by:
Hi, I was hoping someone may be able to help me with a tricky T-SQL problem. I need to come up with a SELECT statement that does basically the following: Select RCRD_REFNO, MAX(MODIF_DTTM)...
3
by: Phil | last post by:
I am looking to set up a hyperlink control on a form to retrieve letters that correspond to a record on a form. That is, there may be 100 form records, and I would like each of those form records...
8
by: Henrik Larsson | last post by:
Hi, I need help with selecting the following rows from a table looking like this: ID IP Query 1 -> 1 2.2.2.2 (ie first IP 1 1.1.1.1 <- Query 2 for each...
48
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
0
by: steve | last post by:
Hi all, Come across a situation I cannot figure how to work out. Im relatively new to access but have looked around and tried everything to my knowledge. I've developed an application whereby...
48
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
2
by: Catch_22 | last post by:
Hi, I have a stored procedure that has to extract the child records for particular parent records. The issue is that in some cases I do not want to extract all the child records only a...
3
by: samdev | last post by:
I have a table that contains records that provide the following info: vehicle type route # start time travel time end time I need to display info for all records in a query and/or report...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.