SQL查询组按不同字段,按其他字段排序

时间:2013-06-28 08:45:55

标签: sql ms-access group-by distinct

我有以下SQL字符串:

sSQL_Select = "SELECT distinct local_insurer, subsidiary, location FROM T_WILMA WHERE PARENT=" & lParent_ID & _
                    " AND ACC_YEAR=" & lAcc_Year_ID & " AND ZOMBIE = FALSE GROUP BY local_insurer ORDER BY " & _
                    "subsidiary ASC, location ASC;"

Access会抛出错误,因为我无法执行此操作 我需要的是具有独特本地保险公司的所有记录,但是按子公司和位置字段的字母顺序排列 不知何故,我似乎从来没有得到正确的结果 有任何想法吗?

2 个答案:

答案 0 :(得分:1)

从查询中删除GROUP BY local_insurer。 (distinct将确保您只获得唯一的值组合。)

答案 1 :(得分:0)

您收到错误的原因是您没有在GROUP BY中包含SELECT中的所有字段。例如,如果选择a,b,c,则需要GROUP BY a,b,c(或c,b,a ...)。

sSQL_Select = "SELECT distinct local_insurer, subsidiary, location FROM T_WILMA WHERE PARENT=" & lParent_ID & _
                " AND ACC_YEAR=" & lAcc_Year_ID & " AND ZOMBIE = FALSE GROUP BY local_insurer, subsidiary, location ORDER BY " & _
                "subsidiary ASC, location ASC;"