选择合并的 SQL 过程

发布于 2024-11-08 16:21:51 字数 527 浏览 6 评论 0原文

我的程序是:

create procedure "news"
as
select newsdate,COUNT(B.id) as total from news B
where B.newsyear < GETDATE()
Group by B.newsdate

select newsdate,COUNT(B.id) as total from news B 
where B.status='WAITING' and B.cancel='1'
Group by B.newsdate

结果:

newsdate   total
2011       4
2010       8

newsdate   total
2011       2
2010       3

如何合并年份总计以获得此结果集:

newsdate   total
2011       6       {4 + 2}
2010       11      {8 + 3}

My procedure is:

create procedure "news"
as
select newsdate,COUNT(B.id) as total from news B
where B.newsyear < GETDATE()
Group by B.newsdate

select newsdate,COUNT(B.id) as total from news B 
where B.status='WAITING' and B.cancel='1'
Group by B.newsdate

Results:

newsdate   total
2011       4
2010       8

newsdate   total
2011       2
2010       3

How can I merge year totals to obtain this result set:

newsdate   total
2011       6       {4 + 2}
2010       11      {8 + 3}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沧桑㈠ 2024-11-15 16:21:51

试试这个:

select newsdate,COUNT(B.id) as total 
from news B 
where ( B.newsyear < GETDATE() )
or ( B.status='WAITING' and B.cancel='1' )
Group by B.newsdate

Try this:

select newsdate,COUNT(B.id) as total 
from news B 
where ( B.newsyear < GETDATE() )
or ( B.status='WAITING' and B.cancel='1' )
Group by B.newsdate
深海少女心 2024-11-15 16:21:51

使用简单的 or 语句(如果确实是同一个表):

select newsdate,COUNT(B.id) as total
from news B
where B.newsyear < GETDATE()
   or B.status='WAITING' and B.cancel='1'
Group by B.newsdate

或使用 union all + sum 聚合(如果是不同的表):

select newsdate, sum(total) as total from (
  select newsdate,COUNT(B.id) as total from news B where B.newsyear < GETDATE()
  Group by B.newsdate
  union all
  select newsdate,COUNT(B.id) as total from news B where B.status='WAITING' and B.cancel='1'
  Group by B.newsdate
  ) as rows
group by newsdate

using a simple or statement (if it's indeed the same table):

select newsdate,COUNT(B.id) as total
from news B
where B.newsyear < GETDATE()
   or B.status='WAITING' and B.cancel='1'
Group by B.newsdate

or using union all + a sum aggregate (if it's different tables):

select newsdate, sum(total) as total from (
  select newsdate,COUNT(B.id) as total from news B where B.newsyear < GETDATE()
  Group by B.newsdate
  union all
  select newsdate,COUNT(B.id) as total from news B where B.status='WAITING' and B.cancel='1'
  Group by B.newsdate
  ) as rows
group by newsdate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文