如何添加所有行并减去表中的一个特定行并插入到新表中?

时间:2013-09-10 17:36:31

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

我遇到了一个疯狂的问题。在XL表中手动执行是否有任何机会在存储过程中进行。

我为内嵌数据创建了一个存储过程

CREATE PROCEDURE Dataloading
AS
BEGIN
    truncate table TABLE1

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

END


Output is 

texas     3000 

dallas    5000

ca        5000

sfo       7000

newyork   8000

alaska    4000

我需要输入到TABLE1的输入作为添加除阿拉斯加以外的所有共享并减去alska并在下面的行中打印。

I want output look like :-
texas     3000 

dallas    5000

ca        5000

sfo       7000

newyork   8000

G total  28000

alaska    4000

Total    24000

你能看到这个吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试连续插入:

CREATE PROCEDURE Dataloading
AS
BEGIN
truncate table TABLE1

insert TABLE1 (
    [Name],
    [Share]
)
select 
    [Name], 
    sum(Share)
from TABLE2
 where [Name] not like '%alaska%'
group by [Share]

insert TABLE1 (
    [Name],
    [Share]
)
select 'G total', sum([Share]) from TABLE1 

insert TABLE1 (
    [Name],
    [Share]
)
select 
    [Name], 
    sum(Share)
from TABLE2
 where [Name] like '%alaska%'
group by [Share]

insert TABLE1 (
    [Name],
    [Share]
)
select 'Total', sum([Share]) from TABLE1 

END

希望它有所帮助:)

你也可以添加一些creazy排序列。可能你会需要它