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.
| 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 TransDatemjdaley 2/14/2006mjdaley 3/1/2006mjdaley 3/22/2006cdsmith 3/15/2006cdsmith 5/15/2006cdsmith 7/31/2006jnsanch 4/20/2006jnsanch 4/28/2006jnsanch 5/20/2006Here 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/2006cdsmith 3/15/2006 7/31/2006jnsanch 4/20/2006 5/20/2006Thanks in advance,Parallon |
|
|
DustinMichaels
Constraint Violating Yak Guru
464 Posts |
Posted - 2006-06-16 : 13:37:20
|
Use a group by clauseSELECT EmpID, MIN(TransDate) AS MinTransDate, MAX(TransDate) AS MaxTransDateFROM YourTableGROUP BY EmpID |
 |
|
|
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 empidMike"oh, that monkey is going to pay" |
 |
|
|
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 |
 |
|
|
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 WoNumFROM WorkOrderWHERE ProjectId = 'MMColParam'))) GROUP BY Schedule.Empid, WorkOrder.Request; Is there any way to get around this error?Thanks again,Parallon |
 |
|
|
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 |
 |
|
|
|
|
|
|
|