Solved

Duplicates in Aggregate & Min Query

Posted on 2013-11-25
2
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 32

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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

626 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