Solved

I'm getting twice the sum I expect

Posted on 2006-11-11
13
438 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:careyb
13 Comments
 
LVL 1

Expert Comment

by:dacz
ID: 17924233
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.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17924581
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
0
 

Author Comment

by:careyb
ID: 17924997
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.
0
 

Author Comment

by:careyb
ID: 17925002
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
0
 

Author Comment

by:careyb
ID: 17925173
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.
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17925314
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.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:careyb
ID: 17925554
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?
0
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 250 total points
ID: 17925593
That still seems to depend on the date though.  You listed four records there and said you only want the top two, which seems (from your earlier query and just from looking at that sample) like you're looking for the total Amount from all records on the latest date listed?

If that sounds right, how's this for a query:

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 = SearchHash
AND t.PlanMne = Session("PlanMne")
AND m.[Desc] = 'VSTPCT'
AND m.[Date] = (SELECT MAX(Date) FROM Miscellaneous m2 WHERE m2.Account = t.Account AND m2.Hash = t.Hash)

GROUP BY Transactions.Account, Transactions.Fund
0
 

Author Comment

by:careyb
ID: 17925688
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.
0
 

Author Comment

by:careyb
ID: 17925739
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
0
 

Author Comment

by:careyb
ID: 17925756
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..
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17925874
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.

0
 

Author Comment

by:careyb
ID: 17930164
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!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now