当前位置: 数据库>sqlserver
sql server中Group By语句的用法
来源: 互联网 发布时间:2014-08-29
本文导语: 本节内容: group by 分组语句。 group by指定用来放置输出行的组,并且如果 SELECT 子句 中包含聚合函数,则计算每组的汇总值。 指定 GROUP BY 时,选择列表中任一非聚合表达式内的所有列都应包含在 GROUP BY 列表中,或 GROUP BY 表...
本节内容:
group by 分组语句。
group by指定用来放置输出行的组,并且如果 SELECT 子句 中包含聚合函数,则计算每组的汇总值。
指定 GROUP BY 时,选择列表中任一非聚合表达式内的所有列都应包含在 GROUP BY 列表中,或 GROUP BY 表达式必须与选择列表表达式完全匹配。
例句:
代码示例:
SELECT U_ID,House_Addrinfo,
COUNT(U_ID) AS CX
FROM YX_ChuShou
GROUP BY U_ID
ORDER BY CX DESC
COUNT(U_ID) AS CX
FROM YX_ChuShou
GROUP BY U_ID
ORDER BY CX DESC
这条语句出现"选择列表中的列 'YX_ChuShou.House_Addrinfo' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。"的错误提示。
正确写法,如下:
代码示例:
SELECT U_ID,min(House_Addrinfo),
COUNT(U_ID) AS CX
FROM YX_ChuShou
GROUP BY U_ID
ORDER BY CX DESC
或
SELECT U_ID,House_Addrinfo,
COUNT(U_ID) AS CX
FROM YX_ChuShou
GROUP BY U_ID ,House_Addrinfo
ORDER BY CX DESC
COUNT(U_ID) AS CX
FROM YX_ChuShou
GROUP BY U_ID
ORDER BY CX DESC
或
SELECT U_ID,House_Addrinfo,
COUNT(U_ID) AS CX
FROM YX_ChuShou
GROUP BY U_ID ,House_Addrinfo
ORDER BY CX DESC
>>> 您可能感兴趣的文章:
sql server中group by的用法详解
有关distinct 多列问题结合group by的解决方法
group by 与 order by的用法一例