Solved

How to select one record per day sql question. Simple!!

Posted on 2004-10-01
9
1,526 Views
Last Modified: 2012-05-05
I have a sample query

select a.row_id, b.name, trunc(a.created)
from   tbl a

I would like a query to return one record per day. The table has multiple rows for the same day.!
0
Comment
Question by:cyberonics
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 7

Expert Comment

by:Bigfam5
ID: 12200101

Select Distinct a.row_id, b.name, trunc(a.created)
from   tbl a
where a.created in (select MAX(a.created) from tabl a)

You could use either MAX or MIN
0
 
LVL 7

Expert Comment

by:Bigfam5
ID: 12200131
Sorry mis-read you question.  Leave out the WHERE
0
 

Author Comment

by:cyberonics
ID: 12200155
Bigfam5,

that does not work. I need one record per date. therefore if the table has 500 records and has 50 records a day, I just want to pick one record per day
0
 
LVL 3

Expert Comment

by:oratim
ID: 12200351
select * from tbl where row_id = ( select max(row_id) from tbl where created in (select distinct(trunc(created)) from tbl))


tim
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Expert Comment

by:oratim
ID: 12200508
this is a bit better

select * from tbl where row_id = ( select max(row_id) from tbl group by trunc(created))

0
 
LVL 3

Accepted Solution

by:
oratim earned 20 total points
ID: 12200523
OOPS, fixed:

select * from tbl where row_id in ( select max(row_id) from tbl group by trunc(created))

tim
0
 

Author Comment

by:cyberonics
ID: 12200758
oratim,

I'm testing your query. My question is that will your query return only one row or will it return multiple rows as max usually returns one row!??

0
 
LVL 3

Expert Comment

by:oratim
ID: 12200833
this part

select max(row_id) from tbl group by trunc(created))

will filter the records to one rowid for each date, the MAX function is to avoid having to group by row_id, which would do nothing since I am assuming row_id is a unique index/PK.

then the outer select * will get the rest of the records to go with the row_id' s retrieved from the inner select, with only one record being returned for each date.

hope it works for you, I used a table I already have and it looked right, I just substituted you schema names in place of mine.


Tim

0
 
LVL 9

Expert Comment

by:pratikroy
ID: 12201141
select a.row_id, b.name, max(trunc(a.created))
from   tbl1 a, tbl2 b
wher a.row_id = b.row_id
group by trunc(a.created)

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Insert and update a row at the same time 4 52
Oracle -- identify blocking session 24 43
How to free up undo space? 3 29
Oracle Listener Not Starting 11 25
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
This video shows how to recover a database from a user managed backup

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now