if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_ForReport62]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fn_ForReport62]
GO
CREATE   function fn_ForReport62 (@P float, @V float, @IsMonophase bit)
returns int
as
begin
  declare @res int
  declare @UnomID int
  set @UnomID = dbo.UnomIDByValue(@V)
  set @res = 0
  if @P<2500 -- äî 2500 íå âêëþ÷èòåëüíî
  begin
    if @UnomID = 9 OR @UnomID = 8 set @res = 1 -- 3-20 êÂ
    if @UnomID = 7 set @res = 2                -- 35 êÂ
  end
  else
    if @P<10000 -- 2500-10000 íå âêëþ÷èòåëüíî
    begin
      if @UnomID = 7 set @res = 3   -- 35 êÂ
      if @UnomID = 6 set @res = 4   -- 110-154 êÂ
    end
    else
      if @P<=80000 -- 10000-80000
      begin
        if @UnomID = 7 set @res = 5 -- 35 êÂ
        if @UnomID = 6 set @res = 6 -- 110-154 êÂ
      end
      else -- áîëåå 80000
      begin
        if @UnomID = 6 set @res = 7 -- 110-154 êÂ
        if @UnomID = 5 set @res = 8 -- 220 êÂ
        if @UnomID = 4              -- 330 êÂ
          if @IsMonophase = 1 set @res = 9
          else set @res = 10
        if @UnomID = 3              -- 400-500 êÂ
          if @IsMonophase = 1 set @res = 11
          else set @res = 12
        if @UnomID = 2 OR @UnomID = 1 set @res = 13 -- îò 750-1150 êÂ
      end
  return @res
end