能否同时查询两个分组条件

张海霖 2007-03-01 04:12:40
--分栏目统计今日更新软件数
select classid,今日更新=count(1) from article where datediff(day,dateandtime,getdate())=1 and classid in (select classid from f_news_getchildclass(208)) group by classid

classid 今日更新
209 186
210 331
211 18
214 10
215 118
217 48
218 269
219 27
220 190
我还想增加一列叫做 [软件总数],就是那个分类的软件总数,我不知道怎么写,既能按当日这个条件汇总出今日更新,又能不要这个条件汇总数软件总数
classid 今日更新 [软件总数]
209 186
210 331
211 18
214 10
215 118
217 48
218 269
219 27
220 190
...全文
343 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
张海霖 2007-03-01
  • 打赏
  • 举报
回复
嗯,没有问题。很正确,你太有才了!
paoluo 2007-03-01
  • 打赏
  • 举报
回复
用我寫的第二個吧,那個應該沒有問題吧。
张海霖 2007-03-01
  • 打赏
  • 举报
回复
我修改了一下,可以了
select
classid,
软件总数=count(1),
今日更新 = (Select Count(*) From article Where classid = A.classid and datediff(day,dateandtime,getdate())=1)
from article A
where
classid in (select classid from f_news_getchildclass(208))
group by classid
张海霖 2007-03-01
  • 打赏
  • 举报
回复
有点问题,总的是12条,而今天只更新了9个分类,所以只查询出9条来
实际上应该查询出12条,有3条今日更新为0
paoluo 2007-03-01
  • 打赏
  • 举报
回复
這麼改下

Select
classid,
今日更新 = SUM(Case When datediff(day,dateandtime,getdate())=1 Then 1 Else 0 End),
软件总数 = Count(*)
From article A
Where classid in (select classid from f_news_getchildclass(208))
Group by classid
paoluo 2007-03-01
  • 打赏
  • 举报
回复
加個子查詢試下

select
classid,
今日更新=count(1),
软件总数 = (Select Count(*) From article Where classid = A.classid)
from article A
where datediff(day,dateandtime,getdate())=1
and classid in (select classid from f_news_getchildclass(208))
group by classid
张海霖 2007-03-01
  • 打赏
  • 举报
回复
同时看看这个帖。
http://community.csdn.net/Expert/topic/5366/5366154.xml?temp=.8335382
搜集另类的sql语句集合

34,590

社区成员

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

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