2017-10-26 2 views
1

これが尋ねられ、ここではすばらしい答えが得られたと確信しています。しかし、それは多くのキーワードに触れるので、私は答えを見つけることができません。
私は基本的にフォームのテーブルを置き換えたい:T-SQL:列の値を効率的に加算する方法

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。

私のpythonの背景で、私はforループアプローチでこのタスクに取り組んでいます。合計金額とnote = 'combined'を持つすべてのタイプに対して新しい行を追加し、残りの行を削除します(下記参照)。私は確かにいくつかのJOINステートメントでより効率的な方法があります。しかし、どのように見えるだろうか?

FYI、これは私が上で働いているソリューションです:私は、キーボードからこの方法をやっている

/*** 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 
+1

'param'を計算するためのルールは何ですか? 「不明瞭」とはどういう意味ですか? –

+0

コメントありがとうございます。ここで私が「不明確」と言ったことを説明する文章を追加しました。 –

答えて

1

これは単一のselectステートメントで達成できます。

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; 
:あなたが唯一の複数の行が結合された場所に適用するために、あなたの combinedフラグが必要な場合は

は、どちらかのユニークなparam値を合わせた行のcount(1)またはcount(distinct param)を組み合わせの結果を確認する別のcase式を追加します

出力:

+------+--------+-------+----------+ 
| type | amount | param | note | 
+------+--------+-------+----------+ 
| 7 |  3 | str1 | combined | 
| 42 |  36 | NULL | combined | 
| 101 |  4 | str4 | combined | 
| 128 |  9 | NULL | combined | 
+------+--------+-------+----------+ 
+0

あなたは完璧です。 –

+0

いいえ、*あなたは* perf @MichelleTurnerです – iamdave

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 
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 
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 
関連する問題