1

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

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

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

在我的 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

4 回答 4

1

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

如果您要求您的combined标志仅适用于组合了多行的情况,请添加另一个case表达式,检查 a 的结果,count(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;

输出:

+------+--------+-------+----------+
| type | amount | param |   note   |
+------+--------+-------+----------+
|    7 |      3 | str1  | combined |
|   42 |     36 | NULL  | combined |
|  101 |      4 | str4  | combined |
|  128 |      9 | NULL  | combined |
+------+--------+-------+----------+
于 2017-10-26T11:37:29.713 回答
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
于 2017-10-26T11:25:45.073 回答
0

这应该有效:

Select Type, sum(amount) as amount, count(distinct param)
, case when count(distinct param) = 1  then max(param) end as param,  
'Combined' as note
From 
mytable
Group By Type
于 2017-10-26T11:35:35.573 回答
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
于 2017-10-26T10:57:51.600 回答