【问题标题】:Get a grand total from several counts从多个计数中获得总计
【发布时间】:2012-08-30 15:20:26
【问题描述】:

我只需要在下面添加每个计数的总计,这对我不起作用,我得到“关键字'SELECT'附近的语法不正确。”:

DECLARE @total int

SET @total = Count(some select statement)

@total = @total + Count(some select statement)
@total = @total + Count(some select statement)
@total = @total + Count(some select statement)
.. and so on

-- now return the grand total
select @total

或者,如果您认为有更简单或更有效的方法来做到这一点,我也全神贯注。

【问题讨论】:

  • 看起来你的想法是正确的,你在中间的三行缺少一个 SET。
  • @DavidSteele 您不能将“set”与“count”结合使用,如我的回答所示
  • 你可以这样做.... SET @total += (SELECT COUNT(*) from sometable)

标签: tsql


【解决方案1】:
declare @total int = 0

select @total += count(*) from (select 1 col1) a
select @total += count(*) from (select 1 col1) a
select @total += count(*) from (select 1 col1) a

select @total

【讨论】:

    猜你喜欢
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 2017-07-28
    • 2020-02-10
    • 1970-01-01
    • 2012-02-17
    相关资源
    最近更新 更多