Advertisement
Guest User

Правило поиска дублей: стандартное правило дублей по имени

a guest
Jul 9th, 2020
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.84 KB | None | 0 0
  1. USE [ntl-7.16.1]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[tsp_FindAccountDuplicateByName]    Script Date: 09.07.2020 10:26:21 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8.  
  9. ALTER PROCEDURE [dbo].[tsp_FindAccountDuplicateByName] (
  10.     @parsedConfig CreatingObjectInfo READONLY,
  11.     @sysAdminUnit UNIQUEIDENTIFIER,
  12.     @ruleId UNIQUEIDENTIFIER
  13. )
  14. AS
  15. BEGIN
  16.     DECLARE @parsedConfigRowsCount INT = (SELECT COUNT(*) FROM @parsedConfig);
  17.     IF OBJECT_ID('tempdb..#searchAccount') IS NOT NULL
  18.     BEGIN
  19.         DROP TABLE #searchAccount
  20.     END
  21.     CREATE TABLE #searchAccount (
  22.         [Name] NVARCHAR(128) COLLATE database_default,
  23.         [SortDate] DATETIME
  24.     );
  25.  
  26.     CREATE NONCLUSTERED INDEX IX_#searchAccount_NameSortDate ON #searchAccount([Name], [SortDate]);
  27.  
  28.     IF @parsedConfigRowsCount = 0
  29.     BEGIN
  30.         INSERT INTO #searchAccount ([Name], [SortDate])
  31.         SELECT
  32.             [dedup].[Name],
  33.             MAX([dedup].[SortDate]) [SortDate]
  34.         FROM (
  35.             SELECT [Id],
  36.                 [Name] AS [Name],
  37.                 MAX([ModifiedOn]) [SortDate]
  38.             FROM [VwAccountCleanDataValues] WITH (NOEXPAND)
  39.             GROUP BY [Id], [Name]
  40.         ) AS [dedup]
  41.         GROUP BY [dedup].[Name]
  42.         HAVING COUNT(*) > 1;
  43.     END;
  44.     ELSE
  45.     BEGIN
  46.         INSERT INTO #searchAccount ([Name], [SortDate])
  47.         SELECT
  48.             [dbo].[fn_NormalizeString]([Name], N'0-9a-zа-яא-ת') AS [Name],
  49.             GETDATE() AS [SortDate]
  50.         FROM @parsedConfig
  51.     END;
  52.  
  53.     INSERT INTO [AccountDuplicateSearchResult] ([AccountId], [GroupId], [RuleId], [SysAdminUnitId])
  54.     SELECT
  55.         [vr].[Id],
  56.         DENSE_RANK() OVER (ORDER BY [vr].[SortDate] DESC, [vr].[Name] ASC),
  57.         @ruleId [RuleId],
  58.         @sysAdminUnit
  59.     FROM (
  60.         SELECT
  61.             [v].[Id],
  62.             [v].[Name],
  63.             [r].[SortDate]
  64.         FROM (
  65.             SELECT [Id], [ModifiedOn], [Name]
  66.             FROM [VwAccountCleanDataValues] WITH (NOEXPAND)
  67.         ) [v], #searchAccount [r]
  68.         WHERE [v].[Name] = [r].[Name]
  69.         GROUP BY [v].[Id], [v].[Name], [r].[SortDate]
  70.     ) [vr];
  71. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement