sql对一个字段进行分组 怎么显示多个字段
selectIteID,count(IteID)WrongCountfromExam_ItemListWrong_ViewwhereIsKeyTrue=2andUserI...
select IteID,count(IteID) WrongCount from Exam_ItemListWrong_View
where IsKeyTrue=2 and UserId in
(select UserId from Sys_UserInfo where classid in
(select ParameterValue from Sys_UserParameter where U_ID=60))
group by IteID order by WrongCount desc
这样是可以的 但只能显示一个字段 我还需要显示同一张表里其他的字段
怎么才能做到呢?
select X.*,WrongCount
from Exam_ItemListWrong_View X,(select IteID,count(IteID) WrongCount from Exam_ItemListWrong_View
where IsKeyTrue=2 and UserId in
(select UserId from Sys_UserInfo where classid in
(select ParameterValue from Sys_UserParameter where U_ID=60))
group by IteID) AS Y
WHERE X.ITEID=Y.ITEID
order by WrongCount desc
这样也不行 会显示重复数据 展开
where IsKeyTrue=2 and UserId in
(select UserId from Sys_UserInfo where classid in
(select ParameterValue from Sys_UserParameter where U_ID=60))
group by IteID order by WrongCount desc
这样是可以的 但只能显示一个字段 我还需要显示同一张表里其他的字段
怎么才能做到呢?
select X.*,WrongCount
from Exam_ItemListWrong_View X,(select IteID,count(IteID) WrongCount from Exam_ItemListWrong_View
where IsKeyTrue=2 and UserId in
(select UserId from Sys_UserInfo where classid in
(select ParameterValue from Sys_UserParameter where U_ID=60))
group by IteID) AS Y
WHERE X.ITEID=Y.ITEID
order by WrongCount desc
这样也不行 会显示重复数据 展开
2018-09-29
展开全部
SELECT "最高分",student.*
FROM student ,(SELECT MAX(score) AS score,`subject` FROM student GROUP BY `subject`)b
WHERE student.`score` = b.score
AND student.`subject` = b.subject
UNION
SELECT "最低分",student.*
FROM student ,(SELECT MIN(score) AS score,`subject` FROM student GROUP BY `subject`)b
WHERE student.`score` = b.score
AND student.`subject` = b.subject;
亲试可行,推荐这种,可以看看,容易理解
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
SELECT COL1,COL2,COL3,TEST
FROM TABLENAME X,(SELECT COL1,COUNT(COL1) AS TEST GROUP BY COL1)AS Y WHERE X.COL1=Y.COL1 只有上面这种方法。GROUP BY 必须按SELECT后不是聚合函数的所有字段分组。
FROM TABLENAME X,(SELECT COL1,COUNT(COL1) AS TEST GROUP BY COL1)AS Y WHERE X.COL1=Y.COL1 只有上面这种方法。GROUP BY 必须按SELECT后不是聚合函数的所有字段分组。
更多追问追答
追问
这样也不行啊 会报错
(除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。)
追答
select X.*,WrongCount
from Exam_ItemListWrong_View X,(select IteID,count(IteID) WrongCount from Exam_ItemListWrong_View
where IsKeyTrue=2 and UserId in
(select UserId from Sys_UserInfo where classid in
(select ParameterValue from Sys_UserParameter where U_ID=60))
group by IteID) AS Y
WHERE X.ITEID=Y.ITEID
order by WrongCount desc
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先你题目不是很明确我理解的是这样:
SELECT COL1,COUNT(COL2),COUNT(3) FROM TABLENAME GROUP BY COL1
SELECT COL1,COUNT(COL2),COUNT(3) FROM TABLENAME GROUP BY COL1
追问
不是这样的 这样可以吗
SELECT COL1,COL2,COL3, COUNT(COL1) FROM TABLENAME GROUP BY COL1
追答
这样肯定不可以,你前面出现了后面group by也要出现
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你的要求是不会有答案的,要不你需要显示的字段也参加分组
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询