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 |
keka3309
Starting Member
11 Posts |
Posted - 2013-03-11 : 10:01:46
|
Hi All,Need some help in writing a query for the below sample dataColumn A,B,C are my tables columnsand D ad E need to be derived from A,B and C.A B C D E2 14 13.5 N 142 14 13.5 N 142 14 14 CM 142 14 16 N 143 14 13.5 N 143 14 13.5 N 143 14 13.5 N 143 14 13.5 N 143 14 14 CM 143 14 14 CM 143 14 16 N 143 14 16 N 146 14 13.5 N 146 14 13.5 N 146 14 14 CM 146 14 16 N 14I have to calculate close match for column B by comapring the values in C and populate D as CM(Close match) and display the close match value in E.For Ex: for A=2 the value for B is 14. The close match for this in column C is 14 so i have to populate CM in D and in E display the value 14 for A=2Your help is much appreciatedThanks |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-11 : 10:19:54
|
SELECT A, B, C, CASE WHEN MIN(ABS( B-C)) OVER( PARTITION BY A) = ABS( B-C) THEN 'CM' ELSE 'N' END AS DFROM @t--Chandu |
|
|
keka3309
Starting Member
11 Posts |
Posted - 2013-03-11 : 11:20:04
|
Thanks Chandu for the quick response now im able to get the column D but how should i display the value in column E |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-11 : 12:59:01
|
please dont open multiple threads for same issue------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
keka3309
Starting Member
11 Posts |
Posted - 2013-03-11 : 13:37:27
|
Hi Vishakh,This issue is not repostedThanks |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-12 : 04:37:08
|
[code]SELECT A, B, C, CASE WHEN Rn=1 THEN 'CM' ELSE 'N' END AS D, MAX( CASE WHEN Rn = 1 THEN B END) OVER(PARTITION BY A) EFROM (SELECT A, B, C, DENSE_RANK() OVER(PARTITION BY A ORDER BY ABS(B-C)) RN FROM @t) temp[/code]--Chandu |
|
|
keka3309
Starting Member
11 Posts |
Posted - 2013-03-12 : 06:01:51
|
Thanks Chandu |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-03-12 : 07:02:46
|
quote: Originally posted by keka3309 Thanks Chandu
Welcome--Chandu |
|
|
|
|
|
|
|