Stored Proc - Get DISTINCT errors with a Date Range, not providing DISTINCT

  • Good Morning everyone,

    I'm attempting to Merge a couple of stored procedures into one.  I need to be able to pass in a DISTINCT error message and a date range.  Individually, they both work...however, when I merge them and try to run I don't get the DISTINCT errors.  They all just come in.

    Looking for some guidance if someone knows where I'm going wrong.

    ALTER PROCEDURE [dbo].[GetUniqueValueNames]

    @MessageProcessFolio nvarchar(75),

    @StartDate DateTime = NULL,

    @EndDate DateTime = NULL

    AS

    SELECT DISTINCT [MessageError], MessageLogDate

    FROM [dbo].[LogArchive]

    WHERE [MessageProcessFolio] = @MessageProcessFolio

    AND MessageLogDate BETWEEN Coalesce(@StartDate, MessageLogDate) AND

    Coalesce(@EndDate, MessageLogDate)

    • This topic was modified 4 years, 11 months ago by  daerne.
  • Just to clarify, when you say distinct errors are you wanting each individual error type to only show up once?  With the way this is written if you have the same error a minute apart its going to show the same error and both times a minute apart because those combination of values is distinct.

  • Correct.

    I am searching through a log file to get distinct error messages.  The extra is to then grab them in a date range.

  • This might not be 100% correct, but this seems to work for me but as a note, basically whats going to happen here is if the error occurred on multiple dates you aren't going to see that you're only going to see that on at least one of the days that error occurred which I believe is what you are asking for.

    WITH CTE ([MessageError], [MessageLogDate], RowNumber)
    AS (SELECT [MessageError],
    [MessageLogDate],
    ROW_NUMBER() OVER(PARTITION BY [MessageError]
    ORDER BY [MessageError])
    FROM dbo.[LogArchive])
    SELECT CTE.[MessageError],CTE.[MessageLogDate]
    FROM CTE
    WHERE CTE.RowNumber='1'
    ORDER BY CTE.[MessageError]
  • In my searching, I started seeing ROW_NUMBER and OVER search variations.

    Thanks, I'll try this and try to understand those elements a little better.

     

  • I'm learning myself but from my understanding, in doing this you're giving every iteration of that error message an assigned row number 1...2...3...4. depending on how many times it appears, and by saying only give me row number 1 you will only get that message once.

    Row Number is important here but the main keyword in play is Partition By

  • a simple group by and max would give the desired result without the need to do a sort/numbering with row_number

    ALTER PROCEDURE dbo.GetUniqueValueNames
    @MessageProcessFolio nvarchar(75),
    StartDate DateTime = NULL,
    @EndDate DateTime = NULL

    AS

    SELECT MessageError, max(MessageLogDate) as MessageLogDate
    FROM dbo.LogArchive
    WHERE MessageProcessFolio = @MessageProcessFolio
    AND MessageLogDate BETWEEN Coalesce(@StartDate, MessageLogDate) AND Coalesce(@EndDate, MessageLogDate)
    group by MessageError
  • SSChampion,

    Thanks that worked.  max(MessageLogDate) as MessageLogDate was what I was missing.

     

  • Makes sense, I messed with that for a second but I was being dumb about it.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply