Reputation: 748
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?
Upvotes: 1
Views: 1237
Reputation: 52250
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
Upvotes: 3