如何跳过特定行并在表格末尾添加?

时间:2013-09-10 15:52:51

标签: sql-server tsql stored-procedures sql-server-2008-r2

CREATE PROCEDURE Dataloading
AS
BEGIN
    truncate table TABLE1

    insert TABLE1 (
        [Name],
        [Share]
    )
    select 
        [Name], 
        sum(Share)
    from TABLE2
    group by [Share]

END

输出:

texas     3000 
dallas    5000
ca        5000
sfo       7000
newyork   8000
G total  28000
alaska    4000
Total    24000

但是我需要最后进入阿拉斯加,然后在beofre之前必须为空?

注意:我要留空,因为我将此输出发送到XL表格,我正在使用一些计算。但我手动做的是对SP进行任何修改。

帮帮我们!!!“

1 个答案:

答案 0 :(得分:3)

试试这个

 select 
        [Name], 
        sum(Share)
    from TABLE2
    group by [Share]

order by case when name= 'alaska' then 1 else 0 end

修改

insert...

select [name],summ from (
 select 
        [Name], 
        cast (sum(Share) as nvarchar(100)) as summ
    from TABLE2
    group by [Share]

union all select '',''
) a 
  order by case  name when  '' then 2  
 when  'alaska' then 3    
else 1 end