Search code examples
sqlderby

How To Write SQL Statement?


I have This table in which i wanted the unique bread id whichever comes first into the table i wanted it into the query result.

means in following scenario i wanted these records which has following id.

58 ,59 , 60 , 61 ,68 , 69

id  bread id                                        exception               time
58  ID:D--113042539-54026-1363171110733-3:1:1:1:1   null                    16:33.2
59  ID:D--113042539-54026-1363171110733-3:1:1:1:2   null                    17:18.7
60  ID:D--113042539-54026-1363171110733-3:1:1:1:3   null                    17:18.9
61  ID:D--113042539-54026-1363171110733-3:1:1:1:4   null                    17:19.0
62  ID:D--113042539-54026-1363171110733-3:1:1:1:4   jmsexception            17:20.1
63  ID:D--113042539-54026-1363171110733-3:1:1:1:4   jmsexception            17:20.1
64  ID:D--113042539-54026-1363171110733-3:1:1:1:4   jmsexception            17:20.1
65  ID:D--113042539-54026-1363171110733-3:1:1:1:4   jmsexception            17:20.1
66  ID:D--113042539-54026-1363171110733-3:1:1:1:4   jmsexception            17:20.5
68  ID:D--113042539-54026-1363171110733-3:1:1:1:9   jmsexception            17:21.2
69  ID:D--113042539-54300-1363171861821-3:1:1:1:1   InvalidXPathExpression  22:28.1

Solution

  • Query:

    SQLFIDDLEExample

    SELECT `t1`.*
    FROM Table1 t1
    WHERE t1.id = (SELECT MIN(t.id)
                   FROM Table1 t
                   WHERE t.`bread id`= t1.`bread id`)
    

    Result:

    | ID |                                      BREAD ID |              EXCEPTION |    TIME |
    -----------------------------------------------------------------------------------------
    | 58 | ID:D--113042539-54026-1363171110733-3:1:1:1:1 |                 (null) | 16:33.2 |
    | 59 | ID:D--113042539-54026-1363171110733-3:1:1:1:2 |                 (null) | 17:18.7 |
    | 60 | ID:D--113042539-54026-1363171110733-3:1:1:1:3 |                 (null) | 17:18.9 |
    | 61 | ID:D--113042539-54026-1363171110733-3:1:1:1:4 |                 (null) | 17:19.0 |
    | 68 | ID:D--113042539-54026-1363171110733-3:1:1:1:9 |           jmsexception | 17:21.2 |
    | 69 | ID:D--113042539-54300-1363171861821-3:1:1:1:1 | InvalidXPathExpression | 22:28.1 |