views:

49

answers:

2

I have a table that has the following data:

id  status  date    part_no part_name 

1   high    1/2/09  55      screw;
1   medium  1/2/09  55      screw; 
2   high    2/2/09  32      plug;
3   low     4/8/09  59      bolt;
4   medium  5/6/09  48      tie;
4   low     5/6/09  48      tie;

I want to write a query that will give me one row per id number and I want to be able to show all the fields in the result. for instance the result of my query should be:

1   medium  1/2/09  55  screw;
2   high    2/2/09  32  plug;
3   low     4/8/09  59  bolt;
4   low     5/6/09  48  tie;

Thanks in advance for any help you can give.

+1  A: 
SELECT
    id,
    MIN(status),
    MIN(date_part),
    part_name
FROM <table>
GROUP BY
    id,
    part_name
Brett Veenstra
can you do a min() on the status column with varchar values like low, medium, and high?
Diakonia7
I was looking at this from the perspective of the questioner. If order is important, other means would be necessary (@OMG Ponies started that thread)
Brett Veenstra
+1  A: 
   SELECT id, 
          MIN(status), 
          date, 
          part_no, 
          part_name  
    FROM table
GROUP BY id, date, part_no, part_name

..is what you're going for, but the complication is that you're storing strings so it depends on the db for how it applies the MIN function to text/characters. In a normalized setup, the status would be a separate code table, and the key would/could be an ID which would work better with the MIN function.

OMG Ponies
thanks for the clarification on min()
Diakonia7
This database is anything but normalized and allows the user to input information that is inconsistent with the business rules. The user is allowed to downgrade a status in the same day so using max(date) worked for records that have different dates but did not work for records that have the same date
jen