Solved

SQL looking for greater AND equal date

Posted on 2011-02-13
4
366 Views
Last Modified: 2012-05-11
This is SQL 2000....

I have a table and data below. When dates are the same...i want to bring one row back (only bring back a1 row).

When dates are not the same..i want to bring back the oldest date...this row in the example below:
'a2','2011-02-11 10:57:22.080''5433'

How can I do this?
create table #test
(
  descr varchar(3),
  processDate datetime,
  shipId as varchar(4)
)


insert into #test ('a1','2011-02-13 16:57:22.080','1111') --same date for a1
insert into #test ('a1','2011-02-13 16:57:22.080','2222') --same date for a1
insert into #test ('a2','2011-02-11 10:57:22.080''5433') -- older date for a2
insert into #test ('a2','2011-02-12 15:57:22.080','5678') -- newer date for a2

select * from #test t1
 inner join #test t2 on t1.descr = t2.descr
where ??

Open in new window

0
Comment
Question by:Camillia
  • 2
4 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 34884880

select shipid, descr, max(processDate)
from #test t1
group by shipid, descr, processDate
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 34884884
Correction, dont group by processdate

select shipid, descr, max(processDate)
from #test t1
group by shipid, descr
0
 
LVL 7

Author Comment

by:Camillia
ID: 34884948
no, that gives me all 4 rows.  I had some bugs in the example above...fixed them below.
create table #test
(
  descr varchar(3),
  processDate datetime,
  shipId  varchar(4)
)


insert into #test values ('a1','2011-02-13 16:57:22.080','1111') --same date for a1
insert into #test values ('a1','2011-02-13 16:57:22.080','2222') --same date for a1
insert into #test values ('a2','2011-02-11 10:57:22.080','5433') -- older date for a2
insert into #test values ('a2','2011-02-12 15:57:22.080','5678') -- newer date for a2

Open in new window

0
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 500 total points
ID: 34885042
How about this one?

SELECT A.descr, A.processDate, MIN(A.shipId) AS shipId
FROM #test A
INNER JOIN
(
	SELECT descr, MIN(processDate) As processDate
	FROM #test
	GROUP BY descr
) B ON A.descr = B.descr AND A.processDate = B.processDate
GROUP BY A.descr, A.processDate

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

895 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

17 Experts available now in Live!

Get 1:1 Help Now