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}
4

2 に答える 2

1

これを試して:

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
于 2011-05-20T12:59:57.147 に答える
0

単純なステートメントまたはステートメントを使用する(実際に同じテーブルである場合):

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 +合計集計を使用します(テーブルが異なる場合):

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
于 2011-05-20T13:01:18.637 に答える