Solved

Duplicates in Aggregate & Min Query

Posted on 2013-11-25
2
262 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

734 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