T-SQL:添加列值的有效方法

时间:2017-10-26 10:51:00

标签: sql tsql sum ssms ssms-2016

现在我确信这已被问到并在这里得到了很好的回答。但是,我无法找到答案,因为它涉及许多关键字 我基本上想要替换表格的表格:

Type   amount   param   note
7      2        str1    NULL
42     12       str2    NULL
128    7        str3    samplenote
42     12       NULL    NULL
101    4        str4    NULL
42     12       NULL    NULL
7      1        str1    samplenote
128    2        str5    NULL

表格如下:

Type   amount   param   note
7      3        str1    combined
42     36       NULL    combined
128    9        NULL    combined
101    4        str4    combined

总之,我试图根据其类型总结amount参数,同时为所有“不清楚”字段声明param = NULL。 (当组合类型的参数值具有多个不同的内容时,param应为NULL;否则,param应具有原始内容。)

在我的python背景下,我用for循环方法解决了这个任务,迭代了类型,为每个类型添加了一个新的行,总计金额和note ='combined',然后删除剩余的行(见下文) )。我肯定有一些更有效的方法可以使用一些JOIN语句。但那会是怎么样的?

仅供参考,这是我正在处理的解决方案(尚未运行!):

/*** dbo.sourcetable holds all possible Type values ***/
CREATE PROCEDURE [sumup]

AS
BEGIN

DECLARE @i int = (SELECT TOP (1) Type FROM [dbo].[sourcetable] ORDER BY Type)
DECLARE @MaxType int = (SELECT TOP (1) Type FROM [dbo].[sourcetable] ORDER BY Type DESC)

DECLARE @sum int

BEGIN TRY
    WHILE @i <= @MaxType
    BEGIN
        IF EXISTS (SELECT * FROM [dbo].[worktable] WHERE Type = @i)
        BEGIN
            SET @sum = (SELECT SUM(amount) FROM [dbo].[worktable] WHERE Type = @i)

            BEGIN
                WITH cte AS (SELECT * FROM [dbo].[worktable] WHERE Type = @i)
                INSERT INTO [dbo].[worktable]
                    ([Type]
                    ,[amount]
                    ,[param]
                    ,[note]
                SELECT
                     cte.Type
                    ,@sum
                    ,cte.param
                    ,'combined'
                FROM cte
            END

            DELETE FROM [dbo].[worktable] WHERE Type = @i AND ISNULL([note],'') <> 'combined'

        END
        SET @i = @i + 1
    END

END TRY
BEGIN CATCH 

    -- some errorlogging code

END CATCH

END

GO

4 个答案:

答案 0 :(得分:1)

这可以通过一个select语句来实现。

如果您要求combined标志仅适用于已合并多行的位置,请添加另一个case表达式,检查count(1)的结果是否合并了行数或{ {1}}结合了唯一的count(distinct param)值:

param

输出:

declare @t as table(type int, amount int, param varchar(15), note varchar(15));
insert into @t values (7,2,'str1',NULL),(42,12,'str2',NULL),(128,7,'str3','samplenote'),(42,12,NULL,NULL),(101,4,'str4',NULL),(42,12,NULL,NULL),(7,1,'str1','samplenote'),(128,2,'str5',NULL);

select type
      ,sum(amount) as amount
      ,case when count(distinct isnull(param,'')) = 1
            then max(param)
            else null
            end as param
      ,'combined' as note
from @t
group by type
order by type;

答案 1 :(得分:0)

我是从键盘做这种方式,但这可能有用或接近你想要的

Select type , amount , iif( dc=1,p,null) param, 'combined' note
from
(
   Select type, sum(amount) amount, 
          count(distinct Param) dc,max(Param) p
   From ....
   Group by type
) x

答案 2 :(得分:0)

这是一个可能的解决方案:

declare @tbl as table (
    type int
    ,amount int
    ,param varchar(15)
    ,note varchar(15)
)

insert into @tbl values (7,2,'str1',NULL)
insert into @tbl values (42,12,'str2',NULL)
insert into @tbl values (128,7,'str3','samplenote')
insert into @tbl values (42,12,NULL,NULL)
insert into @tbl values (101,4,'str4',NULL)
insert into @tbl values (42,12,NULL,NULL)
insert into @tbl values (7,1,'str1','samplenote')
insert into @tbl values (128,2,'str5',NULL)

;WITH CTE AS (
    SELECT
        type
        ,SUM(AMOUNT) AS amount
        ,COUNT(DISTINCT ISNULL(param, 'dummy value')) AS ParamNo
        ,MAX(Param) AS Param
    FROM @tbl
    GROUP BY type
) SELECT
    type
    ,amount
    ,CASE WHEN ParamNo = 1 THEN Param ELSE NULL END AS Param
    ,'combined' AS note
FROM CTE

答案 3 :(得分:0)

这应该有效:

flask_limiter