2017-04-10 8 views
-1

SQL Server 2008にコードがあり、別の行に参加する必要があります。 SQL Server:1行に2行

この

はコード

select 
    usuario.SK_Representative, 
    sum(devo.NM_Material) as deve 
from 
    DW_DTS_Representative usuario 
inner join 
    DIS_DTS_Sales_Return_Fact devo on devo.SK_Representative = usuario.SK_Representative 
inner join 
    DW_DTS_Operation_Nature cfop on cfop.SK_Operation_Nature = devo.SK_Operation_Nature 
inner join 
    DW_DTS_Emitter cli on cli.SK_Emitter = devo.SK_Emitter 
where 
    devo.CD_Currency = '0' 
    and devo.CD_Site = '001' 
    and cli.CD_Customer_Group = '10' 
    and usuario.SK_Representative != '2' 
    and usuario.SK_Representative != '3' 
    and usuario.SK_Representative != '4' 
    and usuario.SK_Representative != '41' 
    and usuario.SK_Representative != '48' 
    and usuario.SK_Representative != '49' 
    and usuario.SK_Representative != '43' 
    and devo.DT_Day between DateAdd(yyyy, DateDiff(yyyy,0,GetDate()), 0) 
         and dateadd([month], datediff([month], '18991231', dateadd(month, -1, getdate())), '18991231') 
group by 
    usuario.SK_Representative 
ある

そのスクリプトの結果は次のとおりです。

enter image description here

しかし、私はのためのSK_Representativeに番号「33」のSK_Representative情報を集約する必要があります番号「47」。私はSK_Representativeである主キーを失うことなくこれを行う必要があります。

+0

これは現在の結果または欲しい出力ですか? –

+0

"番号33のSK_Representative情報を番号 '47'のSK_Representativeに集約する" –

+0

SK_Representative '33'の 'deve'値はSK_Representative '47'の 'deve'と合計する必要があります –

答えて

0

私はあなたの質問を正しく理解していれば、私はこれがあなたが望むものだと信じています。私はまた、usuario.SK_Representative NOT INを使用するための@ juan-carlos-oropezaの提案を考慮に入れました。

select 
    CASE 
     WHEN usuario.SK_Representative IN ('33', '47') THEN '47' 
     ELSE usuario.SK_Representative 
    END AS SK_Representative, 
    sum(devo.NM_Material) as deve 
from 
    DW_DTS_Representative usuario 
inner join 
    DIS_DTS_Sales_Return_Fact devo on devo.SK_Representative = usuario.SK_Representative 
inner join 
    DW_DTS_Operation_Nature cfop on cfop.SK_Operation_Nature = devo.SK_Operation_Nature 
inner join 
    DW_DTS_Emitter cli on cli.SK_Emitter = devo.SK_Emitter 
where 
    devo.CD_Currency = '0' 
    and devo.CD_Site = '001' 
    and cli.CD_Customer_Group = '10' 
    AND usuario.SK_Representative NOT IN ('2', '3', '4', '41', '48', '49', '43') 
    and devo.DT_Day between DateAdd(yyyy, DateDiff(yyyy,0,GetDate()), 0) 
         and dateadd([month], datediff([month], '18991231', dateadd(month, -1, getdate())), '18991231') 
group by 
    CASE 
     WHEN usuario.SK_Representative IN ('33', '47') THEN '47' 
     ELSE usuario.SK_Representative 
    END AS SK_Representative 

これはあなたが正しい経路に進むことを望みます。

ノエル

+0

それはうまくいったけど、コードの最後のケースからエイリアスを削除しました。 –

関連する問題