ALTER FUNCTION [dbo].[foo_GetIdenticalChannelCount]
(
	@StaticGroupId int,
	@EmbeddedGroupId int
)
RETURNS int
AS
BEGIN
	return (
	SELECT COUNT(*)
    FROM [dbo].[Channels] C1,
	     [dbo].[Channels] C2
	WHERE C1.GroupId = @StaticGroupId
	  AND C2.GroupId = @EmbeddedGroupId
	  AND ((C1.SensorId IS NULL AND C2.SensorId IS NULL) OR (C1.SensorId = C2.SensorId))
	  AND C2.IsoCode LIKE REPLACE(C1.IsoCode, '?', '_')
	  AND C1.IsoChannelName = C2.IsoChannelName
	  AND C1.UserCode = C2.UserCode
	  AND C1.UserChannelName = C2.UserChannelName
	  AND (C1.DASId IS NULL OR C2.DASId IS NULL OR C1.DASId = C2.DASId)
	  AND (C1.DASId IS NULL OR C2.DASId IS NULL OR C1.DASChannelIndex = C2.DASChannelIndex)
	  )
END