thayduck
asked on
Create Totals By Unique Invoice Number
SQL 2008 / SSRS 2008
INV Billed Paid
123 20.00 10.00
123 20.00 10.00
124 15.00 5.00
125 12.00 12.00
I want to create 1 SQL record that I can read in the main report query that gives me the total invoice count, total billed and total paid for all unique invoices. So this one record should show:
3 47.00 27.00
Or, better yet, how would I incorporate that into the main report query.
INV Billed Paid
123 20.00 10.00
123 20.00 10.00
124 15.00 5.00
125 12.00 12.00
I want to create 1 SQL record that I can read in the main report query that gives me the total invoice count, total billed and total paid for all unique invoices. So this one record should show:
3 47.00 27.00
Or, better yet, how would I incorporate that into the main report query.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Please try ..full tested solution-
Table Creation and data insertion
SOLUTION
Table Creation and data insertion
CREATE TABLE yourtableName
(
INV INT
,Billed DECIMAL(10,2)
,Paid DECIMAL(10,2)
)
GO
INSERT INTO yourtableName VALUES
(123,20.00,10.00 ),
(123,20.00,10.00 ),
(124,15.00,5.00 ),
(125,12.00,12.00 )
GO
SOLUTION
SELECT COUNT(*) InvoiceCount,SUM(b) Billed , SUM(p) Paid
from (
SELECT inv, Billed b, (Paid) p
FROM yourtableName
group by inv,Billed,Paid)l
OUTPUT[code]
/*------------------------
SELECT COUNT(*) InvoiceCount,SUM(b) Billed , SUM(p) Paid
from (
SELECT inv, Billed b, (Paid) p
FROM yourtableName
group by inv,Billed,Paid)l
------------------------*/
InvoiceCount Billed Paid
------------ --------------------------------------- ---------------------------------------
3 47.00 27.00
(1 row(s) affected)
ASKER
This is report query:
SELECT Custname,Suffix,InvNo,Stat ,Terms,Inv Date,Age,D ueDate,Pas tDue,Bille dAmt,PaidA mt,NetAmt, Currency,E quipment,W ayBillNo,W ayBillOSDa te,RefBLNo ,Origin,De stination,
Commodity,OSBillNo,OSCateg ory,OSType ,PromisedA mt,SchPymt Date,eBill iCash,eBil l820,Check No,PymtCnt rlNo,DueDa teAPC,AvgP ayCycle,No te,ActiveC ontact,Pro mised,Last Action,
Stage,FollowupDate,CommTit le,Followu pType,Prob Group,Prob No,ProbSta tus,ProbOw nerName1,P robOwnerNa me2,ProbRe ason,ProbR easonDesc, ProbResolv erName,Pro bContact,
replace(replace(replace((L EFT(ProbNo teIndicato r, 32767)),'&','&'),'&apo s;',''''), '"',' "') as ProbNoteIndicator,ProbDate Entered,Pr obDesc,Pro bNextStep,
ProbRequired,RMSQueueName, Transactio nType,ARRe p,Business Unit,@env AS WhatServer
FROM #ARInvDetail
SELECT Custname,Suffix,InvNo,Stat
Commodity,OSBillNo,OSCateg
Stage,FollowupDate,CommTit
replace(replace(replace((L
ProbRequired,RMSQueueName,
FROM #ARInvDetail
>This is report query:
Ok. So which column can we use in the report query to JOIN to the set you asked for? The set in the question is count's and sum's only..
Ok. So which column can we use in the report query to JOIN to the set you asked for? The set in the question is count's and sum's only..
Please show output of your last comment and the expected output from that. Thanks
ASKER
I created another field in my temp table called RecID which will only contain a 'X'
ProbRequired,RMSQueueName, Transactio nType,ARRe p,Business Unit,RecID ,@env AS WhatServer
FROM #ARInvDetail
ProbRequired,RMSQueueName,
FROM #ARInvDetail
ASKER
This works for me:
SELECT COUNT(InvNo) as InvoiceCount, SUM(BilledAmt) as TotalBilled, SUM(PaidAmt) as TotalPaid, SUM(NetAmt) as TotalNet, SUM(PromisedAmt) as TotalPromisedm, max(RecID) as RecID1 into #Totals11
FROM (SELECT InvNo, max(BilledAmt) as BilledAmt, max(PaidAmt) as PaidAmt, max(NetAmt) as NetAmt, max(PromisedAmt) as PromisedAmt, RecID as RecID FROM #ARInvDetail Group By InvNo, RecID) a
SELECT Custname,Suffix,InvNo,Stat ,Terms,Inv Date,Age,D ueDate,Pas tDue,Bille dAmt,PaidA mt,NetAmt, Currency,E quipment,W ayBillNo,W ayBillOSDa te,RefBLNo ,Origin,De stination,
Commodity,OSBillNo,OSCateg ory,OSType ,PromisedA mt,SchPymt Date,eBill iCash,eBil l820,Check No,PymtCnt rlNo,DueDa teAPC,AvgP ayCycle,No te,ActiveC ontact,Pro mised,Last Action,
Stage,FollowupDate,CommTit le,Followu pType,Prob Group,Prob No,ProbSta tus,ProbOw nerName1,P robOwnerNa me2,ProbRe ason,ProbR easonDesc, ProbResolv erName,Pro bContact,
replace(replace(replace((L EFT(ProbNo teIndicato r, 32767)),'&','&'),'&apo s;',''''), '"',' "') as ProbNoteIndicator,ProbDate Entered,Pr obDesc,Pro bNextStep,
ProbRequired,RMSQueueName, Transactio nType,ARRe p,Business Unit,RecID ,@env AS WhatServer,
InvoiceCount, TotalBilled, TotalPaid, TotalNet, TotalPromisedm, RecID1
FROM #ARInvDetail
Left Join #Totals11 a
ON a.RecID1 = #ARInvDetail.RecID
SELECT COUNT(InvNo) as InvoiceCount, SUM(BilledAmt) as TotalBilled, SUM(PaidAmt) as TotalPaid, SUM(NetAmt) as TotalNet, SUM(PromisedAmt) as TotalPromisedm, max(RecID) as RecID1 into #Totals11
FROM (SELECT InvNo, max(BilledAmt) as BilledAmt, max(PaidAmt) as PaidAmt, max(NetAmt) as NetAmt, max(PromisedAmt) as PromisedAmt, RecID as RecID FROM #ARInvDetail Group By InvNo, RecID) a
SELECT Custname,Suffix,InvNo,Stat
Commodity,OSBillNo,OSCateg
Stage,FollowupDate,CommTit
replace(replace(replace((L
ProbRequired,RMSQueueName,
InvoiceCount, TotalBilled, TotalPaid, TotalNet, TotalPromisedm, RecID1
FROM #ARInvDetail
Left Join #Totals11 a
ON a.RecID1 = #ARInvDetail.RecID
group by inv;