- Мне нужно добавить дату завершения в финальную таблицу.
- Заменить предложение IN на объединение для повышения производительности.
- Пожалуйста, дайте мне знать, если требуется какая-либо дополнительная информация.
ALTER PROCEDURE [dbo].[CategoryCounts]
@Skeys VARCHAR(8000) = '1,2,3,4,5,6,7,8,9,10',
@Inc BIT = 0,
@FromDate DATETIME = NULL,
@ToDate DATETIME = NULL
AS
BEGIN
SELECT @FromDate = ISNULL(@FromDate, '01/01/2005'),
@ToDate = ISNULL(@ToDate, '12/31/2050');
DECLARE @FinalTable TABLE
([Row#] INT,
AKey INT,
PersonName VARCHAR(200),
SKey INT,
Category VARCHAR(100),
CategorySamples INT,
type INT,
NoResults INT
);
INSERT INTO @FinalTable
(AKey,
PersonName,
SKey,
Category,
CategorySamples,
type,
NoResults
)
SELECT a.AKey,
a.LastName + ', ' + a.Firstname AS PersonName,
s.SKey,
s.name,
0 AS CategorySamples,
0 AS type,
0 AS NoResults
FROM Categorys s,
AccountBookss a
WHERE s.SKey IN
(
SELECT [Key]
FROM dbo.funcBuildKeyTable(@Skeys)
)
AND s.activeind = 1
AND a.AKey IN
(
SELECT DISTINCT
a.AKey
FROM AccountBookss a
INNER JOIN Users u ON a.AKey = u.AKey
INNER JOIN CategorySampleIdentifications ssi ON u.UserKey = ssi.PrimaryPersonNameUserKey
INNER JOIN CategoryCategorySamples ss ON ssi.CategorySampleKey = ss.CategorySampleKey
WHERE ss.CompleteDate BETWEEN @FromDate AND @ToDate
)
ORDER BY s.Name,
PersonName;
END;