MS Sql query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • swatibd
    New Member
    • Oct 2007
    • 1

    MS Sql query

    I am creating function to get the percentiles in a given column
    So , it's necessary to pass a tablename and columnname to the function
    the following function is working fine.
    But , I want to modify it to pass the columnname as totaltime and tablename as a

    CREATE function percentile ( @shortname nvarchar(255) )
    returns float as
    begin
    return (select max(totaltime) from a where row_id in

    (select top 90 percent C.row_id from a C where C.shortname
    like @shortname order by C.totaltime )
    and shortname like @shortname )

    end

    HOW DO I DO THIS??
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by swatibd
    I am creating function to get the percentiles in a given column
    So , it's necessary to pass a tablename and columnname to the function
    the following function is working fine.
    But , I want to modify it to pass the columnname as totaltime and tablename as a

    CREATE function percentile ( @shortname nvarchar(255) )
    returns float as
    begin
    return (select max(totaltime) from a where row_id in

    (select top 90 percent C.row_id from a C where C.shortname
    like @shortname order by C.totaltime )
    and shortname like @shortname )

    end

    HOW DO I DO THIS??
    Hi and welcome to TSDN. Hope you'll have a great time here.
    I've moved this to the MS SQL server forum where it belongs.
    Please use the most appropriate forum when posting further questions.

    Comment

    • iburyak
      Recognized Expert Top Contributor
      • Nov 2006
      • 1016

      #3
      This is just an idea on how it should be done. You should know better your database to write and test it correctly.

      [PHP]CREATE function percentile ( @shortname nvarchar(255), @tablename varchar(255), @totaltime varchar(255) )
      returns float as
      begin
      Declare @Retvalue float
      If @tablename = ‘table1’

      select @Retvalue = max(totaltime)
      from table1 where row_id in

      (select top 90 percent C.row_id from table1 C where C.shortname
      like @shortname and C.totaltime = @totaltime order by C.totaltime)
      and shortname like @shortname
      ElseIf @tablename = ‘table2’
      select @Retvalue = max(totaltime)
      from table1 where row_id in

      (select top 90 percent C.row_id from table1 C where C.shortname
      like @shortname and C.totaltime = @totaltime order by C.totaltime)
      and shortname like @shortname
      Else
      @ Retvalue = 0


      Return @ Retvalue
      end[/PHP]

      Comment

      Working...