SQL Server将单行拆分为两行

时间:2015-02-11 07:43:40

标签: sql sql-server

我有以下查询,它将返回单行中的值列表,如下所示:

  SELECT r_squared = CASE WHEN SS_tot = 0 THEN 1.0 ELSE 1.0 - ( SS_err / SS_tot ) END,
            Std_Dev,
            Alpha,
            SS_tot,
            SS_err,
            Beta1,
            Beta2,
            StdErrVar1,
            StdErrVar2,
            CASE WHEN @Const = 0 THEN NULL ELSE Beta1 + (@Const * StdErrVar1) END UpperLimit1,
            CASE WHEN @Const = 0 THEN NULL ELSE Beta1 - (@Const * StdErrVar1) END LowerLimit1,
            CASE WHEN @Const = 0 THEN NULL ELSE Beta2 + (@Const * StdErrVar2) END UpperLimit2,
            CASE WHEN @Const = 0 THEN NULL ELSE Beta2 - (@Const * StdErrVar2) END LowerLimit2                        
     FROM AlphaCalc
     CROSS JOIN BetaCalc
     CROSS JOIN SSCalc 

r_squared           Std_Dev             Alpha               SS_tot              SS_err               Beta1                  Beta2               StdErrVar1          StdErrVar2          UpperLimit1         LowerLimit1          UpperLimit2       LowerLimit2
+----------------------------------------------------------------------------------------------------------------------------------------+
0.931325401666624   1.08724114970357    -11.8437899314731   154.916666666667    10.6388398584788    -0.00448725387963724    1.00559107829696    0.00927464483890608 0.0909196724390537  0.0161779426244307  -0.0251524503837052 1.20817273286953    0.803009423724394

但我需要输出如下:

Variable    rsquared    StErr   StDev   Coefficients    Standard Error  Lower 95%   Upper 95%
+-----------------------------------------------------------------------------------------+
Beta1   0.931325402 10.63883986 154.9166667 -0.004487254    0.009285177 -0.025491783    0.016517276
Beta2   0.931325402 10.63883986 154.9166667 1.005591078 0.091022919 0.79968293  1.211499227

1 个答案:

答案 0 :(得分:1)

根据我对您的问题的理解我认为这就是您所需要的:

( select 'Beta1' as Variable, r_squared as rsquared, SS_err as StErr, SS_tot as 'StDev', 
        Beta1 as Coefficients, StdErrVar1 as 'Standard Error',
        LowerLimit1 as 'Lower 95%',UpperLimit1 as 'Upper 95%'
from    
    (SELECT r_squared = CASE WHEN SS_tot = 0 THEN 1.0 ELSE 1.0 - ( SS_err / SS_tot ) END,
     Std_Dev, Alpha, SS_tot, SS_err, Beta1, Beta2, StdErrVar1, StdErrVar2,
     CASE WHEN @Const = 0 THEN NULL ELSE Beta1 + (@Const * StdErrVar1) END UpperLimit1,
     CASE WHEN @Const = 0 THEN NULL ELSE Beta1 - (@Const * StdErrVar1) END LowerLimit1,
     CASE WHEN @Const = 0 THEN NULL ELSE Beta2 + (@Const * StdErrVar2) END UpperLimit2,
     CASE WHEN @Const = 0 THEN NULL ELSE Beta2 - (@Const * StdErrVar2) END LowerLimit2  
     FROM AlphaCalc
     CROSS JOIN BetaCalc
     CROSS JOIN SSCalc) as dat1
)
union all
( select 'Beta2' as Variable, r_squared as rsquared, SS_err as StErr, SS_tot as 'StDev', 
        Beta2 as Coefficients, StdErrVar2 as 'Standard Error',
        LowerLimit2 as 'Lower 95%',UpperLimit2 as 'Upper 95%'
from    
    (SELECT r_squared = CASE WHEN SS_tot = 0 THEN 1.0 ELSE 1.0 - ( SS_err / SS_tot ) END,
     Std_Dev, Alpha, SS_tot, SS_err, Beta1, Beta2, StdErrVar1, StdErrVar2,
     CASE WHEN @Const = 0 THEN NULL ELSE Beta1 + (@Const * StdErrVar1) END UpperLimit1,
     CASE WHEN @Const = 0 THEN NULL ELSE Beta1 - (@Const * StdErrVar1) END LowerLimit1,
     CASE WHEN @Const = 0 THEN NULL ELSE Beta2 + (@Const * StdErrVar2) END UpperLimit2,
     CASE WHEN @Const = 0 THEN NULL ELSE Beta2 - (@Const * StdErrVar2) END LowerLimit2  
     FROM AlphaCalc
     CROSS JOIN BetaCalc
     CROSS JOIN SSCalc) as dat2
)