Solved

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

Posted on 2004-10-01
9
1,524 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

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.

Join & Write a Comment

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

747 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

9 Experts available now in Live!

Get 1:1 Help Now