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 |
eugz
Posting Yak Master
210 Posts |
Posted - 2010-08-05 : 15:10:23
|
Hi All.How to return MIN vallue for CODE field of JOIN two tables?What I mean. For instance:select a.Emp_Id, Codefrom Table1 ajoin table2 bon a.Emp_Id = b.Emp_Id if return is:2 12.512 54.32 3.256 57.69 101.36 12.1My question is, how to get result like:2 3.256 12.19 101.3Thanks. |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
eugz
Posting Yak Master
210 Posts |
Posted - 2010-08-10 : 17:00:49
|
Thanks for replay.Now I need to use your select like subselect in other SELECT statment. My target is to display new fields including CODE field witch is MIN in subselect. selectv.Visit_Id,Convert(varchar(10),Svc_Date,101) Svc_Date,Adm_No,FName,LName,(select a.Emp_Id, MIN(Code) AS MinCode from Table1 a join table2 b on a.Emp_Id = b.Emp_Id group by a.Emp_Id) Code,Submitfrom Table3 vjoin dbo.Table4 pon v.AAA_id = p.AAA_idleft outer join Table2 vdon v.Emp_Id = vd.Emp_IdIf know other way to get same result I will appreciate for help.Thanks. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-08-11 : 05:01:10
|
See if this worksselect t1.* from table1 as t1 inner join(select emp_id, min(code) as code from table2group by emp_id) as t2on t1.emp_id=t2.emp_id and t1.code=t2.codeMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|