2017-01-18 9 views
1

私はこのコードを書いて、個々の学生の料金支払い、保留などを表示しましたが、実際には学生全体のクラスになるはずでした。私はたくさん試しましたが、クラス全体ではできません。これを行う方法 ? @studentIDを無視して、@ClassIDだけにしたい。1人ではなく、複数人で出力するには?

ALTER PROCEDURE [Accounts].[GetUserPaymentsPartialRecord] 
    @StudentId INT= 14, 
    @classID int=1 
AS 
BEGIN 



    declare @accID int 
    set @accID= (select st.Account_ID from School.StudentInformation ST where st.StudentID = @StudentId and st.ClassToWhichAdmitted_ID= @classID) 

    declare @className varchar(50) 
    Set @className= (Select c.ClassName from School.Classes c 
        Inner Join School.StudentInformation si 
        ON si.ClassToWhichAdmitted_ID= c.ClassID where si.StudentID= @StudentId) 

    declare @transactions varchar(max) 
    Set @transactions=(select convert(varchar,Convert(decimal(10,0),t.Credit))+', ' AS 'data()' from Accounts.Transactions T 
    where t.Account_ID = (@accID) 
    and 
    t.Credit>0 
    AND 
    t.IsReversed=0 
    AND 
    t.InvoiceNumber in(Select inv.InvoiceNumber from Accounts.Invoices inv where inv.Student_ID= @StudentId) 
    AND 
    Particulars Like '%Received%' 
    FOR XML PATH('')) 

    declare @PayableAmount money 
    set @PayableAmount = (select sum(i.Amount+ i.Fine) from Accounts.Invoices I where I.Student_ID= @StudentId and i.Class_ID = @classID)    


    declare @TotalAmountPaid money 
    Set @TotalAmountPaid= (Select sum(t.Credit) from Accounts.Transactions T 
    where t.Account_ID = (@accID) 
    and 
    t.Credit>0 
    AND 
    t.IsReversed=0 
    AND 
    t.InvoiceNumber in(Select inv.InvoiceNumber from Accounts.Invoices inv where inv.Student_ID= @StudentId) 
    AND 
    t.Particulars Like '%Received%' 
    group by t.Account_ID) 


    declare @PendingAmount money 
    Set @PendingAmount= (@PayableAmount - @TotalAmountPaid) 

    SELECT st.StudentName, st.StudentRegisterationNo,@className as ClassName, 
      @transactions as Paid, Convert(decimal(10,0),@TotalAmountPaid) as TotalPaid, 
      @PayableAmount, @PendingAmount as 'PendingAmount' FROM School.StudentInformation ST 
    where st.StudentID= @StudentId 



END 
+0

[Minimal、Complete、Verifiable example](http://stackoverflow.com/help/mcve)も投稿してください。[How to ask](http://stackoverflow.com/help/how-to-ask)もチェックしてください。 – wdosanjos

答えて

0

あなたが学生とクラスレベルである、上記の手順を移動しようとすることができ、単にクラスレベルで照会することができる表形式のセットとして

参照サンプルPROC以下:

CREATE PROCEDURE [Accounts].[GetUserPaymentsPartialRecordByClass] 
    @classID int=1 
AS 
BEGIN 

create table #temp (sID int,cID int, accID int, className varchar(50),transactions varchar(max),PayableAmount money,TotalAmountPaid money,PendingAmount money) 

INSERT into #temp 
    (sID,cID,accID,className) 
select 
    distinct st.StudentID as sID, 
    st.ClassToWhichAdmitted_ID as cID , 
    st.Account_ID as accID, 
    c.ClassName as className 
from School.StudentInformation st 
    left join School.Classes c 
     on st.ClassToWhichAdmitted_ID= c.ClassID 
where st.ClassToWhichAdmitted_ID= @classID 

update temp 
set temp.transactions=transactions 
from #temp temp outer apply(
select convert(varchar,Convert(decimal(10,0),t.Credit))+', ' AS 'data()' 
from Accounts.Transactions T 
on T.Account_ID=temp.accID 
and T.Credit>0 and T.IsReversed=0 
and Particulars Like '%Received%' 
inner join Accounts.Invoices inv 
on inv.Student_ID=temp.sID and inv.InvoiceNumber=T.InvoiceNumber 
FOR XML PATH('') 
) s(transactions) 




update T 
set PayableAmount = sum(i.Amount+ i.Fine) 
from #temp T 
INNER JOIN Accounts.Invoices I ON I.Student_ID= T.sId and i.Class_ID = T.cID    

update temp 
set TotalAmountPaid =sum(t.Credit) from 
#temp temp Inner join 
Accounts.Transactions T 
on t.Account_ID = temp.accID 
and t.Credit>0 AND t.IsReversed=0 and 
t.Particulars Like '%Received%' 
inner join Accounts.Invoices inv 
on inv.Student_ID=temp.sID and inv.InvoiceNumber=T.InvoiceNumber 
group by T.Account_ID 


update temp 
sett PendingAmount= (PayableAmount - TotalAmountPaid) 

SELECT st.StudentName, st.StudentRegisterationNo,ClassName, 
    transactions as Paid, Convert(decimal(10,0),TotalAmountPaid) as TotalPaid, 
    PayableAmount, PendingAmount as 'PendingAmount' FROM #temp T inner join School.StudentInformation ST 
    On st.StudentID= T.sId 



END 
+0

ありがとう、しかし2つのエラー。 – Covert

+0

Msg 156、レベル15、状態1、プロシージャGetUserPaymentsPartialRecordByClass、行25 キーワード 'on'の近くに構文が正しくありません。 メッセージ156、レベル15、状態1、プロシージャGetUserPaymentsPartialRecordByClass、行50 'group'キーワードの構文が正しくありません。 – Covert

+0

ONとグループのエラーを取得する – Covert

関連する問題