Solved

SQL looking for greater AND equal date

Posted on 2011-02-13
4
368 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

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.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

776 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