 మరక మన్నారిండి క౿. మంము నిమరిండ్గటిండి. ఆతరినెసింరిరోహాన్ఎ క౿మరిమరి. అళితుటా లిమిలైతిండిండి. మసిమిమిల్డి. సిసానికప్న క౿ in card number we are having string data that is c1, c2, c3 and c4 but in value column we are having numeric data that is 1.25611301 then 1.145798 so we are having the numeric values in the value column then we are also having input variable that is at the rate well and it is also having numeric data that is 1.2511304 so as of now we just want to get the nearest match value from this variable to the value column and here we just want only top 2 values but in real time you can use the same query and we will get the top 2, top 3, top 5 and top n value according to our requirement so go to on SQL Server Management Studio so as I am using SQL 2017 you may have other versions of SQL Server but this query will work for all the versions so first of all I would like to create the table as we seen our input table has 2 columns first is card number and second is value so let me execute this query to create the table now I just want to insert data on this table let me execute this insert a statement to insert the values so as we seen we have total 8 cards here we just want to insert all the card values into the table so 8 rows are selected data has been inserted successfully on the table now I just want to check the data then we will implement our logic so we can write select as tick from our table so as we can see we are having same metadata as we seen in the slide so what would be the logic so here we just want to get the nearest match for top 2 values so here I just want to create one more column and that column will have the difference between the value and our variable that is at the right value now I just want to execute this query so as we can see here we are having the negative values as well so we don't want to negative values so we can use the salute function to remove the negative sign and we can put column name with minimum value now let me execute this query we are having the difference values between variable and existing column so this is the values we are getting so here we just want to create a sequence number on behalf of this minimum value column and that sequence number would be the increasing order so we can use the row number as we have discussed in the many videos so we can directly use the row number then we can use the over clause then here we just want to use the order by clause so order by and this time we just want to use the order by with this minimum value column so we can copy this calculated field and paste it here so this is for order by clause now we can put it in a column with rw now we can execute and we can check so here we are going to get the row number that is 12345678 so this is a sequence number so how we can get the top two values so we can put this value into a CT so we can use a CT so then we can use as and now we can use select as tick from this CT in where clause we can filter out where this rw value less or equals to 2 now let me execute this query and we just want to check the data so we are having C1 and C8 go to our slide and here we are having C1 and C8 so these two are the nearest match value with the variable that is at the right well here in filter criteria we can use 3 so it will be going to return top 3 nearest match and according to our requirement we can filter out by using this rw so it will be going to return top 4 nearest match value so I hope you have understand how we can get the nearest match value if we want to use the top 2 top 3 top n so we can also use that one so if you need this script it will be a label on the description of this video so you can copy from there for your practice purpose so thank you so much for watching this video if you like this video please subscribe our channel to get many more videos don't forget to press the bell icon to get the notification of our newly uploaded videos thank you so much