Solved

SQL Get MIN row for each record

Posted on 2011-09-23
7
205 Views
Last Modified: 2012-06-27
Hello I have the following table data(posed in the CODE sec.  

wondering how to get the row data with the min date from each OPID.  If the 2nd Window date is less than 1st Window date then the 2nd Window should be read.    

get the min date goes something like...
Select distinct min DATE, OPID, Window
from TABLE

But the problem is if the 2nd window is the lesser date of the windows.
Accnt	OPID	DATE	Window 
34	1	9/22/11 12:32 PM	1st WINDOW
34	1	9/22/11 12:33 PM	2nd WINDOW
12	2	9/21/11 11:02 AM	1st WINDOW
12	2	9/21/11 11:01 AM	2nd Window
455	1	9/22/11 1:09pm	1st WINDOW

Open in new window

0
Comment
Question by:TechMonster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36590001
try:

select * from (
select *, row_number() over (partition by opid order by window) rn
from myTable ) x
where rn=1
0
 
LVL 51

Accepted Solution

by:
Huseyin KAHRAMAN earned 500 total points
ID: 36590009
oops, we should sue [DATE]

select * from (
select *, row_number() over (partition by opid order by [DATE]) rn
from myTable ) x
where rn=1
0
 

Author Comment

by:TechMonster
ID: 36590092
i think that got it!  Have to test it out a few more times though...thanks.
0
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

Author Comment

by:TechMonster
ID: 36590112
Well it gets the min from all DATes..but I have to get the row by min dateTime for each day.
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36590151
then you should partition by day & opid

easy way is to get date part

select floor(cast(GETDATE() as float))

select * from (
select *, row_number() over (partition by opid,  floor(cast([DATE] as float)) order by [DATE]) rn
from myTable ) x
where rn=1
0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36590172
or use CONVERT(varchar, getdate(), 101) -- to get rid of time part and group by date only like : 09/23/2011

select * from (
select *, row_number() over (partition by opid, CONVERT(varchar, getdate(), 101) order by [DATE]) rn
from myTable ) x
where rn=1
0
 

Author Comment

by:TechMonster
ID: 36591917
Perfect..thanks again
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET - Why is accordion not working? 3 47
Jquery driving me nuts... 14 28
DataTable column sorting incorrectly 2 20
Read text on Table 7 29
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

749 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