1

I am trying to sort a table so that duplicates dont show up on it based on two different columns (MODEL_NUMBER and YEAR_INTRODUCED)

Right now my query is designed like:

cmd = @"Select * From ARCHIVE_DECADE_TBL WHERE DECADE_" + decade + @"=@decade AND PRODUCT_LINE=@Line AND QUANTITY is not null AND QUANTITY <> 0 ORDER BY PRODUCT_NAME;";

Here is the table layout:

ARCHIVE_ID    MODEL_NUMBER    YEAR_INTRODUCED    LOCATION
1001          B10             1989               SKID 43
1002          B10             1989               SKID 48
1003          B10             1989               SKID 73

The ARCHIVE_ID is the primary key. Should I use a group by? If I do use a group by which ARCHIVE_ID would stay?

1
  • It depends on the aggregate function (min, max, count, ...) which archive_id would stay. Commented Jun 20, 2016 at 22:13

1 Answer 1

3

Depends on the result set that you wish.

If the resultset only contains MODEL_NUMBER and YEAR_INTRODUCED, you can simply use distinct:

SELECT DISTINCT 
     MODEL_NUMBER, 
     YEAR_INTRODUCED 
FROM ARCHIVE_DECADE_TBL

If you want the resultset to include other columns, you have to decide which values you want to show up. Since you only have one row per unique pairing, you can only show one value from the other columns. Which one do you want to show up? And do the values need to come from the same row?

You could do something like

SELECT   MIN(ARCHIVE_ID), 
         MODEL_NUMBER, 
         YEAR_INTRODUCED, 
         MIN(LOCATION)
FROM     ARCHIVE_DECADE_TBL 
GROUP BY MODEL_NUMBER, 
         YEAR_INTRODUCED

...if you don't care if the values come from the same row.

If you do care, you have to do something a little more complicated, such as

SELECT A.* 
FROM   ARCHIVE_DECADE_TBL A
JOIN   (SELECT   MIN(ARCHIVE_ID), 
                 MODEL_NUMBER, 
                 YEAR_INTRODUCED 
        FROM     ARCHIVE_DECADE_TBL 
        GROUP BY MODEL_NUMBER, 
                 YEAR_INTRODUCED) B
ON     A.ARCHIVE_ID = B.ARCHIVE_ID
Sign up to request clarification or add additional context in comments.

2 Comments

When I try the second option the query changes the column name from LOCATION to Expr1000, do you know how I can solve that?
You need to add a column alias, e.g. if you want to name the column MIN_LOCATION you would use SELECT MIN(LOCATION) MIN_LOCATION FROM etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.