Solved

SQL looking for greater AND equal date

Posted on 2011-02-13
4
373 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:Ephraim Wangoya
ID: 34884880

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

Expert Comment

by:Ephraim Wangoya
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server stored procedure parameter 10 20
Freeze portion of datamart 2 21
SQL Server Trigger 8 33
SQL - Error "The Multi-Part Identifier could not be found" 2 12
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 …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

685 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