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 |
ronin2307
Posting Yak Master
126 Posts |
Posted - 2006-10-30 : 10:18:47
|
I'm sure this is a no-brainer but because of the lack of experience I'll ask anyways:imagine a table that looks like this:COL_A COL_B1 A2 A1 B2 B1 C2 CI would like the query all DISTINCT col_b entries and their respective MAX col_a entries so that my query returns this2 A2 B2 Cwhat does the query look like for this?Thanx |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-30 : 10:22:01
|
[code]select max(COL_A), COL_Bfrom tablegroup by COL_B[/code] KH |
 |
|
ronin2307
Posting Yak Master
126 Posts |
Posted - 2006-10-30 : 10:27:57
|
yeah I thought so, but unfortunately I oversimplified the above example for what I need. I'll try to work on it a little more before posting the full query. thanx anyway |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-30 : 10:30:31
|
Sure. Please post the table structure, some sample data and the expected result. And also try to explain what are you trying to achieve. It will help to speed things up. KH |
 |
|
ronin2307
Posting Yak Master
126 Posts |
Posted - 2006-10-30 : 10:34:20
|
[code]SELECT hwk_taskdates.fCycleCount,hwk_taskdates.fTotalHrs,hwk_taskdates.fDate, hwk_machinedata.fCycleCount,hwk_machinedata.fTotalHrs, hwk_Tasks.fCycle,hwk_Tasks.fRunTime,hwk_Tasks.fDayCount, hwk_taskdates.fGUID FROM hwk_taskdates left outer join hwk_machinedata on hwk_machinedata.fMachineID = hwk_taskdates.fMachineID left outer join hwk_tasks on hwk_tasks.fGUID = hwk_taskdates.ftaskid WHERE (cast(hwk_taskdates.fDate as varchar(50)) like dbo.fx_GetLastCompletedDate(dbo.fx_GetTaskGUID('%%'))) AND (hwk_taskdates.fMachineID = dbo.fx_GetMachineGUID('VROC'))[/code]the result is:[code]0 .00 2006-06-30 00:00:00.000 10000 4300.00 NULL NULL 90.00 764C3C4C-C4A0-4610-9489-296BE18C6D0310000 4300.00 2006-10-27 16:49:26.900 10000 4300.00 500.00 NULL NULL EF025E99-218B-4E0E-89D9-EC7872B33B310 .00 2006-10-01 00:00:00.000 10000 4300.00 NULL NULL 1.00 5B7279A9-3950-45C8-821A-416606E96B049900 .00 2006-10-27 00:00:00.000 10000 4300.00 500.00 NULL NULL EF025E99-218B-4E0E-89D9-EC7872B33B31[/code]as you can see in the GUID col row 2 and 4 have the same GUID. what I want is only the entries for the latest date for each distinct GUID to be returned.I hope it is somewhat clear what I needThanx |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-30 : 10:41:54
|
"I hope it is somewhat clear what I need"Yes. Sorry but i don't quite understand the logic behind your query.Anyway this is another method you can use instead of the select max(COL_A), COL_B from table group by COL_B
declare @table table( COL_A int, COL_B char(1))insert into @tableselect 1, 'A' union allselect 2, 'A' union allselect 1, 'B' union allselect 2, 'B' union allselect 1, 'C' union allselect 2, 'C'select *from @table twhere t.COL_A = (select max(COL_A) from @table x where x.COL_B = t.COL_B)/*COL_A COL_B ----------- ----- 2 C2 B2 A(3 row(s) affected)*/ KH |
 |
|
ronin2307
Posting Yak Master
126 Posts |
Posted - 2006-10-30 : 10:48:54
|
the logic is somewhat complicated but I was hoping that it is still doable:basically: If given an existing description of a task, the query would pull the LATEST entries for that task from the taskdates table (along with some other stuff from other tables)If not then it would pick out the rows for each distinctive task in the taskdates table where fdate is the latest datehope that makes sense |
 |
|
|
|
|
|
|