一般group by 后面带多少个字段?

taz01 2010-08-28 11:31:52
select UserID,max(CreateON) as CreateOn from 文章表
group by UserID


如果还要多个字段
应该在group by后面加不?
还是嵌套两个select?
...全文
5915 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
霸气哦!我好久能达到这种水平就好了![Quote=引用 6 楼 的回复:]
SQL code


select a.TopicBody,a.TopicImg,a.CreateON,b.UserID,b.nickname
from SNS_Topic a
inner join
(
select a.UserID,max(a.createon) as Createon,b.nickname,b.headimg from SNS_Topic a
……
[/Quote]
狼王_ 2010-08-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zc19860213 的回复:]
除了聚合函数以外的都要写在GROUP BY 里
[/Quote]

如果你少写了,SQL会给你提示错误的。
zc19860213 2010-08-28
  • 打赏
  • 举报
回复
除了聚合函数以外的都要写在GROUP BY 里
taz01 2010-08-28
  • 打赏
  • 举报
回复
三个表attention(关注),topic(文章),UserInfo

查询某一用户他关注的人,拿出他关注的人中最新发表的一条话题数据 。
有按最新关注,最近更新,昵称首字母三种排列,还要分页!
上面的代码是昵称首字母
=====
下面的就是 按最近更新的

USE [Tuan]
GO
/****** Object: StoredProcedure [dbo].[Pro_Fujet_SNS_Attention_GetList_SortByNewTopic] Script Date: 08/28/2010 12:34:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc [dbo].[Pro_Fujet_SNS_Attention_GetList_SortByNewTopic]
@userid int,
@top int,
@index int
as
begin
select a.Notes,a.AttentionUserID,d.AttentionBody ,
b.Headimg,isnull(b.nickname,Email) as NickName,isnull(b.Sex,'保密') as Sex,
isnull(c.CityName,'火星') as City,(select count(UserID) from SNS_Attention
where AttentionUserID=a.AttentionUserID) as Fans,
e.TopicBody,e.TopicImg,e.createon
from SNS_Attention a
left join userinfo b on a.AttentionUserID=b.userid
left join City c on b.CityID=c.CityID
left join SNS_AttentionGroup d on a.AttentionGroupID=d.AttentionGroupID
inner join
(
select a.Userid , a.TopicBody,a.TopicImg,a.createon
from SNS_Topic a
inner join
( --分页begin
select top(@top) * from
(
select row_number() over(order by createon desc) rowNum,* from
(
select userid,max(createon) as Createon from sns_topic where userid in
(
select attentionuserid from sns_attention where userid=@userid
)
group by userid
) as temp
) as t
where t.rowNum>(@top*(@index-1))
--分页end
) as tempTopic
on a.UserID=tempTopic.userid
and a.createon =tempTopic.createon
where a.UnShield=0
) as e
on a.AttentionUserid=e.UserID
where a.userid=@userid
order by e.createon desc
end


代码效率几何?
taz01 2010-08-28
  • 打赏
  • 举报
回复

select a.TopicBody,a.TopicImg,a.CreateON,b.UserID,b.nickname
from SNS_Topic a
inner join
(
select a.UserID,max(a.createon) as Createon,b.nickname,b.headimg from SNS_Topic a
inner join
(
select top 50 * from
(
select row_number() over(order by dbo.f_GetPy(t.nickname) desc) as Num, * from
(
select isnull(nickname,email) as nickname ,UserID,headImg from UserInfo
where UserID in
(
select attentionUserID from SNS_attention where UserID=444
)
) as t
) as temp
where num>0
) b
on a.UserID=b.UserID
where a.UnShield=0
group by a.UserID,b.Num,b.NickName,b.Headimg,b.UserID
) b
on a.UserID=b.UserID and a.CreateON=b.Createon
order by dbo.f_GetPy(b.nickname) desc

这样的写法效率如何?
黄_瓜 2010-08-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 taz01 的回复:]
但问题是
我要不只是一个UserID的数据
多个UserID 最新的文章
[/Quote]
你试试就知道了
duanzhi1984 2010-08-28
  • 打赏
  • 举报
回复
把你的需求贴出来吧。。

我写的GROUP BY 还没碰到过有限制。


taz01 2010-08-28
  • 打赏
  • 举报
回复
但问题是
我要不只是一个UserID的数据
多个UserID 最新的文章
黄_瓜 2010-08-28
  • 打赏
  • 举报
回复
看你的语句可以试试

--1
select * from 文章表 a where not exists(select 1 from 文章表 where UserID=a.UserID and CreateON>a.CreateON)
--2
select * from 文章表 a where CreateON=(select max(CreateON) from 文章表 where UserID=a.UserID )
百年树人 2010-08-28
  • 打赏
  • 举报
回复
关键要看你的需求是什么
cxmcxm 2010-08-28
  • 打赏
  • 举报
回复
根据多少个字段进行分类统计,就带多少个字段,
一般情况下是group by 的字段就是select中的非统计字段
但group by 中有的,select 中也可不列出,
但如果select 中列出的字段,group by 中一定要带上
shierqu 2010-08-28
  • 打赏
  • 举报
回复
除了使用聚合函数的列不放到group by子句中,其他在select子句中的字段都要放到group by子句中。
代码效率问题,有待研究与侦测
xiaoyi_nuo 2010-08-28
  • 打赏
  • 举报
回复
后面是没有限制滴,执行顺序就是先按找group by 的第一个进行分类,然后在第一个中再按第二个进行分类,不过一般都只有一个就好了额,并且,后面有几个,在前面的select后面就得写几列,一般都是一个就可以解决问题了~~~~
claro 2010-08-28
  • 打赏
  • 举报
回复
空心兜兜 2010-08-28
  • 打赏
  • 举报
回复
有max、count这类聚合函数的话
查询分析器会提示你,哪些字段没有被聚合函数处理也没有在group by中

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧