【问题标题】:Compound Operators with T-SQL使用 T-SQL 的复合运算符
【发布时间】:2014-05-22 11:53:57
【问题描述】:
USE Saleslogix
DECLARE @AssumedGrowth int
SET @AssumedGrowth = 28

SELECT
   account,
   employees as NumberIn2013,
   @AssumedGrowth += employees as NumberIn2014
FROM sysdba.account
WHERE employees <> 'NULL'
  and account like 'Shaw%'

它告诉我+= 无效并且仅适用于+。有人可以帮助我让这个例子作为复合运算符工作吗?我不知道它是否有太大的不同,但我使用的是 2005 Management Studio。

如果它不是一个巨大的痛苦,添加同样的例子,@AssumedGrowth 是一个百分比?

【问题讨论】:

  • @BAdmin 编辑时,请注意内联代码跨度 (like this) 仅用于句子中的部分代码(例如变量名),不用于突出显示或其他强调。例如,@AssumedGrowth 可以,但2005 Management Studio 不行。谢谢!
  • 你真的是指&lt;&gt; 'NULL'吗?你的意思可能是IS NOT NULL
  • 所以澄清一下 - 您希望从 28 开始的每一行中都有一个运行总计,并且之后 @AssumedGrowth 变量保持设置为总计?

标签: sql sql-server tsql operators


【解决方案1】:

你想要做的是:

SELECT account, employees as NumberIn2013,
       (@AssumedGrowth = @AssumedGroup + employees) as NumberIn2014
FROM sysdba.account
WHERE employees <> 'NULL' and account like 'Shaw%';

但是,我认为它不会起作用。我建议使用内置功能,特别是row_number()

SELECT account, employees as NumberIn2013,
       employees * pow(1 + @AssumedGrowth/100.0, row_number() over (order by <field>) - 1)
FROM sysdba.account
WHERE employees <> 'NULL' and account like 'Shaw%';

请注意,您需要指定结果的顺序。据推测,存在某种具有适当顺序的iddatetime 列。表格代表无序集合,因此没有“第一”行。

【讨论】:

    猜你喜欢
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 2012-02-16
    • 2010-12-23
    • 2011-04-23
    • 1970-01-01
    相关资源
    最近更新 更多