有两个SQL表:
Parents:
+--+---------+
|id| text |
+--+---------+
| 1| Blah |
| 2| Blah2 |
| 3| Blah3 |
+--+---------+
Childs
+--+------+-------+
|id|parent|feature|
+--+------+-------+
| 1| 1 | 123 |
| 2| 1 | 35 |
| 3| 2 | 15 |
+--+------+-------+
我想从Parents表的每一行中选择单个查询,并从Childs表中选择与“parent” - “id”值和最大“feature”列值相关的每一行 . 在此示例中,结果应为:
+----+------+----+--------+---------+
|p.id|p.text|c.id|c.parent|c.feature|
+----+------+----+--------+---------+
| 1 | Blah | 1 | 1 | 123 |
| 2 | Blah2| 3 | 2 | 15 |
| 3 | Blah3|null| null | null |
+----+------+----+--------+---------+
其中p =父表和c =子表
我尝试LEFT OUTER JOIN和GROUP BY,但MSSQL Express告诉我,使用GROUP BY的查询需要在每个非Groupped字段上使用Aggregate函数 . 而且我不想将它们全部分组,而是选择顶行(使用自定义排序) .
我完全没有想法......