Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Is there a Min/Max function???

Author  Topic 

parallon
Starting Member

25 Posts

Posted - 2006-06-16 : 13:33:11
Hello all. I have a table with an EmpID and TransDate. What I would like to do is to find the Min() and Max() Transdate of each specific EmpID within one query or one set of results. Is this possible?

Here is a sample of the data...

EmpID TransDate
mjdaley 2/14/2006
mjdaley 3/1/2006
mjdaley 3/22/2006
cdsmith 3/15/2006
cdsmith 5/15/2006
cdsmith 7/31/2006
jnsanch 4/20/2006
jnsanch 4/28/2006
jnsanch 5/20/2006

Here is a sample of the output that I would like to display on my page...

min(TransDate) max(TransDate)
mjdaley 2/14/2006 3/22/2006
cdsmith 3/15/2006 7/31/2006
jnsanch 4/20/2006 5/20/2006

Thanks in advance,

Parallon

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2006-06-16 : 13:37:20
Use a group by clause

SELECT EmpID, MIN(TransDate) AS MinTransDate, MAX(TransDate) AS MaxTransDate
FROM YourTable
GROUP BY EmpID
Go to Top of Page

mfemenel
Professor Frink

1421 Posts

Posted - 2006-06-16 : 13:38:08
select empid,min(transdate),max(transdate) from table group by empid order by empid

Mike
"oh, that monkey is going to pay"
Go to Top of Page

parallon
Starting Member

25 Posts

Posted - 2006-06-16 : 14:17:28
Thank you both so very much. I don't know why, but I was getting all sorts of errors. I actually didn't know that you could put a min() and a max() in the same query, so I was creating two separate recordsets and trying to combine them. What a pain in the butt.

Thank you both again,

Parallon
Go to Top of Page

parallon
Starting Member

25 Posts

Posted - 2006-06-16 : 14:45:49
Ok, all was good and I added some additional sub-queries that worked in Access, but once I uploaded it to my SQL db, I get the following error:

quote:
Microsoft OLE DB Provider for SQL Server error '80040e14'

The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.


Here is the query in its completion (which works perfectly in MS Access):


SELECT Schedule.Empid, Min(Schedule.TransDate) AS MinTransDate, Max(Schedule.TransDate) AS MaxTransDate, WorkOrder.Request  FROM Schedule INNER JOIN WorkOrder ON Schedule.WoNum = WorkOrder.WoNum  WHERE (((Schedule.WoNum) In (SELECT WoNum
FROM WorkOrder
WHERE ProjectId = 'MMColParam'))) GROUP BY Schedule.Empid, WorkOrder.Request;


Is there any way to get around this error?

Thanks again,

Parallon
Go to Top of Page

parallon
Starting Member

25 Posts

Posted - 2006-06-16 : 16:39:41
Actually, I finally got it with the following:

SELECT Schedule.Empid, Schedule.TransDate, Min(Schedule.TransDate) AS MinTransDate, Max(Schedule.TransDate) AS MaxTransDate, convert(varchar(200), Request) AS Request   
FROM Schedule INNER JOIN WorkOrder ON Schedule.WoNum = WorkOrder.WoNum
WHERE (((Schedule.WoNum) In (SELECT WoNum
FROM WorkOrder
WHERE ProjectId = 'MMColParam')))
GROUP BY Schedule.Empid, convert(varchar(200), WorkOrder.Request), Schedule.TransDate
ORDER BY EmpID ASC, TransDate ASC


Thank you both for your time and effort though.

Parallon
Go to Top of Page
   

- Advertisement -