Solved

SQL looking for greater AND equal date

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

746 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

11 Experts available now in Live!

Get 1:1 Help Now