views:

445

answers:

3

[ edit ]

For the record, here's the problem portion of the query, which is now working:

SELECT 
 m.groupNum, 
 t.ea,
 ( t.ea - ( t.ea * m.margin )) AS estCost,
 (( t.ea - ( t.ea * m.margin )) * t.quantity ) AS salesSub,
 ((( t.ea - ( t.ea * m.margin )) * t.quantity ) / 
   (
    SELECT SUM(( t2.ea - ( t2.ea * m.margin )) * t2.quantity )
    FROM temp as t2
    INNER JOIN masters as m2 
    ON t2.mod = m2.mod
    WHERE m2.groupNum = m.groupNum
    )
  ) AS salesPercent
etc...

[ end edit ]

I think I need a query that can recursively update itself based on the total of a column's values after inserting values on all the rest of the records for a given (groupNum) range.

I already have the estCost and salesSub fields. Now I need to do calculations on the salesPercent field, which involves knowing the total amount of all salesSub records in a given set (groupNum).

salesPercent = 
  ( salesSub / [the sum total of all salesSub amounts for each groupNum] )

(snip)

SELECT 
 m.id, 
 t.priceEach,
 ( t.priceEach - ( t.priceEach * m.margin )) AS estCost,
 (( t.priceEach - ( t.priceEach * m.margin )) * t.quantity ) AS salesSub
 -- is it possible to perform calculation on salesPercent here?
INTO output

FROM financeMasters AS m
INNER JOIN temp AS t .......

(end snip)

I have this...

------
output
---------------------------------------------------------------
id | groupNum | priceEach | estCost | salesSub | salesPercent |
---------------------------------------------------------------
1  | apple    | 150.00    |  90.00  | 90.00    |   
2  | apple    | 100.00    |  60.00  | 60.00    |   
3  | apple    |  50.00    |  30.00  | 30.00    |

but how can I calculate salesPercent on the salesSub total (in this case 180.00) before knowing the total?

+2  A: 

You'll probably have to use a subselect to do the math on each row, there's no way to "go back" and change previous rows.

Something like this:

SELECT 
 m.id, 
 t.priceEach,
 ( t.priceEach - ( t.priceEach * m.margin )) AS estCost,
 (( t.priceEach - ( t.priceEach * m.margin )) * t.quantity ) AS salesSub,
 ((( t.priceEach - ( t.priceEach * m.margin )) * t.quantity )
     / (SELECT SUM(( t2.priceEach - ( t2.priceEach * m.margin )) * t2.quantity )
        FROM financeMasters AS m2
            INNER JOIN temp AS t2 .....
        WHERE m2.groupNum = m.groupNum))
     AS salesPercent
INTO output

FROM financeMasters AS m
INNER JOIN temp AS t .......

Yes, it's pretty ugly. I had to leave out some details due to not knowing how you were doing the join, and also I'm not sure which table that groupNum is coming from, you didn't show that anywhere.

Chad Birch
Chad, please see my update in the original post. the sub select doesn't seem to be getting the total value of the salesSub column.will a sub select run after all other values have been determined?I tried referencing the actual table that all data is being input to ( the 'output' table ) but that threw access errors which made me believe access couldn't find those values yet.
42
the problem is that (and I'm not sure about this) but that it -seems- like the temp table in the sub select doesn't know anything about any of the salesSub values since they don't exist until they are put into the output table.
42
I got it. :) Chad Birch used the same 't' alias for both temp tables. That was throwing thing off in my actual query. After I changed the inner SELECT SUM ... to use a different table alias, everything worked perfectly.
42
Ah, sorry about that, 42, my mistake. I'll update the query.
Chad Birch
+1  A: 

Like Chad Birch said, a subselect is probably your best bet. You could make it less ugly by storing the subselect in a separate query.

John M Gant
+1  A: 

What you are going to do is call that query from another query (and join that with the original). Assuming your query is called salesCalc. It looks like this (I've left out the join):

SELECT m.id, 
    t.priceEach,
    ( t.priceEach - ( t.priceEach * m.margin )) AS estCost,
    (( t.priceEach - ( t.priceEach * m.margin )) * t.quantity ) AS salesSub
INTO output

FROM financeMasters AS m
INNER JOIN temp AS t .......

You will create another query called salesSum. It uses the first query. It will probably look exactly like this:

select sum(salesSub) as salesGroupTotal, groupNum
from salesCalc
group by groupNum;

You will create a third query called salesPercent. It uses both the first and second. It will look like this (I've left out the join here as well):

select salesCalc.*,  salesCalc.SalesSub/salesSum.salesGroupTotal
from salesCalc, salesSum
inner join ...

(YMMV)

CodeSlave
I didn't know you could reference queries in access like classes.are you referencing the actual name of the query as saved in access?
42
You can reference queries like tables. CodeSlave is suggesting you inner join to the salesSub query. This is the right approach based on the details you've provided. If you're getting incorrect results, double check the results of salesSub by itself.
John M Gant
which is the salesCalc query? in codeSlave's first [salesSum] example, he's selecting FROM salesCalc. is salesCalc a query or a table at that point? I think I'm mostly confused about which queries come first.
42
I think salesCalc is intended to represent your original query. salesPercent would be your final query, the one with the INTO clause. So you'd have three altogether. It's not strictly necessary to split them up like this, but it's a more Access-ish way to do it from my experience. (I think it's easier to work with small simple queries in Access because the SQL behind larger ones is unusually ugly and parenthesis-ridden).
John M Gant
jmgant - your right salesCalc was meant to represent the original query
CodeSlave
@codeslave. could you please clarify your response. I'm not seeing it yet. also, I'm not sure about the [square bracket titles]. are those the name of the saved query? maybe that's what's confusing me.
42
when I try referencing a saved query, I get the "enter parameter value" box for [saved-query-name].fieldName.
42
42, I can see my []'s were confusing the issue (especially since they mean something in MS-Access. I've reformatted my answer for clarity.
CodeSlave