USE [DataPRO]
GO

/****** Object:  UserDefinedFunction [dbo].[foo_GetIdenticalChannelCount]    Script Date: 1/25/2019 2:34:47 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO




CREATE FUNCTION [dbo].[foo_GetIdenticalChannelCount]
(
	@GroupId1 int,
	@GroupId2 int
)
RETURNS int
AS
BEGIN
	return (
	SELECT COUNT(*)
    FROM [DataPro].[dbo].[Channels] C1,
	     [DataPro].[dbo].[Channels] C2
	WHERE C1.GroupId = @GroupId1
	  AND C2.GroupId = @GroupId2
	  AND ((C1.SensorId IS NULL AND C2.SensorId IS NULL) OR (C1.SensorId = C2.SensorId))
	  AND C1.IsoCode = C2.IsoCode
	  AND C1.IsoChannelName = C2.IsoChannelName
	  AND C1.UserCode = C2.UserCode
	  AND C1.UserChannelName = C2.UserChannelName
	  AND ((C1.DASId IS NULL AND C2.DASId IS NULL) OR (C1.DASId = C2.DASId))
	  AND C1.DASChannelIndex = C2.DASChannelIndex
	  AND C1.GroupChannelOrder = C2.GroupChannelOrder
	  )
END

GO

