Query to return all data that has same site, section and date

  • I have a query Select Id, WOCode, Site, Section, CompletionDate from work orders where type = 'BRE'. I want to be able to return list of all WOCode that has same Site,Section and CompletionDate. The date field will be date only with no time

  • Nomvula wrote:

    I have a query Select Id, WOCode, Site, Section, CompletionDate from work orders where type = 'BRE'. I want to be able to return list of all WOCode that has same Site,Section and CompletionDate. The date field will be date only with no time

    same Site,Section and CompletionDate

    Same as what?

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • The list of work orders where work orders have the same Site, Section and same date

     

  • SELECT WOCode
    FROM WorkOrders
    WHERE Site = Section AND Section = CompletionDate

    I'm fairly certain that this is not what you want, but that is what your post is asking for.

    If you provide consumable DDL, sample data and desired results (and with >3,500 points, you should know how to do that), all will become clear.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • This was removed by the editor as SPAM

  • You didn't provide sample data and expected results.

    This is my best guess:

    WITH BRE AS
    (
    SELECT Id, WOCode, Site, Section, CompletionDate
    , MAX(CASE WHEN [type] = 'BRE' THEN 'Y' ELSE 0 END) OVER(PARTITION BY Site, Section, CompletionDate) AS Is_BRE
    FROM [work orders]
    )
    SELECT Id, WOCode, Site, Section, CompletionDate
    FROM BRE
    WHERE Is_BRE = 'Y'

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • My guess is to copy Drew's query but with DISTINCT in the outer SELECT

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

  • Steve Collins wrote:

    My guess is to copy Drew's query but with DISTINCT in the outer SELECT

    Presumably, the ID field is the primary/unique key, so adding DISTINCT will not change the results, but it will probably increase processing time.  What is your reasoning for adding DISTINCT?

    Also, the OP said they wanted to include a "list of ALL WOCode" (emphasis added).  I would think that would exclude using DISTINCT.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • If the ID is unique then yes DISTINCT would do nothing.  I was sort of thinking right-to-left if there were duplicate properties there could also be duplicate ID's.  No data tho

    Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können

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

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