【问题标题】:SQL Server: Storing the result of a SELECT query in a float variable to be used in calculationsSQL Server:将 SELECT 查询的结果存储在要用于计算的浮点变量中
【发布时间】:2015-04-02 02:11:43
【问题描述】:

我想计算一列的中位数,将其分配给一个动态变量,然后在进一步的计算中使用它。这是我写的:

declare @sqlcmd nvarchar(max)
declare @tablename nvarchar(max)
set @tablename = 'table1'

-- calculate median
declare @median float

set @sqlcmd =
'SELECT
((SELECT MAX(field1) FROM
(SELECT TOP 50 PERCENT field1 FROM ' + @tablename + ' Where field1 is not null        
ORDER BY field1) AS BottomHalf)
+
(SELECT MIN(field1) FROM
 (SELECT TOP 50 PERCENT field1FROM ' + @tablename + ' Where field1 is not null 
ORDER BY field1 DESC) AS TopHalf)
 ) / 2'
 execute sp_executesql @sqlcmd

 set @median = ???

-- calculate value2 as value 1 minus median
'update ' + @tablename +
' set value2 = value 1 - ' + @median
execute sp_executesql @sqlcmd

我的实验结果是:

  • “将数据类型 nvarchar 转换为浮点数时出错。”
  • 或者如果我删除第二个计算,它只是将结果弹出到结果窗口。

使用 SQL Server 2012。提前致谢!

【问题讨论】:

    标签: sql sql-server median


    【解决方案1】:

    更新时只需将@median 转换为VARCHAR

    SET @sqlcmd = 'update ' + @tablename +
                  ' set value2 = value 1 - ' + CONVERT(VARCHAR(100), @median)
    EXEC(@sqlcmd )
    

    【讨论】:

    • 谢谢。现在它没有错误,并且中值已分配给@median,但它没有将 value2 设置为值 - 中值并保持为空...
    【解决方案2】:

    尽量避免使用动态 SQL 语句(连接一个 SQL 字符串然后执行它)。这可能会导致“nvarchar to float”错误。声明你的变量,然后在 select 语句中设置它们:

    DECLARE @max float
    DECLARE @min float
    
    SELECT @max = MAX(field1) FROM table1
    SELECT @min = Min(field1) FROM table1
    
    —- use @max and @min in other statements
    

    查看this question,了解如何在 SQL Server 中获取中位数。

    您可能还想在how to get a result of dynamic SQL into a variable 上查看此问题。

    【讨论】:

    • 我之所以用@tablename这个动态语句是因为我需要在几十个表上运行这个脚本,所以在一个位置输入表名会大大提高效率。在链接上,这是我用来计算中位数的第二个答案,效果很好。只是想弄清楚如何将该选择/计算的结果转换为公式。谢谢
    • 感谢您澄清 Shannon Cox;答案已为此编辑...请参阅第二个链接的问题/答案。
    【解决方案3】:

    最后,在本文的指导下,我采用了将选择查询的输出传递到使用临时表的更新语句的策略:https://smehrozalam.wordpress.com/2009/10/14/t-sql-using-result-of-a-dynamic-sql-query-in-a-variable-or-table/

    结果如下所示:

    declare @sqlcmd nvarchar(max)
    declare @tablename nvarchar(max)
    
    --insert tablename here
    set @tablename = 'table1'
    
    -- create temporary table to store median results to pass to equation
    create table temptable (calc_Result(12,10))
    
    -- calculate medians
    insert into temptable exec
    (N'SELECT
    ((SELECT MAX(field1) FROM
       (SELECT TOP 50 PERCENT field1 FROM results_' + @tablename + ' Where     field1 is not null ORDER BY field1) AS BottomHalf)
    +
    (SELECT MIN(field1) FROM
    (SELECT TOP 50 PERCENT field1 FROM results_' + @tablename + ' Where field1 is not null ORDER BY field1 DESC) AS TopHalf)
    ) / 2 AS Field1_Median)')
    
    -- calculate Field1 - Median
    set @sqlcmd = 
    'update results_' + @tablename +
    ' set Field1_Minus_Median = Field1 - (select Feild1_Median from temptable )'
    execute sp_executesql @sqlcmd
    

    感谢大家的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多