将两个输出记录合并为一行

时间:2013-07-22 12:27:16

标签: sql-server

我有这样的存储过程:

alter procedure [dbo].[Test]  @locid int as  begin
declare @Mns decimal , @dec decimal
select @dec= AVG( CONVERT(NUMERIC(18,2), DATEDIFF(MI,t.Paydate,t.DelDate) ))  FROM Transaction_tbl t WHERE Locid=@locid 
select HH=convert(decimal(10,1) ,@dec/60)  select @Mns=@dec%60
 select @dec=@dec/60   select mm=@Mns
  end

这是在两个记录的

中返回输出
HH
29.1

mm
6

我希望获得1行的输出。期望这样:

HH           mm
29.1          6  

我怎么能这样做?

3 个答案:

答案 0 :(得分:3)

没有任何其他变量的解决方案 -

ALTER PROCEDURE [dbo].[test] 
@locid INT
AS BEGIN

     SELECT 
            mm = [dec] % 60
          , HH = CONVERT(DECIMAL(10, 1), [dec] / 60)
     FROM (
          SELECT [dec] = AVG(CONVERT(NUMERIC(18, 2), DATEDIFF(MI, t.Paydate, t.DelDate)))
          FROM dbo.Transaction_tbl t
          WHERE Locid = @locid
     ) t

END

答案 1 :(得分:2)

也许最后将HH和mm放在一个选择中:

    alter procedure [dbo].[Test]  @locid int as  begin
    declare @Mns decimal , @dec decimal
    select @dec= AVG( CONVERT(NUMERIC(18,2), DATEDIFF(MI,t.Paydate,t.DelDate) ))  FROM Transaction_tbl t WHERE Locid=@locid;
    -- select @Mns=@dec%60;
    -- select @dec=@dec/60;   
    select HH=convert(decimal(10,1) ,@dec/60), mm=@dec%60;
    end

答案 2 :(得分:1)

试试这个:

Select
   @Mns As Mns,
   @HH As HH

您可以选择任何内容,只需用逗号分隔

即可
Select
   'John'           As MyName,
   5*20             As MyCalc,
   MONTH(GetDate()) AS ThisMonth,
   @SomeVariable    As SomeVariable

输出

MyName    MyCalc    ThisMonth    SomeVariable
-----------------------------------------------
John      100       7            0