Solved

Duplicates in Aggregate & Min Query

Posted on 2013-11-25
2
255 Views
Last Modified: 2014-01-08
I have a query . . .that works perfectly (before I add RESOURCE_ID), but when I add resource, I get duplicates.

SELECT        WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, MIN(SEQUENCE_NO) AS Expr1, RESOURCE_ID
FROM            OPERATION
GROUP BY WORKORDER_TYPE, WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, STATUS, RESOURCE_ID
HAVING        (WORKORDER_TYPE = 'W') AND (STATUS = 'R')
ORDER BY WORKORDER_BASE_ID

I would like to add Resource_ID. . .but resource may be duplicated . . I don't want duplicates for Resource. . I would think I need to put a query in a query?

WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, SEQUENCE_NO, RESOURCE_ID
100, 1, 1, 10, C, ISSUE
100, 1, 1, 20, R, ASSEMBLE
100, 1, 1, 30, R, MACHINE
100, 1, 1, 40, R, MACHINE

I would like to see this, please. . .but I'm missing something

WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, SEQUENCE_NO, RESOURCE_ID
100, 1, 1, 20, R, ASSEMBLE

My query results are this. . .
WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, SEQUENCE_NO, RESOURCE_ID
100, 1, 1, 20, R, ASSEMBLE
100, 1, 1, 30, R, MACHINE
100, 1, 1, 40, R, MACHINE
0
Comment
Question by:CadenceAerospaceIT
2 Comments
 
LVL 11

Expert Comment

by:Louis01
ID: 39676700
Assuming you want to return the RESOURCE_ID of the same row as MIN(SEQUENCE_NO) and that SEQUENCE_NO is unique in each row for WORKORDER_BASE_ID, you can try the following
SELECT t1.WORKORDER_BASE_ID, t1.WORKORDER_LOT_ID, t1.WORKORDER_SPLIT_ID, t1.MIN_SEQUENCE_NO, t2.RESOURCE_ID
  FROM (SELECT WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID, MIN(SEQUENCE_NO) AS MIN_SEQUENCE_NO
          FROM OPERATION
         GROUP BY WORKORDER_BASE_ID, WORKORDER_LOT_ID, WORKORDER_SPLIT_ID
        HAVING WORKORDER_TYPE = 'W'
           AND STATUS = 'R') t1
        INNER JOIN (SELECT WORKORDER_BASE_ID, SEQUENCE_NO, RESOURCE_ID FROM OPERATION) t2
    ON t1.WORKORDER_BASE_ID = t2.WORKORDER_BASE_ID
   AND t1.MIN_SEQUENCE_NO = t2.SEQUENCE_NO
 ORDER BY t1.WORKORDER_BASE_ID

Open in new window

0
 
LVL 31

Accepted Solution

by:
awking00 earned 500 total points
ID: 39678333
You need to define the criteria for which resource_id to select (i.e. lowest sequence_no  or by alphabetical order where status = 'R').
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now