是否可以跨列中的不同值迭代计算?

时间:2015-06-19 21:03:29

标签: sql-server sql-server-2008

我想迭代一个计算,该计算会计算符合特定条件的人数,并根据不同地区的计数来计算百分比。

我的代码:

USE Database1;
GO


declare @ShouldRegister as float
declare @Registered as float


SET @ShouldRegister = (SELECT COUNT(*) FROM dbo.TABLE 
                WHERE field1 in.. 
                AND field2 in..  
                AND field3 in..
                ...
                )

SET @Registered = (SELECT COUNT(*) FROM dbo.TABLE
                WHERE field1 in..
                AND field2 in..
                AND field3 in..
                ...
                )

SELECT  
     @ShouldRegister as ShouldRegister 
    , @Registered as Registered
    , cast((@Registered/NULLIF(@ShouldRegister, 0))*100 as decimal(12,8)) as Percentmet
    , CAST(100*2.33*(SQRT(@Registered/NULLIF(@ShouldRegister, 0) * (1-(@Registered/NULLIF(@ShouldRegister, 0)))/NULLIF(@ShouldRegister, 0))) as decimal(12,8)) + cast((@Registered/NULLIF(@ShouldRegister, 0))*100 as decimal(12,8)) as AdjPercentmet

代码返回如下内容:

ShouldRegister  Registered  Percentmet      adjpercentmet
223587          565         0.25269805      0.27743717

每个人都有一个区域分配在"区域"柱。上面的代码计算所有地区。我希望看到的是:

ShouldRegister  Registered  Percentmet      adjpercentmet   Region
223             50          0.12345678      0.12345678      Region1
456             100         0.12345678      0.12345678      Region2
789             456         0.12345678      0.12345678      Region3

我的大脑想做:"对于区域中的区域,做(代码)",但我不认为SQL是这样工作的。

1 个答案:

答案 0 :(得分:1)

尝试这种方式: -

<?php 
    echo $_POST['n1'];
?>