Below is the sample data:
c1 c2 c3 c4 c5
1 a1 a 1 1
2 a2 a 2 1
3 a3 a 3 1
4 a4 a 4 1
5 b1 b 1 1
6 b2 b 2 1
7 b3 b 3 1
8 b4 b 4 1
9 a1 c 3 1
I want to get the the below details:
c1 c2 c3 c4 c5
1 a1 a 1 1
5 b1 b 1 1
9 a1 c 3 1
C1 is primary key, the criteria is for any given unique(c2) where c4 is the lowest, I want to return the contents(all the 5 columns) of the row.
Try this:
SQL Fiddle Demo
Update:1 In SQL the returned results is a set set(unless you specify an
ORDER BYclause, it is a cursor in this case), wherein the order is not guaranteed. This is a standard. You should use an ORDER BY clause if you want to guarantee a specific order. In your case , the results is not guaranteed to be ordered like1 5 9. AddORDER BY c1instead.The
ORDER BYclause might be crucial in some cases, for example, if want to get the top three rows, or the maximum one, in this case you have to specify anORDER BYclause.So if you wants to persist a specific order the you have specify an
ORDER BY.1 As noted by @Fahim Parker, see the comments below.