Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > General > Database Concepts & Design > How do I limit my result set to just one record

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-27-07, 21:07
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
How do I limit my result set to just one record

Hello everyone,

This seems like a simple question but I just can't seem to get past how to solve the problem of limiting my result set to just one "current" record.

I have a status table that holds values for a person's *current* status such as busy, with a client, whatever. A current record means the user's current status and not the past status.

When I select the person's status WHERE user_Id = something, I am getting ALL of thier status records.

How can I get just the *current* record? I'm sure that this is an easy question to answer, but I'm having a mental block.

I was thinking since I am using a timestamp in the table that maybe I could use a CURRENT_TIME() - 1 minute or something like that to get the user's current status?

Can someone please help? Thanks,

Frank
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #2 (permalink)  
Old 09-27-07, 21:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
which one is the current one? how do you tell a current one from past ones?

are you going to return one row from the query (with person id as parameter), or one row for every person?
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #3 (permalink)  
Old 09-27-07, 22:26
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Quote:
Originally Posted by r937
which one is the current one? how do you tell a current one from past ones?

are you going to return one row from the query (with person id as parameter), or one row for every person?
Thanks for the help Rudy.

What I would like is to be able to tell what a single person's status is at whatever time of the day it is. So, if there is 1 employee, with 100 records there should be 100 rows reflecting their status. I am only concerned with their "last" or most current entry denoted by the timestamp being most current. The tricky part for me is eliminating the "old" or "history" records. Let me give you some sample data.

Code:
employee_Id status timestamp 1 in office 2007-01-01 14:36 1 lunch 2007-01-01 15:00:21

There will probably be several rows for employee_Id 1 but what I need is to determine the "latest" entry probably determined by the timestamp which in this case would be the lunch entry at 15:00:21 hours. I don't care about the in office status at 14:36 hours because there is now a new entry at 15:00.
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.

Last edited by Frunkie : 09-27-07 at 22:32.
Reply With Quote
  #4 (permalink)  
Old 09-27-07, 23:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
so it's just the latest row per person?
Code:
select employee_Id , status , timestamp from daTable as T where timestamp = ( select max(timestamp) from daTable where employee_Id = T.employee_Id )
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #5 (permalink)  
Old 09-28-07, 01:16
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Well, let me try this again seeing as how I already responded once but lost the message due to the site being under maintenance for replication.

Thank you Rudy. The MAX aggragate function worked out very well.

Frank
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #6 (permalink)  
Old 09-28-07, 05:44
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
The MAX() function works dandy for one record (As I know is what I said I needed) but I need the top or latest record for each row of data. What constitutes a row of data is the employee number.

Here is some actual sample data from the database.

Code:
status emp_No timestamp id In 1 2007-09-27 08:27:15 1 In 2 2007-09-27 08:45:40 2 In 5 2007-09-27 09:01:10 3 In 3 2007-09-27 09:15:06 4 Busy 1 2007-09-27 10:31:08 5

There are 4 seperate employees. Employee #1 has two entries. I need to get the most recent row for him.

I should have 3 additional rows returned for employee#s 2, 3 and 5 because they only have 1 current entry. If those employees had additional rows, I would need the latest rows returned for those as well.

Is this possible in sql or better done in my application code?

Thanks.
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #7 (permalink)  
Old 09-28-07, 06:18
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
I believe that I have it. Here is what I have done.

Code:
SELECT employee_Number_Id, max( timestamp ) as max FROM console GROUP BY employee_Number_Id

Is this ok?
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #8 (permalink)  
Old 09-28-07, 06:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
you don't have what you originally asked for, but you do have something

you have the max timestamp per employee

but it is not the row with the max timestamp, because you aren't returning the row, just the PK and the max timestamp

if that's what you wanted, fine, but you did ask to return the row

should you ever wish to have the row with the max timestamp, please see post #5

in particular, you are still missing the status -- you may have the latest timestamp per employee, but you don't know what the status is that corresponds to that!

__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #9 (permalink)  
Old 09-28-07, 07:26
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Rudy, thank you! That (post #4) did work after all. I don't know what I did wrong with that code the first time, but I have it working now.

What was confusing me was that I needed to join a second table for additional information and I wasn't quite sure how to do that. I found a few examples after googling it and eventually got it.

Thank you again.
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #10 (permalink)  
Old 09-28-07, 07:34
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
just a tip, but if you pose a problem, and you do it in a simplistic way, and then hope to adapt the given solution by adding another table, maybe you should show the real problem right from the get-go

what was the additional table?
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #11 (permalink)  
Old 09-28-07, 09:10
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Quote:
Originally Posted by r937
just a tip, but if you pose a problem, and you do it in a simplistic way, and then hope to adapt the given solution by adding another table, maybe you should show the real problem right from the get-go

what was the additional table?
I'm so afraid your gonna rip my design apart when I post the whole thing. Actually, I just figured that it would be less confusing if I only posted the table that was giving me a problem. I'll take your advice and in the future post all of the tables involved.

The other table was the employee table.

Since you asked and because I have a headache from overthinking a problem I have been having, please let me ask you how you would handle it.

This other table (employee) holds all of the information on the employee such as name address dob etc. I have two types of employees, office personnel and field personnel. I want to track the whereabouts of both types of employees. I created a springboard type table to show the employees status.

Anybody working inside the office is not a problem. The problem is with the field employees. The field employees require consistant updating of the database. Their location will change several times during a shift.

I have created another table that will give the status of the field personnel and their work and also their whereabouts. There is no need to track work progress for the office personnel.

Idealy, what I was shooting for was to have just one table to put this information into and display it in a tabular format. I couldn't get the relationships correct though. The PK from the field personnel table becomes a FK in the employee status table. The employee PK becomes a FK also in the employee status table.

With regard to the employee status table, I now have two FKs. Whenever I need to update a particular employee, be it office or field, one of the db fields (FK) would have to be nullable which is not correct. I am either updating one type of employee or the other but never both at the same time.

What I have done in the meantime which doesn't seem like a bad alternative was to program two seperate pages; one for field personnel and the other for office personnel. This way, I could just leave the tables as they are.

How might you handle this Rudy?

Thanks..

__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #12 (permalink)  
Old 09-28-07, 12:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
i'm not sure how i would handle it because i don't know what your requirements are

if you are building tables, though, then you are in a much better position than someone who has to retrieve meaningful information from an already-designed database

but it sounds like you have an employees table and an employee status table, and they can be combined easily even with the "only the latest" condition
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #13 (permalink)  
Old 09-30-07, 07:04
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Quote:
Originally Posted by r937
so it's just the latest row per person?
Code:
select employee_Id , status , timestamp from daTable as T where timestamp = ( select max(timestamp) from daTable where employee_Id = T.employee_Id )
Rudy, this code is really great, thank you! Can you explain it to me please? I understand the select columns, I understand the FROM, but the subquery has me baffled. Would you mind explaining it?

I have to use this code elsewhere in the db but only this time I have to join one other table. How would I go about doing that?

What I need in this query is exactly like before. I need all of the rows returned that have the MAX() timestamp *per person*. I need to join the employee table to pull the person's name.

Thanks,

Frank
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
  #14 (permalink)  
Old 09-30-07, 07:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
Code:
select T.employee_Id , T.status , T.timestamp , employees.name from daTable as T inner join employees on employees.id = T.employee_Id where timestamp = ( select max(timestamp) from daTable where employee_Id = T.employee_Id )
the subquery is a correlated subquery, you can tell because it refers to a column, T.employee_Id, which is part of the main query

what that means is, the subquery is evaluated for each row of the main query

for each row of the main query, all the rows which are for the same employee are evaluated

the subquery will retain all rows from the main query that have the highest timestamp for that employee
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #15 (permalink)  
Old 09-30-07, 17:43
Frunkie Frunkie is offline
Gives Bad Advice
 
Join Date: Mar 2007
Location: 010101010110100
Posts: 706
Thanks so much, Rudy!
__________________
I and many others around the world are of the strong belief that the universe was created by the Flying Spaghetti Monster. It was He who created all that we see and all that we feel. We feel strongly that the overwhelming scientific evidence pointing towards evolutionary processes is nothing but a coincidence, put in place by Him.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On