2011-08-02 12 views
1

これはおそらく以前に要求されていますが、解決策を見つけることができません。私は別の列に基づいて値を合計する必要があるストアドプロシージャを記述しています。 AMOUNTは、SchoolIDが等しい場合には合計する必要があります。同じ列を持つすべての行の合計を検索するSqlクエリ

ID  ReqID SchoolID NAME   AMOUNT 
141  30  0104  LaborHilto 148.72 
142  30  0104  LabClaxton 242.25 
143  30  0104  LabWilliam 285.00 
144  30  0196  LabWilliam 249.00 
151  30  0196  Kelly  265.72 
163  30  2056  Kelley  968.76 

これまでの質問です。

select distinct sum(i.amount) 
from invoice i inner join vSchool v on v.SchoolID = i.SchoolID and v.SystemID = @SystemID 
inner join request R on R.requestid = i.requestid 
inner join grantsystem G on G.grantsystemID = R.grantsystemID 
inner join grants GR on GR.grantsid = G.grantsID 
where i.SchoolID = v.SchoolID           
and i.ReimbursementTypeID = '29'    
and month(R.FundMonth)[email protected] 
and R.requesttypeID = @ReqStatus          
and GR.grantsid = '5' or GR.grantsid = '7' 

基本的に何が起こるか、それは私が

TOTAL 
675.97 
514.72 
968.76 

で必要なものを一緒

TOTAL 
2159.45  

すべての金額を追加することは、これを行う方法はありますか?

+2

あなたはグループ化を試みましたか? –

+0

どのバージョンのSQL Serverですか? – Lucero

+0

グループを使うと、あなたの所在地に基づいてi.amountを集計することができます。グループによってi.amount –

答えて

1

グループを識別欄で追加するだけです。

0
Select amount as [Total] 
From [whatever tables etc..] 
Group by ReqID ? 

正確に何を集計していますか?これはSchoolID

+0

'GROUP BY SchoolID'、おそらく。 –

1

によってグループがちょうどGROUP BY句を使用するものです合計)を選択し、その列にgroup byを追加します。

select SchoolID, sum(i.amount) 
from invoice i inner join vSchool v on v.SchoolID = i.SchoolID and v.SystemID = @SystemID 
inner join request R on R.requestid = i.requestid 
inner join grantsystem G on G.grantsystemID = R.grantsystemID 
inner join grants GR on GR.grantsid = G.grantsID 
where i.SchoolID = v.SchoolID           
and i.ReimbursementTypeID = '29'    
and month(R.FundMonth)[email protected] 
and R.requesttypeID = @ReqStatus          
and GR.grantsid = '5' or GR.grantsid = '7' 
group by SchoolID 
4

の値ごとに別々の合計がSchoolID(または何列かに基づいて、必要な追加行います

<your query> 
GROUP BY SchoolID 

:それはあなたが

関連する問題