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 |
|
shaik.zakeer
Posting Yak Master
117 Posts |
Posted - 2009-05-12 : 23:19:23
|
| Hi friendsI have two tables like the followingtable AOID Name1 A2 B3 C4 D5 E6 Ftable BOID tableAOID code value modifieddttm date1 1 Height 152 01/05/2009 01/05/20092 1 weight 52 01/05/2009 01/05/20093 1 height 165 02/05/2009 02/05/20094 1 weight 65 02/05/2009 02/05/20095 2 height 172 01/05/2009 01/05/20096 2 weight 72 01/05/2009 01/05/20097 2 height 165 02/05/2009 02/05/20098 2 weight 65 02/05/2009 02/05/20099 2 height 185 03/05/2009 03/05/200910 2 weight 75 03/05/2009 03/05/2009Now i need to write a function and the input parameter will be "OID of table A".Using same function i need to get columns height ,weight & date individually based on latest modifieddttm .likeOid Height weight date 1 165 65 02/05/20092 185 75 03/05/2009i need to write only function . and suggest me is there any other way also.I will use this function in a sp. Kindly help me . How can i do this.Thanks Zakeer Sk |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-05-12 : 23:29:33
|
| [code]create function dbo.udf_sample(@oid int)RETURNS @temp2 TABLE ( oid INT, height int ,weight int, date DATETIME)ASBEGIN insert into @temp2select b.tableAOID,max(case when code ='height' then b.value end) as height,max(case when code ='weight' then b.value end) as weight ,max(modifieddttm)from @t tinner join @b b on b.tableaoid = t.oid where t.oid = @oidgroup by b.tableAOIDRETURNEND[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-13 : 10:46:08
|
quote: Originally posted by bklr
create function dbo.udf_sample(@oid int)RETURNS @temp2 TABLE ( oid INT, height int ,weight int, date DATETIME)ASBEGIN insert into @temp2select b.tableAOID,max(case when code ='height' then b.value end) as height,max(case when code ='weight' then b.value end) as weight ,max(modifieddttm)from @t tinner join @b b on b.tableaoid = t.oid where t.oid = @oidgroup by b.tableAOIDRETURNEND
how will this ensure you get height,weight values for latest date? i think it should be like belowcreate function dbo.udf_sample(@oid int)RETURNS @temp2 TABLE ( oid INT, height int ,weight int, date DATETIME)ASBEGIN insert into @temp2select b.tableAOID,max(case when b.code ='height' then b.value end) as height,max(case when b.code ='weight' then b.value end) as weight ,max(b.modifieddttm)from @t tinner join @b b on b.tableaoid = t.oid inner join (select tableAOID,max(modifieddttm) as latest from @b group by tableAOID) b1on b1.tableaoid=b.tableaoidand b1.latest=b. modifieddttmwhere t.oid = @oidgroup by b.tableAOIDRETURNEND |
 |
|
|
shaik.zakeer
Posting Yak Master
117 Posts |
Posted - 2009-05-13 : 23:04:57
|
| actually i m calling this function in a SP along with some other columns which is from some other tables.Likeselecta.name,b.name,----------dbo.udf_sample(@oid) as height,dbo.udf_sample(@oid) as weight,dbo.udf_sample(@oid) as datefromtable ajointable b join ----------------the output should be likeFName LName ADDRESS CITY ZIP HEIGHT WEIGHT DATEA B NO:2/5 NY NY4353 172 75 03/05/2009 Is this possible to get like this or i need to pass one more parameter likedbo.udf_sample(@oid,'Height') as Heightdbo.udf_sample(@oid,'Weight') as Weight-----my scenario is i should hot code like this.Kindly help me to overcome this.thanks in advance.Thanks Zakeer Sk |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-14 : 10:49:47
|
| will the code values be always height,weight? if not then you need to deterime this dynamically and need to do it in a procedure.However, if they are static, you can use udf given and pass the value for @oid from main table using cross apply and it will give you crosstabbed results |
 |
|
|
shaik.zakeer
Posting Yak Master
117 Posts |
Posted - 2009-05-14 : 23:04:13
|
| yes the code values always will be static only... using the given function i am getting three values at a time.i think i need to pass one more parameter like what i have mentioned in the above reply.dbo.udf_sample(@oid,'Height') as Heightdbo.udf_sample(@oid,'Weight') as Weightdbo.udf_sample(@oid,'modifiedat') as modifiedatwill it hamper the performance?Kindly suggest me what is the best way to do this.Thanks Zakeer Sk |
 |
|
|
|
|
|
|
|