第1个回答 推荐于2017-11-26
以下语句可用于ms sql server。
SELECT COUNT(1) AS 数量, c.用户组名称 FROM 文件表 a INNER JOIN 用户表 b ON a.发布人 = b.用户名 INNER JOIN 用户组表 c ON b.用户组id = c.id GROUP BY c.id, c.用户组名称 ORDER BY 数量本回答被提问者采纳
第2个回答 2013-10-18
ARTICLE_T
[TITLE],[CONTENT],[PUBLISH_PERSON],[PUBLISH_TIME]
USER_T
[GROUP_ID],[USER_NAME],[USER_PASS]
GROUP_T
[GROUP_ID],[GROUP_NAME]
/*只显示组,并按组的发文数量排序*/
SELECT UT.GROUP_ID,COUNT(*) FROM ARTICLE_T AT,USER_T UT
WHERE AT.PUBLISH_PERSON=UT.USER_NAME
GROUP BY UT.GROUP_ID ORDER BY COUNT(*) DESC
/*显示所有数据并排序*/
SELECT T.* FROM
(SELECT ROW_NUMBER() OVER(PRATITION BY UT.GROUP_ID ORDER BY UT.USER_NAME) AS N,COUNT(AT.TITLE) OVER(PRATITION BY UT.GROUP_ID) AS C,AT.*,UT.*,GT.* FROM ARTICLE_T AT,USER_T UT,GROUP_T GT
WHERE AT.PUBLISH_PERSON=UT.USER_NAME
AND UT.GROUP_ID=GT.GROUP_ID) T
ORDER BY T.C DESC,T.GROUP_ID DESC,T.N ASC
/*只显示发文用户,并按发文用户的发文数量排序*/
SELECT UT.USER_NAME ,COUNT(*) FROM ARTICLE_T AT,USER_T UT
WHERE AT.PUBLISH_PERSON=UT.USER_NAME
GROUP BY UT.USER_NAME ORDER BY COUNT(*) DESC
/*显示所有数据并排序*/
SELECT T.* FROM
(SELECT ROW_NUMBER() OVER(PRATITION BY UT.USER_NAME ORDER BY AT.TITLE) AS N,COUNT(AT.TITLE) OVER(PRATITION BY UT.USER_NAME) AS C,AT.*,UT.*,GT.* FROM ARTICLE_T AT,USER_T UT,GROUP_T GT
WHERE AT.PUBLISH_PERSON=UT.USER_NAME
AND UT.GROUP_ID=GT.GROUP_ID) T
ORDER BY T.C DESC,T.GROUP_ID DESC,T.N ASC