Author |
Topic |
Neutje
Starting Member
8 Posts |
Posted - 2006-11-21 : 08:38:11
|
Hi all,From this morning I'm trying to calculate the total amount with this query:select (count(*) * table1.amount) as counter from table1 , table2where table2.order=100 AND table1.order=100 group by table2.order,table1.amountthe result is only correct when there is only one product in the orderWhen there are more results I get more results from the query but I want to have the sum of the results of the query. I tried many ways but can't seem to find a way to get the correct result.Hope anyone can help mebest regards,Neut David |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 08:43:15
|
Your query makes no sense at all. Start with this oneselect table1.order, sum(table2.amount) as counter, count(*) as somethingelsefrom table1inner join table2 on table2.order = table1.orderwhere table1.order in (100, 101, 102)group by table1.order And try your way what you REALLY want, since you can't explain it properly...Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 08:51:27
|
It would be so much better if you could provide some proper sample data, the expected result of the provided sample data and maybe even some DDL.Peter LarssonHelsingborg, Sweden |
 |
|
Neutje
Starting Member
8 Posts |
Posted - 2006-11-21 : 09:35:58
|
This query is a part of a bigger procedure I'm working on so It's very difficult to post data and soIn this query there are 2 different outputs (counter & somethingelse)What i need is the sum of amount*count (sum(table2.amount * count(*))When I know how to do that I can handle the rest of itquote: Originally posted by Peso Your query makes no sense at all. Start with this oneselect table1.order, sum(table2.amount) as counter, count(*) as somethingelsefrom table1inner join table2 on table2.order = table1.orderwhere table1.order in (100, 101, 102)group by table1.order And try your way what you REALLY want, since you can't explain it properly...Peter LarssonHelsingborg, Sweden
|
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 09:43:36
|
The formula you describe fills no function.You want the count of related records multiplied with some amount, take the sum of all that, and calculate the records (which is 1) and then multiply with amount again?Peter LarssonHelsingborg, Sweden |
 |
|
Neutje
Starting Member
8 Posts |
Posted - 2006-11-21 : 09:52:18
|
I want the count of related records multiplied with the amountThen I want the sum of all the matchesorder 100 has 2 or more products with a differend amountso i need to get the count of the same products multiplied with the amountwith that result i want to make the sum of all products from the orderorder 1005x product 1 with amount 22 = 1101x product 2 with amount 27 = 27the result I want = 137the 5x is the count(*) I dothe amount can be different for each productwith my query I get the 2 results : 110 and 27 what I need to get is one result: 137thats why i'm trying to make the sum of the resultshope i'm clear nowquote: Originally posted by Peso The formula you describe fills no function.You want the count of related records multiplied with some amount, take the sum of all that, and calculate the records (which is 1) and then multiply with amount again?Peter LarssonHelsingborg, Sweden
|
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 09:55:07
|
How does your tables look like? Post some DDL (definitions here).Peter LarssonHelsingborg, Sweden |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-21 : 09:57:31
|
Something like this?SELECT o.OrderNumber, SUM(ors.Items * ors.Price)FROM Orders oINNER JOIN OrderRows ors ON ors.[Order] = o.[Order]GROUP BY o.OrderNumber Peter LarssonHelsingborg, Sweden |
 |
|
|