if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_GetFirstAnalysis]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fn_GetFirstAnalysis]
GO

-- =============================================
-- Author: Марат Зигангараев (C) Vitacard System 2009
-- Create date: 2009-11-18
-- Description:	Возвращает первый анализ пациента между указанными датами
-- =============================================
--CREATE FUNCTION  dbo.fn_GetFirstAnalysis (@KvdPatientID uniqueidentifier, @FromDate datetime, @ToDate datetime)
--RETURNS uniqueidentifier
--AS
--BEGIN
--	Return (
--		Select 
--			Top 1 a.KvdAnalysisID
--		From 
--			[AKUZ].T_KVD_ANALYSIS a with(nolock)
--		Where 
--			a.KvdPatient = @KvdPatientID 
--				And 
--			a.[Date] Between @FromDate And @ToDate
--		Order By 
--			a.[Date], a.RegDate
--			)
--END
--GO