| Author |
Topic |
|
bmahony993
Yak Posting Veteran
58 Posts |
Posted - 2008-01-18 : 17:38:37
|
| I have a stored procedure, and the user enters a projectId, start date and end date. The sp does some calculations and also works out a beginning balance. However, if the user wants to use a wildcard for the projectId, I don't know how to make the "beginBal" calculation for each project. Below is part of the code I have. Any help would be greatly appreciated :)Create Table #tbl_BalanceSheet (projectId char(6), project_desc char(100), customer char(10), projSite char(10), status_pa char(10), rpm char(5), rpmName char(50), padm char(5), padmName char(50), begin_date char(12), end_date char(12), JrnlType char(4), TranType char(3), TranDate char(12), PerEnt char(10), RefNbr char(10), batNbr char(10), perpost char(6), acct char(4), trandesc char(100), beginBal money, cramt money, dramt money )INSERT INTO #tbl_BalanceSheet(projectId, project_desc, customer,projSite, status_pa, rpm, rpmName, padm, padmName, begin_date, end_date, JrnlType, TranType, TranDate, PerEnt, RefNbr, batNbr, perpost, acct, trandesc, beginBal,cramt, dramt) SELECT g.projectId, p.project_desc, p.customer, NULL, p.status_pa, p.manager1, NULL, p.manager2, NULL, p.start_date, p.end_date, g.JrnlType, g.TranType, g.TranDate, g.PerEnt, g.RefNbr, g.batNbr, g.perpost, g.acct, g.trandesc,0, g.cramt, g.dramt FROM GLTran g INNER JOIN PJPROJ p ON g.projectid = p.project WHERE g.acct = 1400 and g.posted = 'p' AND (g.projectId LIKE @project_ID + '%') Update #tbl_BalanceSheet Set beginBal = (Select Sum([dramt]-[cramt])from #tbl_BalanceSheet where perpost < @startPeriod), padmName = (Select formalName from tblAbraData where pen = padm), rpmName = (Select formalName from tblAbraData where pen = rpm), projSite = (Select distinct pe_id02 from PJPENT where project LIKE @project_ID) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-18 : 22:01:20
|
quote: Originally posted by bmahony993 I have a stored procedure, and the user enters a projectId, start date and end date. The sp does some calculations and also works out a beginning balance. However, if the user wants to use a wildcard for the projectId, I don't know how to make the "beginBal" calculation for each project. Below is part of the code I have. Any help would be greatly appreciated :)Create Table #tbl_BalanceSheet (projectId char(6), project_desc char(100), customer char(10), projSite char(10), status_pa char(10), rpm char(5), rpmName char(50), padm char(5), padmName char(50), begin_date char(12), end_date char(12), JrnlType char(4), TranType char(3), TranDate char(12), PerEnt char(10), RefNbr char(10), batNbr char(10), perpost char(6), acct char(4), trandesc char(100), beginBal money, cramt money, dramt money )INSERT INTO #tbl_BalanceSheet(projectId, project_desc, customer,projSite, status_pa, rpm, rpmName, padm, padmName, begin_date, end_date, JrnlType, TranType, TranDate, PerEnt, RefNbr, batNbr, perpost, acct, trandesc, beginBal,cramt, dramt) SELECT g.projectId, p.project_desc, p.customer, NULL, p.status_pa, p.manager1, NULL, p.manager2, NULL, p.start_date, p.end_date, g.JrnlType, g.TranType, g.TranDate, g.PerEnt, g.RefNbr, g.batNbr, g.perpost, g.acct, g.trandesc,0, g.cramt, g.dramt FROM GLTran g INNER JOIN PJPROJ p ON g.projectid = p.project WHERE g.acct = 1400 and g.posted = 'p' AND (g.projectId LIKE @project_ID + '%') Update #tbl_BalanceSheet Set beginBal = (Select Sum([dramt]-[cramt])from #tbl_BalanceSheet where perpost < @startPeriod), padmName = (Select formalName from tblAbraData where pen = padm), rpmName = (Select formalName from tblAbraData where pen = rpm), projSite = (Select distinct pe_id02 from PJPENT where project LIKE @project_ID)
Change Update like this & try:-Update t Set t.beginBal = (Select Sum([dramt]-[cramt])from #tbl_BalanceSheet where projectId=t.projectId AND perpost < @startPeriod), padmName = (Select formalName from tblAbraData where projectId=t.projectId AND pen = padm), rpmName = (Select formalName from tblAbraData where projectId=t.projectId AND pen = rpm), projSite = (Select distinct pe_id02 from PJPENT where project = t.projectId )FROM #tbl_BalanceSheet t |
 |
|
|
bmahony993
Yak Posting Veteran
58 Posts |
Posted - 2008-01-22 : 10:56:37
|
| That almost works, but seems to only return the beginBal for the first projectId - any others in the dataset are getting a null value? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-22 : 11:29:55
|
quote: Originally posted by bmahony993 That almost works, but seems to only return the beginBal for the first projectId - any others in the dataset are getting a null value?
Try this then:-Create Table #tbl_BalanceSheet (projectId char(6),project_desc char(100),customer char(10),projSite char(10),status_pa char(10),rpm char(5),rpmName char(50),padm char(5),padmName char(50),begin_date char(12),end_date char(12),JrnlType char(4),TranType char(3),TranDate char(12),PerEnt char(10),RefNbr char(10),batNbr char(10),perpost char(6), acct char(4),trandesc char(100),beginBal money,cramt money,dramt money)INSERT INTO #tbl_BalanceSheet(projectId, project_desc, customer,projSite, status_pa, rpm, rpmName, padm, padmName, begin_date, end_date, JrnlType, TranType,TranDate, PerEnt, RefNbr, batNbr, perpost, acct, trandesc, beginBal,cramt, dramt)SELECT g.projectId, p.project_desc, p.customer, NULL, p.status_pa, p.manager1, NULL, p.manager2, NULL, p.start_date, p.end_date, g.JrnlType, g.TranType, g.TranDate, g.PerEnt,g.RefNbr, g.batNbr, g.perpost, g.acct, g.trandesc,0, g.cramt, g.dramtFROM GLTran g INNER JOINPJPROJ p ON g.projectid = p.project WHERE g.acct = 1400 and g.posted = 'p'AND (g.projectId LIKE @project_ID + '%') Update tbsSet tbs.beginBal = Sum(CASE WHEN perpost < @startPeriod THEN tbs.[dramt]-tbs.[cramt] ELSE 0 END),tbs.padmName = tad1.formalName ,tbs.rpmName = tad2.formalName ,tbs.projSite = p.pe_id02FROM #tbl_BalanceSheet tbsINNER JOIN tblAbraData tad1ON tad1.pen =tbs.padmINNER JOIN tblAbraData tad2ON tad2.pen =tbs.rpmINNER JOIN PJPENT pON p.project=tbs.projectid |
 |
|
|
bmahony993
Yak Posting Veteran
58 Posts |
Posted - 2008-01-22 : 12:22:03
|
| That doesn't work either - "an aggregate may not appear in the set list of an update statement". I tried moving the SUM into the CASE statement but still got the same error :( |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-22 : 12:26:51
|
quote: Originally posted by bmahony993 That doesn't work either - "an aggregate may not appear in the set list of an update statement". I tried moving the SUM into the CASE statement but still got the same error :(
Can you explain what the value beginBal stores? |
 |
|
|
bmahony993
Yak Posting Veteran
58 Posts |
Posted - 2008-01-22 : 12:38:03
|
| It is a data type Money, and should ideally add the credit column(cramt)for each month before the start period that the user enters, and subtract that total from the sum of the debit column (dramt) for each month before the start period.project cramt dramt perpost133500 25 50 200612133500 14 0 200611133500 25 60 200610133500 26 40 200701If the user entered 200701 as the start date, then the beginning balance would be (150 - 90) or 60. I need to calculate this for each project, assuming the user will use a wild card something like 1335% |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-22 : 12:41:34
|
| try like this:-t.beginBal = (Select Sum([dramt])-Sum([cramt])from #tbl_BalanceSheet where projectId=t.projectId AND perpost < @startPeriod) |
 |
|
|
bmahony993
Yak Posting Veteran
58 Posts |
Posted - 2008-01-22 : 12:47:28
|
| That still only gives me a beginning balance for the first project, with NULL for the others that follow. I wonder if it just can't be done this way at all...Thanks for all your suggestions though :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-22 : 12:49:15
|
quote: Originally posted by bmahony993 That still only gives me a beginning balance for the first project, with NULL for the others that follow. I wonder if it just can't be done this way at all...Thanks for all your suggestions though :)
Are you sure you dont have NULL values in both the fields? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-22 : 12:56:01
|
| try this too:-Update tbsSet tbs.beginBal = tmp.OpenBal,tbs.padmName = tad1.formalName ,tbs.rpmName = tad2.formalName ,tbs.projSite = p.pe_id02FROM #tbl_BalanceSheet tbsINNER JOIN tblAbraData tad1ON tad1.pen =tbs.padmINNER JOIN tblAbraData tad2ON tad2.pen =tbs.rpmINNER JOIN PJPENT pON p.project=tbs.projectidINNER JOIN (SELECT projectid,Sum([dramt])-Sum([cramt]) AS OpenBal FROM #tbl_BalanceSheet WHERE perpost < @startPeriod GROUP BY projectid)tmpON tmp.projectid=tbs.projectid |
 |
|
|
bmahony993
Yak Posting Veteran
58 Posts |
Posted - 2008-01-22 : 13:00:11
|
| There is definitely a value in cramt and dramt - even if the value is 0. I will have to check the data some more - there must be something I am not seeing that is causing the NULL for some values. Again, thanks so much for your help! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-22 : 13:06:07
|
quote: Originally posted by bmahony993 There is definitely a value in cramt and dramt - even if the value is 0. I will have to check the data some more - there must be something I am not seeing that is causing the NULL for some values. Again, thanks so much for your help!
tried my last attempt too? |
 |
|
|
|
|
|