Solved

Duplicates in Aggregate & Min Query

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
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…
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.
Viewers will learn how the fundamental information of how to create a table.

839 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