Sum in Query problem/question

W

wesley.allen

Hello,

I am trying to create a query that will sum several lines. Details are
below, but I am trying to have several occurances of Open Trades from a
table sum into one number in my query. When I set it up, it shows a
seperate line for each. I am basic at this, and don't have much
experience with SQL. Can anyone help?

Thanks.


Tables: Fund Name Info, Portfolio, Open Trades
Fields in Query: Portfolio.AccNum, Portfolio.CUSIP,
Portfolio.Description, Portfolio.SECClass, Portfolio.Shares, Open
Trades.Shares, Adjusted Position


SQL:
SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares,
Sum((Portfolio!Shares-[Open Trades]!Shares)) AS [Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));


CUSIP is the common field in Portfolio and Open Trades. The open
trades may have several occurances of CUSIP that have different
positions, and I want to SUM those positions in one field.


Thanks Again.
 
S

Steve Schapel

Wesley,

You are grouping by fields that return unique values. It seems to me
that you will need to remove such fields from the query. I am not sure
of the meaning of all of the fields, but for a start try removing the
Portfolio.Shares and [Open Trades].Shares fields and see what you get.
 
W

wesley.allen

Thanks Steve. Sorry I was not very clear.

My query pulls a share position from my portfolio table. The portfolio
table links to an Open Trades Table using a CUSIP Number. The CUSIP
only appears once in the Portfolio Table, but multiple times in Open
Trades Table. What I am trying to do is get the multiple lines in the
Open Trades table with the same CUSIP to add up and only appear once in
my query.

Is this possible?

Thanks.



Steve said:
Wesley,

You are grouping by fields that return unique values. It seems to me
that you will need to remove such fields from the query. I am not sure
of the meaning of all of the fields, but for a start try removing the
Portfolio.Shares and [Open Trades].Shares fields and see what you get.

--
Steve Schapel, Microsoft Access MVP

Hello,

I am trying to create a query that will sum several lines. Details are
below, but I am trying to have several occurances of Open Trades from a
table sum into one number in my query. When I set it up, it shows a
seperate line for each. I am basic at this, and don't have much
experience with SQL. Can anyone help?

Thanks.


Tables: Fund Name Info, Portfolio, Open Trades
Fields in Query: Portfolio.AccNum, Portfolio.CUSIP,
Portfolio.Description, Portfolio.SECClass, Portfolio.Shares, Open
Trades.Shares, Adjusted Position


SQL:
SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares,
Sum((Portfolio!Shares-[Open Trades]!Shares)) AS [Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));


CUSIP is the common field in Portfolio and Open Trades. The open
trades may have several occurances of CUSIP that have different
positions, and I want to SUM those positions in one field.


Thanks Again.
 
W

wesley.allen

Thanks Steve. Sorry I was not very clear.
My query pulls a share position from my portfolio table. The portfolio
table links to an Open Trades Table using a CUSIP Number. The CUSIP
only appears once in the Portfolio Table, but multiple times in Open
Trades Table. What I am trying to do is get the multiple lines in the
Open Trades table with the same CUSIP to add up and only appear once in
my query.

Is this possible?

Thanks.



Steve said:
Wesley,

You are grouping by fields that return unique values. It seems to me
that you will need to remove such fields from the query. I am not sure
of the meaning of all of the fields, but for a start try removing the
Portfolio.Shares and [Open Trades].Shares fields and see what you get.

--
Steve Schapel, Microsoft Access MVP

Hello,

I am trying to create a query that will sum several lines. Details are
below, but I am trying to have several occurances of Open Trades from a
table sum into one number in my query. When I set it up, it shows a
seperate line for each. I am basic at this, and don't have much
experience with SQL. Can anyone help?

Thanks.


Tables: Fund Name Info, Portfolio, Open Trades
Fields in Query: Portfolio.AccNum, Portfolio.CUSIP,
Portfolio.Description, Portfolio.SECClass, Portfolio.Shares, Open
Trades.Shares, Adjusted Position


SQL:
SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares,
Sum((Portfolio!Shares-[Open Trades]!Shares)) AS [Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));


CUSIP is the common field in Portfolio and Open Trades. The open
trades may have several occurances of CUSIP that have different
positions, and I want to SUM those positions in one field.


Thanks Again.
 
S

Steve Schapel

Wesley,

Yes, I understood what you want to do. And yes, it is certainly
possible. So it is me that needs to apologise for lack of clarity.

What I meant was, if you want the totals to be for each CUSIP, then you
will need to remove all fields from the query where the value is
different for records *within* each CUSIP grouping. I would expect that
[Open Trades].Shares falls into this category, so remove it from the
query. That's it. I had also suggested removing the Portfolio.Shares
field, and that's probably desirable but probably not necessary for the
solution to the problem. So, did you try what I suggested?
 
S

Steve Schapel

Wesley,

You need to let us know whether you tried the previously offered
solution, and what the result was.

--
Steve Schapel, Microsoft Access MVP


Thanks Steve. Sorry I was not very clear.

My query pulls a share position from my portfolio table. The portfolio
table links to an Open Trades Table using a CUSIP Number. The CUSIP
only appears once in the Portfolio Table, but multiple times in Open
Trades Table. What I am trying to do is get the multiple lines in the
Open Trades table with the same CUSIP to add up and only appear once in
my query.

Is this possible?

Thanks.



Steve said:
Wesley,

You are grouping by fields that return unique values. It seems to me
that you will need to remove such fields from the query. I am not sure
of the meaning of all of the fields, but for a start try removing the
Portfolio.Shares and [Open Trades].Shares fields and see what you get.

--
Steve Schapel, Microsoft Access MVP

(e-mail address removed) wrote:
Hello,

I am trying to create a query that will sum several lines. Details are
below, but I am trying to have several occurances of Open Trades from a
table sum into one number in my query. When I set it up, it shows a
seperate line for each. I am basic at this, and don't have much
experience with SQL. Can anyone help?

Thanks.


Tables: Fund Name Info, Portfolio, Open Trades
Fields in Query: Portfolio.AccNum, Portfolio.CUSIP,
Portfolio.Description, Portfolio.SECClass, Portfolio.Shares, Open
Trades.Shares, Adjusted Position


SQL:
SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares,
Sum((Portfolio!Shares-[Open Trades]!Shares)) AS [Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));


CUSIP is the common field in Portfolio and Open Trades. The open
trades may have several occurances of CUSIP that have different
positions, and I want to SUM those positions in one field.


Thanks Again.
 
W

wesley.allen

Thanks Again Steve. I removed these from the query. Now what? I am
trying to sum the amounts in the open trades table, field shares. If I
remove it, what do I do?

Thanks.


Steve said:
Wesley,

Yes, I understood what you want to do. And yes, it is certainly
possible. So it is me that needs to apologise for lack of clarity.

What I meant was, if you want the totals to be for each CUSIP, then you
will need to remove all fields from the query where the value is
different for records *within* each CUSIP grouping. I would expect that
[Open Trades].Shares falls into this category, so remove it from the
query. That's it. I had also suggested removing the Portfolio.Shares
field, and that's probably desirable but probably not necessary for the
solution to the problem. So, did you try what I suggested?

--
Steve Schapel, Microsoft Access MVP

Thanks Steve. Sorry I was not very clear.

My query pulls a share position from my portfolio table. The portfolio
table links to an Open Trades Table using a CUSIP Number. The CUSIP
only appears once in the Portfolio Table, but multiple times in Open
Trades Table. What I am trying to do is get the multiple lines in the
Open Trades table with the same CUSIP to add up and only appear once in
my query.

Is this possible?

Thanks.
 
S

Steve Schapel

Wesley,

Well, run the query, and see if it gives you the expected result.

Instead of this (which you gave us)...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares,
Sum((Portfolio!Shares-[Open Trades]!Shares)) AS [Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

.... the SQL of the query will be like this...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Sum((Portfolio!Shares-[Open Trades]!Shares)) AS
[Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

.... or, for that matter, to simplify even further, since the Fund Name
Info table seems to be serving no useful purpose...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Sum((Portfolio!Shares-[Open Trades]!Shares)) AS
[Adjusted Position]
FROM Portfolio LEFT JOIN [Open Trades] ON (Portfolio.AccNum = [Open
Trades].AccNum) AND (Portfolio.CUSIP = [Open Trades].CUSIP)
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

So, how does that differ from what you need? Specifically?
 
W

wesley.allen

When I try to save using the SQL you sent, I get a syntax error that
says "Syntax Error in JOIN operation"


Steve said:
Wesley,

Well, run the query, and see if it gives you the expected result.

Instead of this (which you gave us)...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares,
Sum((Portfolio!Shares-[Open Trades]!Shares)) AS [Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

... the SQL of the query will be like this...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Sum((Portfolio!Shares-[Open Trades]!Shares)) AS
[Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

... or, for that matter, to simplify even further, since the Fund Name
Info table seems to be serving no useful purpose...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Sum((Portfolio!Shares-[Open Trades]!Shares)) AS
[Adjusted Position]
FROM Portfolio LEFT JOIN [Open Trades] ON (Portfolio.AccNum = [Open
Trades].AccNum) AND (Portfolio.CUSIP = [Open Trades].CUSIP)
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

So, how does that differ from what you need? Specifically?

--
Steve Schapel, Microsoft Access MVP

Thanks Again Steve. I removed these from the query. Now what? I am
trying to sum the amounts in the open trades table, field shares. If I
remove it, what do I do?
 
W

wesley.allen

OK, I got it to work, but it isn't giving me what I need.

Let me look and see what is going on.

Thanks.


Steve said:
Wesley,

Well, run the query, and see if it gives you the expected result.

Instead of this (which you gave us)...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares,
Sum((Portfolio!Shares-[Open Trades]!Shares)) AS [Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

... the SQL of the query will be like this...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Sum((Portfolio!Shares-[Open Trades]!Shares)) AS
[Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

... or, for that matter, to simplify even further, since the Fund Name
Info table seems to be serving no useful purpose...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Sum((Portfolio!Shares-[Open Trades]!Shares)) AS
[Adjusted Position]
FROM Portfolio LEFT JOIN [Open Trades] ON (Portfolio.AccNum = [Open
Trades].AccNum) AND (Portfolio.CUSIP = [Open Trades].CUSIP)
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

So, how does that differ from what you need? Specifically?

--
Steve Schapel, Microsoft Access MVP

Thanks Again Steve. I removed these from the query. Now what? I am
trying to sum the amounts in the open trades table, field shares. If I
remove it, what do I do?
 
W

wesley.allen

Do you have an email where I can send screenprints of what I am getting
to help explain?


Steve said:
Wesley,

Well, run the query, and see if it gives you the expected result.

Instead of this (which you gave us)...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares,
Sum((Portfolio!Shares-[Open Trades]!Shares)) AS [Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Portfolio.Shares, [Open Trades].Shares
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

... the SQL of the query will be like this...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Sum((Portfolio!Shares-[Open Trades]!Shares)) AS
[Adjusted Position]
FROM [Fund Name Info] INNER JOIN (Portfolio LEFT JOIN [Open Trades] ON
(Portfolio.AccNum = [Open Trades].AccNum) AND (Portfolio.CUSIP = [Open
Trades].CUSIP)) ON [Fund Name Info].[Fund Number] = Portfolio.AccNum
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

... or, for that matter, to simplify even further, since the Fund Name
Info table seems to be serving no useful purpose...

SELECT Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass, Sum((Portfolio!Shares-[Open Trades]!Shares)) AS
[Adjusted Position]
FROM Portfolio LEFT JOIN [Open Trades] ON (Portfolio.AccNum = [Open
Trades].AccNum) AND (Portfolio.CUSIP = [Open Trades].CUSIP)
GROUP BY Portfolio.AccNum, Portfolio.CUSIP, Portfolio.Description,
Portfolio.SECClass
HAVING (((Portfolio.SECClass)="TERM" Or (Portfolio.SECClass)="LOC"));

So, how does that differ from what you need? Specifically?

--
Steve Schapel, Microsoft Access MVP

Thanks Again Steve. I removed these from the query. Now what? I am
trying to sum the amounts in the open trades table, field shares. If I
remove it, what do I do?
 
S

Steve Schapel

Wesley,

Ok, I think I know what you want. You want the Portfolio.Shares minus
the total of the [Open Trades]!Shares for each CUSIP, right?

Well you will need to do it in 2 steps.

First make a query based on the [Open Trades] table, like this...

Select AccNum, CUSIP, Sum([Shares]) As OTShares
FROM [Open Trades]
GROUP BY AccNum, CUSIP

Then, make a second query, from this first query joined to your
Portfolio table, like this...
SELECT Portfolio.AccNum, Portfolio.CUSIP,
Portfolio.Shares-[FirstQuery].OTShares AS [Adjusted Position]
FROM Portfolio INNER JOIN [FirstQuery] ON Portfolio.AccNum =
[FirstQuery].AccNum AND Portfolio.CUSIP = [FirstQuery].CUSIP
WHERE Portfolio.SECClass In("TERM","LOC")

Well, probably still needs some refining, but as a baseline let us know
how that looks.
 
W

wesley.allen

THAT'S IT. Thank you very much for the help and for being so patient.


Steve said:
Wesley,

Ok, I think I know what you want. You want the Portfolio.Shares minus
the total of the [Open Trades]!Shares for each CUSIP, right?

Well you will need to do it in 2 steps.

First make a query based on the [Open Trades] table, like this...

Select AccNum, CUSIP, Sum([Shares]) As OTShares
FROM [Open Trades]
GROUP BY AccNum, CUSIP

Then, make a second query, from this first query joined to your
Portfolio table, like this...
SELECT Portfolio.AccNum, Portfolio.CUSIP,
Portfolio.Shares-[FirstQuery].OTShares AS [Adjusted Position]
FROM Portfolio INNER JOIN [FirstQuery] ON Portfolio.AccNum =
[FirstQuery].AccNum AND Portfolio.CUSIP = [FirstQuery].CUSIP
WHERE Portfolio.SECClass In("TERM","LOC")

Well, probably still needs some refining, but as a baseline let us know
how that looks.

--
Steve Schapel, Microsoft Access MVP

OK, I got it to work, but it isn't giving me what I need.

Let me look and see what is going on.
 
W

wesley.allen

Hello Steve. I know this is an old issue, but I wonder if I could
bring it back for a quick question or addition.

While using this process, we have noticed a slight problem. All the
trades, whether buys or sells, seem to subtract from the overall
number. There is another column in my table, TransCode, that has 2
choices, B and SEL. I would like to change the query below that you
previously provided for me, that will subtract the Shares Column when
TransCode is B and add when it is SEL. Further, if there are B's and
SEL's, I would like to take the net. Is this possible?

Thanks,
Wes
 
S

Steve Schapel

Wes,

Does this do what you want?...
Select AccNum, CUSIP, Sum([Shares]*IIf([TransCode]="B",1,-1)) As OTShares
FROM [Open Trades]
GROUP BY AccNum, CUSIP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top