Link to home
Start Free TrialLog in
Avatar of careyb
careyb

asked on

I'm getting twice the sum I expect

SELECT Transactions.Account, Transactions.Fund,
SUM(Transactions.Shares) AS TotalShares,
MAX(Miscellaneous.[Date]) AS datDate,
Miscellaneous.Amount
FROM Transactions, Miscellaneous
WHERE
Transactions.Account = Miscellaneous.Account
AND Transactions.Hash = SearchHash
AND Miscellaneous.Hash = SearchHash
AND Transactions.PlanMne = Session("PlanMne")
AND  Miscellaneous.[Desc] = 'VSTPCT'
GROUP BY Transactions.Account, Transactions.Fund, Miscellaneous.Amount
ORDER BY Transactions.Account, Transactions.Fund

It may have something to do with my grouping?  When I remove all references to the 'Miscellaneous' table I get the correct TotalShares
Avatar of dacz
dacz

try this:

SELECT Transactions.Account, Transactions.Fund,
SUM(Transactions.Shares) AS TotalShares,
MAX(Miscellaneous.[Date]) AS datDate
FROM Transactions, Miscellaneous
WHERE
Transactions.Account = Miscellaneous.Account
AND Transactions.Hash = SearchHash
AND Miscellaneous.Hash = SearchHash
AND Transactions.PlanMne = Session("PlanMne")
AND  Miscellaneous.[Desc] = 'VSTPCT'
GROUP BY Transactions.Account, Transactions.Fund
ORDER BY Transactions.Account, Transactions.Fund

is the Miscellaneous.Amount needed? maybe Miscellaneous.Amount have diferent values.
Avatar of Guy Hengel [angelIII / a3]
SELECT t.Account, t.Fund,
select SUM(t.Shares) AS TotalShares,
m.[Date]) AS datDate,
m.Amount
FROM Transactions t
JOIN Miscellaneous m
 ON t.Account = m.Account
AND t.Hash = SearchHash
AND t.Hash = SearchHash
AND t.PlanMne = Session("PlanMne")
AND  m.[Desc] = 'VSTPCT'
AND m.[Date] = ( Select max(i.[date]) from Miscellaneous i WHERE i.Account = m.Account AND  i.[Desc] = 'VSTPCT' )
GROUP BY Transactions.Account, Transactions.Fund, m.Amount , m.[Date]
ORDER BY Transactions.Account, Transactions.Fund
Avatar of careyb

ASKER

dacz,
With yours I still get the double amounts.  I do need the miscellaneous amounts,  there is a percentage in that record (60, 80, 100, etc) that I need to associate with the record.
Avatar of careyb

ASKER

angelIII,
I'm getting ...
Error 7200: AQE Error: State = 42000; NativeError = 2115; [Extended Systems][Advantage SQL Engine]Expected lexical element not found: identifier or expression -- Location of error in the SQL statement is: 28 [Parsing Expression (column 3 in the SELECT clause)] SELECT t.Account, t.Fund, select SUM(t.Shares) AS TotalShares, m.[Date]) AS datDate, m.Amount FROM Transactions t JOIN Miscellaneous m ON t.Account = m.Account AND t.Hash = 996 AND t.Hash = 996 AND t.PlanMne = 'LACV1' AND m.[Desc] = 'VSTPCT' AND m.[Date] = ( Select max(i.[date]) from Miscellaneous i WHERE i.Account = m.Account AND i.[Desc] = 'VSTPCT' ) GROUP BY Transactions.Account, Transactions.Fund, m.Amount , m.[Date] ORDER BY Transactions.Account, Transactions.Fund

Is it the second Selelct that is the problem?  If I remove that I get a different error...
Error 7200: AQE Error: State = 42000; NativeError = 2115; [Extended Systems][Advantage SQL Engine]Expected lexical element not found: FROM You are missing the keyword FROM after the column definitions in your SELECT statement. -- Location of error in the SQL statement is: 67 SELECT t.Account, t.Fund, SUM(t.Shares) AS TotalShares, m.[Date]) AS datDate, m.Amount FROM Transactions t JOIN Miscellaneous m ON t.Account = m.Account AND t.Hash = 996 AND t.Hash = 996 AND t.PlanMne = 'LACV1' AND m.[Desc] = 'VSTPCT' AND m.[Date] = ( Select max(i.[date]) from Miscellaneous i WHERE i.Account = m.Account AND i.[Desc] = 'VSTPCT' ) GROUP BY Transactions.Account, Transactions.Fund, m.Amount , m.[Date] ORDER BY Transactions.Account, Transactions.Fund
Avatar of careyb

ASKER

angelIII

this was in there twice
AND t.Hash = SearchHash
AND t.Hash = SearchHash

I've changed the second one to m.Hash  for accuracy but that doesn't change the error msg or location.
Ahh, it looks like you want the amount from Miscellaneous that has the maximum date for this transaction.  To get that you'd need something like this:



SELECT Transactions.Account
, Transactions.Fund
, SUM(Transactions.Shares) AS TotalShares
, MAX(Miscellaneous.[Date]) AS datDate
, Miscellaneous.Amount
FROM Transactions t
INNER JOIN Miscellaneous m ON (t.Account = m.Account AND t.Hash = m.Hash)

WHERE t.Hash = SearchHash
AND Transactions.PlanMne = Session("PlanMne")
AND  Miscellaneous.[Desc] = 'VSTPCT'
AND Miscellaneous.[Date] =
  (SELECT MAX([Date] FROM Miscellaneous
   WHERE m2.Account = t.Account AND m2.Hash = t.Hash)

GROUP BY Transactions.Account, Transactions.Fund


This assumes that there will only ever be one Miscellaneous record on a given date.  If that's incorrect we'll have to rework it some more.
Avatar of careyb

ASKER

Actually I don't want the date from the Miscellaneous record, there is a Date, an Account, a Description, and an Amount  column.  Each quarter there could be a record inserted for each account (there could be 4 or more accounts)
12/31/06  Deferral  VstPct  100
12/31/06  Employer VstPct 100
12/31/05  Deferral  VstPct  80
12/31/05  Employer VstPct 80

I want to pick up those top 2 records only, from 12/31/06 and add them to the first group of data which will be the sum of shares added up for each of the accounts.

Does that help?
ASKER CERTIFIED SOLUTION
Avatar of VoteyDisciple
VoteyDisciple

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of careyb

ASKER

AngelIII,
I've also removed the 'Select'  from line 2 of yours....
select SUM(t.Shares) AS TotalShares,
as that was generating an error.

I've managed to dial in and run it on the SQL test window instead of trying to do it with an ASP page.  Using this...
SELECT t.Account, t.Fund,
SUM(t.Shares) AS TotalShares,
m.[Date] AS datDate,
m.Amount
FROM Transactions t
JOIN Miscellaneous m
 ON t.Account = m.Account
AND t.Hash = 996                     <<< hardcoded these for the moment
AND m.Hash = 996
AND t.PlanMne =  'LACV1'
AND  m.[Desc] = 'VSTPCT'
AND m.[Date] = ( Select max(i.[date]) from Miscellaneous i WHERE i.Account = m.Account AND  i.[Desc] = 'VSTPCT' )
GROUP BY Transactions.Account, Transactions.Fund, m.Amount , m.[Date]
ORDER BY Transactions.Account, Transactions.Fund

I get this error
Error 7200:  AQE Error:  State = 42000;   NativeError = 2117;  [Extended Systems][Advantage SQL Engine]Unexpected token: JOIN -- Expecting
semicolon. -- Location of error in the SQL statement is: 107 (line: 6 column: 1)

I'm no SQL expert but a semicolon at that spot just gets a error about the select stmt not being found,,, I think it's trying to tell me something else.
Avatar of careyb

ASKER

VoteyDisciple,
with this....
SELECT Transactions.Account
, Transactions.Fund
, SUM(Transactions.Shares) AS TotalShares
, SUM(Miscellaneous.Amount) AS TotalAmount
FROM Transactions t
INNER JOIN Miscellaneous m ON (t.Account = m.Account AND t.Hash = m.Hash)
WHERE t.Hash = 996
AND t.PlanMne = 'LACV1'
AND m.[Desc] = 'VSTPCT'
AND m.[Date] = (SELECT MAX(Date) FROM Miscellaneous m2 WHERE m2.Account = t.Account AND m2.Hash = t.Hash)

I'm getting this error....  
Error 7200:  AQE Error:  State = S0000;   NativeError = 2122;  [Extended Systems][Advantage SQL Engine]Table or alias not found: Transactions --
Location of error in the SQL statement is: 8
Avatar of careyb

ASKER

ok.... VoteyDisciple,
I changed the t's to Transaction and the m's to Miscellaneous and it looks to be working.

AngelIII I'll do the same with yours, my dial-in connection just kicked me off.

If they both work I'll increase the points and split them, no prob.

This is advantage database, I'll look into why something like "FROM Transactions t" may be causing probs..  

Thanks guys, I'll be back as soon as I can get dialed back in..
I'm sure it doesn't have a problem with the "FROM Transactions t" but once a table is renamed like that the word "Transactions" can't be used elsewhere in the query.  I just missed a couple in the SELECT portion of the clause.  It has to either be "t" everywhere or "Transactions" everywhere, but the mixture of both annoys it.

Avatar of careyb

ASKER

AngelIII,
I've changed the t's to Transaction & the m's to Miscellaneous and am still getting a syntax error ....

Unexpected token: JOIN -- Expecting semicolon. -- Location of error in the SQL statement is: 162 (line: 6 column: 1)


the stmt currently looks like
SELECT Transactions.Account, Transactions.Fund,
SUM(Transactions.Shares) AS TotalShares,
Miscellaneous.[Date] AS datDate,
Miscellaneous.Amount
FROM Transactions
JOIN Miscellaneous                        <???? don't see anything out of whack
 ON Transactions.Account = Miscellaneous.Account
AND Transactions.Hash = 996
AND Miscellaneous.Hash = 996
AND Transactions.PlanMne = 'LACV1'
AND  Miscellaneous.[Desc] = 'VSTPCT'
AND Miscellaneous.[Date] = ( Select max(i.[date]) from Miscellaneous i WHERE i.Account = Miscellaneous.Account AND  i.[Desc] = 'VSTPCT' )
GROUP BY Transactions.Account, Transactions.Fund, Miscellaneous.Amount , Miscellaneous.[Date]
ORDER BY Transactions.Account, Transactions.Fund

If we get it working I'd still be willing to add some points (may need to post another question)

Thanks guys!