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 |
|
ra_sasi
Starting Member
9 Posts |
Posted - 2005-12-22 : 13:56:04
|
| Hi,I am new to this forum and i hope this is going to be a good experience. While writing a particualr SQL, am facing this problemTABLE ResourceRes_Id(pk) Name 1 Res12 Res23 Res3TABLE UploadUpload_Id(pk) ResId(fk) FILE_PATH 1 1 PATH1 2 1 PATH2 3 1 PATH3 4 2 PATH4 5 2 PATH5 6 2 PATH6 Our requirement is to get the maximum UploadId and the corresponding file path for all the resource IdExpected outputRes_id Upload_Id FILE_PATH 1 3 PATH3 2 6 PATH6If I am not putting FILE_PATH in SQL,its working fine and is returning Res_id and Upload_idselect RM.RES_ID,max(UPD.UPLOAD_ID),RM.name from Resource RM join Upload as UPDON RM.resource_id = UPD.resource_id group by rm.RES_ID,UPD.RES_ID, RM.Namehaving RM.RESOURCE_ID=UPD.RESOURCE_ID;But if I am adding FILE_PATH in SQL, its returning multiple rowsselect RM.RES_ID,max(UPD.UPLOAD_ID),RM.name,UPD.FILE_PATHfrom Resource RM join Upload as UPDON RM.resource_id = UPD.resource_id group by rm.RES_ID,UPD.RES_ID, RM.Name,UPD.FILE_PATHhaving RM.RESOURCE_ID=UPD.RESOURCE_ID; |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-12-22 : 14:26:28
|
Well I certainly hope your experience here is fun, educational, and spirtually uplifting. Welcome!use pubsgocreate TABLE Resource (Resource_id int primary key, Name varchar(10))goinsert Resource select 1, 'Res1' unionselect 2, 'Res2' unionselect 3, 'Res3'gocreate TABLE Upload (Upload_Id int primary key ,Resource_Id int references Resource(Resource_id) ,FILE_PATH varchar(10))goinsert Uploadselect 1, 1, 'PATH1' unionselect 2, 1, 'PATH2' unionselect 3, 1, 'PATH3' unionselect 4, 2, 'PATH4' unionselect 5, 2, 'PATH5' unionselect 6, 2, 'PATH6'goselect a.Resource_id ,a.upload_id ,b.File_Pathfrom (--derived table to get the max upload_id for each resource_id select resource_id ,max(upload_id) upload_id from Upload group by resource_id ) ajoin Upload b on b.upload_id = a.upload_idgodrop table Uploaddrop table Resource EDIT:quote: select RM.RES_ID,max(UPD.UPLOAD_ID),RM.name,UPD.FILE_PATHfrom Resource RM join Upload as UPDON RM.resource_id = UPD.resource_id group by rm.RES_ID,UPD.RES_ID, RM.Name,UPD.FILE_PATHhaving RM.RESOURCE_ID=UPD.RESOURCE_ID;
Your HAVING clause is not needed because it simply duplicates your join criteria.By adding File_path to the GROUP BY clause, you've now instructed sql server to make sure each File_Path is represented by a group.UPD.RES_ID is not needed in your group by clause because based on your join criteria it will always be the same as rs.res_id, and it's not included in your select list.Be One with the OptimizerTG |
 |
|
|
ra_sasi
Starting Member
9 Posts |
Posted - 2005-12-22 : 15:00:38
|
Thanks TG...its wonderfult o get reply in such a short time and it WORKS!! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-23 : 00:49:36
|
| OrSelect Resource_id,Upload_Id,File_Path from Upload Uwhere upload_id=(Select max(upload_id) from Upload where Resource_id=U.Resource_id group by Resource_id)MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|