2017-03-10 12 views
0

ManagerのB_Allocation値が異なる場合、別の値を合計する必要があります。たとえばManager 5の場合、10800、661782.60、2141987.20、および2163743.43を合計できるようにしたいと考えています。彼らは(Original Image)以下で表示されるそのB_Allocation明瞭ではありません6、8、14のような管理職のために、私は値が欲しいSQL Server 2012でグループ内の値が異なる場合の合計値

select 
      BU.BuCode 
     , PE.ManagerID 
     , BUA.AllocatedAmount as B_Allocation 
     , PE.AllocatedAmount 
    from CFMS_ProjectEntry PE 
    join CFMS_BUAllocation BUA on BUA.BUDGETENTRYID = PE.BUDGETENTRYID 
    join CFMS_BU   BU on BU.BUID   = BUA.BUID 
    join CFMS_User   US on US.UserID   = PE.ManagerID 
    where BUCODE = 42406016 
    group by BU.BUCode 
      , ManagerID 
      , BUA.AllocatedAmount 
      , PE.AllocatedAmount 

私は(Original Image)欲しい:

+ -------- + --------- + ------------ + --------------- + 
| BUCode | ManagerID | B_Allocation | AllocatedAmount | 
+ -------- + --------- + ------------ + --------------- + 
| 42406016 | 5   | 10800.00  | 1293916.30  | 
| 42406016 | 5   | 661782.60 | 1723699.70  | 
| 42406016 | 5   | 2141987.20 | 4628466.40  | 
| 42406016 | 5   | 2163743.43 | 109956.12  | 
| 42406016 | 5   | 2163743.43 | 407655.00  | 
| 42406016 | 5   | 2163743.43 | 499902.00  | 
| 42406016 | 5   | 2163743.43 | 523275.55  | 
| 42406016 | 5   | 2163743.43 | 622954.76  | 
| 42406016 | 6   | 4747880.45 | 1500000.00  | 
| 42406016 | 6   | 4747880.45 | 2000000.00  | 
| 42406016 | 8   | 726893.75 | 14142.71  | 
| 42406016 | 8   | 726893.75 | 22143.56  | 
| 42406016 | 8   | 726893.75 | 106448.50  | 
| 42406016 | 8   | 726893.75 | 265501.45  | 
| 42406016 | 8   | 726893.75 | 318657.53  | 
| 42406016 | 14  | 4747880.45 | 747880.45  | 
+ -------- + --------- + ------------ + --------------- + 
+0

これらの権利が必要ないので、最初に複製を削除する必要がありますか?あなたのクエリに別名を追加することで、これを動作させるにはAllocatedAmountを削除する必要があります。その後、B_AllocationにSum()を使用できます。また、ManagerIDでグループ分けして作業する必要があります。それを試して、あなたのSQLを質問に書いてください。 B_Allocation – Sourcery

+2

@RPGとしてBU.BUCode、PE.ManagerID、sum(BUA.AllocatedAmount)を選択してください。関連するすべてのテーブルのテーブル構造、値のINSERTスクリプト、試したクエリ、取得した結果を入力してください。あなたは最終結果として何を期待していますか?常にテキスト形式で提供してください。人々がテキストから価値を得て、できるだけ早くあなたを助けるのは簡単でしょう。 – Venu

答えて

0

はこれを試してみてください。

Declare @T1 Table(BUCode INT, ManagerID INT, B_Allocation Float,  AllocatedAmount Float); 
Declare @T2 Table(BUCode INT, ManagerID INT, B_Allocation Float,  AllocatedAmount Float); 
with cte as (**Add your Select statement here**) 

Insert Into @T1 
Select * from cte 

Insert Into @T2 
Select BUCode, ManagerID, sum(B_Allocation) from @T1 
Group By BUCode, ManagerID 

Select t2.BUCode, t2.ManagerID, t2.B_Allocation, t1.AllocatedAmount from @T1 as t1 
left join @T2 as t2 on t1.BUCode = t2.BUCode AND t1.ManagerID = t2.ManagerID 
関連する問題