I have a table which looks like this.
+----+--------+---------+-----------+------------------------+
| id | parent | type | libTypeId | name |
+----+--------+---------+-----------+------------------------+
| 2 | 1 | project | 2 | 1p6m4x0y1z_1.2-1.8_hvt |
| 5 | 1 | project | 6 | 1p6m4x0y1z_1.2-1.8_hvt |
| 8 | 1 | project | 16 | 1p6m4x0y1z_1.2-1.8_hvt |
| 11 | 1 | project | 21 | 1p6m4x0y1z_1.2-1.8_hvt |
| 3 | 1 | project | 2 | 1p6m4x0y1z_1.2-1.8_lvt |
| 6 | 1 | project | 6 | 1p6m4x0y1z_1.2-1.8_lvt |
| 9 | 1 | project | 16 | 1p6m4x0y1z_1.2-1.8_lvt |
| 12 | 1 | project | 21 | 1p6m4x0y1z_1.2-1.8_lvt |
| 1 | 1 | project | 2 | 1p6m4x0y1z_1.2-1.8_svt |
| 4 | 1 | project | 6 | 1p6m4x0y1z_1.2-1.8_svt |
| 7 | 1 | project | 16 | 1p6m4x0y1z_1.2-1.8_svt |
| 10 | 1 | project | 21 | 1p6m4x0y1z_1.2-1.8_svt |
| 13 | 2 | project | 2 | 065nm_GPIO |
| 17 | 2 | project | 4 | 065nm_GPIO |
| 14 | 2 | project | 6 | 065nm_GPIO |
| 18 | 2 | project | 12 | 065nm_GPIO |
| 15 | 2 | project | 16 | 065nm_GPIO |
| 16 | 2 | project | 21 | 065nm_GPIO |
| 19 | 2 | project | 2 | 065nm_Specialized |
+----+--------+---------+-----------+------------------------+
What I am looking for is a query which results in a list where we get all projects with id = 1 which is sorted by name, libtypeid and the FIRST libtypeid only.
In otherwords I should end up with this:
+----+--------+---------+-----------+------------------------+
| id | parent | type | libTypeId | name |
+----+--------+---------+-----------+------------------------+
| 2 | 1 | project | 2 | 1p6m4x0y1z_1.2-1.8_hvt |
| 3 | 1 | project | 2 | 1p6m4x0y1z_1.2-1.8_lvt |
| 1 | 1 | project | 2 | 1p6m4x0y1z_1.2-1.8_svt |
+----+--------+---------+-----------+------------------------+
Now I can get this far but how do I get only the first one??
Variant.objects.filter(parent=self.id).order_by('name', 'libtype_id')
I then further do this..
full = Variant.objects.filter(parent=self.id).order_by('name', 'libtype_id')
names, out = [], []
for v in full:
if v.name not in names:
out.append(v)
names.append(v.name)
return out
Much appreciate if someone can clean this up a bit..
Here is the answer I was looking for..
This basically does a default
GROUP BYwhich reduces everything to what I need. Thanks to all who attempted answered!