Hi there!
I have a query that uses two UDF's. When I included the first one with help of George (thank you) it slowed down my query just a little bit. Now that I tried to write my own it takes close to two minutes instead of a few seconds so I gather I didn't do such a good job. Any help is of course greatly appreciated. Here is the query:
SELECT DISTINCT
ContactExtendedProperties.ActiveStatus,
Contacts.LastName,
Contacts.FirstName,
ContactExtendedProperties.WelcomeCard,
ContactExtendedProperties.BeforePic,
ContactExtendedProperties.AfterPic,
dbo.RO_GetCommaSeparatedNeedsTable(Contacts.ContactId) AS Description,
ContactExtendedProperties.CurrentStatus,
ContactExtendedProperties.SpacesCompleted,
ContactExtendedProperties.ClientLongevity,
MaxFollowupdate.Maxdate AS DateOfFollowup,
ContactExtendedProperties.ReceivedEval,
ContactExtendedProperties.OncancellationList,
dbo.RO_GetRemainingApps(Contacts.ContactId) AS RemainingApps
FROM Contacts
INNER JOIN ContactExtendedProperties ON Contacts.ContactId = ContactExtendedProperties.ContactId
INNER JOIN ClientStatus ON Contacts.ClientStatusId = ClientStatus.StatusId
INNER JOIN (
SELECT ContactId, MAX(DateOfFollowup) AS MaxDate
FROM ContactFollowup
GROUP BY ContactId) As MaxFollowupDate ON Contacts.ContactId = MaxFollowupDate.ContactId
INNER JOIN ContactFollowup ON Contacts.ContactId = ContactFollowup.ContactId
INNER JOIN RO_Users ON Contacts.PrimaryConsultantId = RO_Users.UserId
INNER JOIN ContactToNeeds ON Contacts.ContactId = ContactToNeeds.ContactId
INNER JOIN NeedsTable ON ContactToNeeds.NeedsId = NeedsTable.NeedId
WHERE Clientstatus.StatusDesc LIKE '%Client%'
Here is the first UDF, the one that worked fine.
CREATE FUNCTION dbo.RO_GetCommaSeparatedNeedsTable
(@ContactId int)
RETURNS nvarchar(100)
AS
BEGIN
DECLARE @Result nvarchar(100)
SELECT @Result = IsNull(@Result + ', ' + Description, Description)
FROM NeedsTable
INNER JOIN ContactToNeeds ON NeedsTable.NeedId = ContactToNeeds.NeedsId
WHERE ContactToNeeds.ContactId = @ContactId
RETURN @Result
END
It stores multiple values in one cell.
The second one, which slows the query something fierce is supposed to go to a table and look how many appointments each clients are booked for in the future. Here it is
CREATE FUNCTION dbo.RO_GetRemainingApps
(@ContactId int)
RETURNS int
AS
BEGINNDeclare @Result int
DECLARE @Date datetime
SET @Date = getdate()
SELECT @Result = Count(BeginDateTime)
FROM Calendar
WHERE ContactId = @ContactId AND Begindatetime > @Date
RETURN @Result
END
I'm tickled pink that my query is returning anything at all except "syntax error" but I would also like to use it which I can't in its current state. Thanks for looking and once again, any help is appreciated.
Fred
I have a query that uses two UDF's. When I included the first one with help of George (thank you) it slowed down my query just a little bit. Now that I tried to write my own it takes close to two minutes instead of a few seconds so I gather I didn't do such a good job. Any help is of course greatly appreciated. Here is the query:
SELECT DISTINCT
ContactExtendedProperties.ActiveStatus,
Contacts.LastName,
Contacts.FirstName,
ContactExtendedProperties.WelcomeCard,
ContactExtendedProperties.BeforePic,
ContactExtendedProperties.AfterPic,
dbo.RO_GetCommaSeparatedNeedsTable(Contacts.ContactId) AS Description,
ContactExtendedProperties.CurrentStatus,
ContactExtendedProperties.SpacesCompleted,
ContactExtendedProperties.ClientLongevity,
MaxFollowupdate.Maxdate AS DateOfFollowup,
ContactExtendedProperties.ReceivedEval,
ContactExtendedProperties.OncancellationList,
dbo.RO_GetRemainingApps(Contacts.ContactId) AS RemainingApps
FROM Contacts
INNER JOIN ContactExtendedProperties ON Contacts.ContactId = ContactExtendedProperties.ContactId
INNER JOIN ClientStatus ON Contacts.ClientStatusId = ClientStatus.StatusId
INNER JOIN (
SELECT ContactId, MAX(DateOfFollowup) AS MaxDate
FROM ContactFollowup
GROUP BY ContactId) As MaxFollowupDate ON Contacts.ContactId = MaxFollowupDate.ContactId
INNER JOIN ContactFollowup ON Contacts.ContactId = ContactFollowup.ContactId
INNER JOIN RO_Users ON Contacts.PrimaryConsultantId = RO_Users.UserId
INNER JOIN ContactToNeeds ON Contacts.ContactId = ContactToNeeds.ContactId
INNER JOIN NeedsTable ON ContactToNeeds.NeedsId = NeedsTable.NeedId
WHERE Clientstatus.StatusDesc LIKE '%Client%'
Here is the first UDF, the one that worked fine.
CREATE FUNCTION dbo.RO_GetCommaSeparatedNeedsTable
(@ContactId int)
RETURNS nvarchar(100)
AS
BEGIN
DECLARE @Result nvarchar(100)
SELECT @Result = IsNull(@Result + ', ' + Description, Description)
FROM NeedsTable
INNER JOIN ContactToNeeds ON NeedsTable.NeedId = ContactToNeeds.NeedsId
WHERE ContactToNeeds.ContactId = @ContactId
RETURN @Result
END
It stores multiple values in one cell.
The second one, which slows the query something fierce is supposed to go to a table and look how many appointments each clients are booked for in the future. Here it is
CREATE FUNCTION dbo.RO_GetRemainingApps
(@ContactId int)
RETURNS int
AS
BEGINNDeclare @Result int
DECLARE @Date datetime
SET @Date = getdate()
SELECT @Result = Count(BeginDateTime)
FROM Calendar
WHERE ContactId = @ContactId AND Begindatetime > @Date
RETURN @Result
END
I'm tickled pink that my query is returning anything at all except "syntax error" but I would also like to use it which I can't in its current state. Thanks for looking and once again, any help is appreciated.
Fred