0

This issue has been bothering me for some time now. I have made the statement and it works as I get all my data out the way I want it.

CREATE VIEW `pizza` AS
SELECT 
    `table1`.`id` AS `id`,
    `table1`.`name` AS `name`,
    `table2`.`name` AS `rb_name`,
    `table3`.`netto` AS `min`,
    `table3`.`netto` AS `max`
FROM
    ((`table1`
    JOIN `table3` ON ((`table1`.`id` = `table3`.`id`)))
    JOIN `table2` ON ((`table2`.`t1_id` = `table3`.`t1_id`)))

Now the problem is that i want to SUM the min and max value in the table. But when I do that I go from having a list of results to just having 1 result. This is the code i add to the SELECT statements where I have the min and max value:

        SUM((`table3`.`netto` - `table3`.`tolerance`)) AS `min`,
        SUM((`table3`.`netto` + `table3`.`tolerance`)) AS `max`,

I don't know how to work around it. My database structure is as follows:

Table 1 has columns "rb_id" and "rb_name".

Table 2 has columns "id", "name" and others that aint beeing used here.

Table 3 connects them both by having "rb_id" and "id" with the "netto" and "tolerance" values

1
  • what does table3 contain? can you explain better your database structure and how table3 is related to table1 and table2? Commented May 1, 2016 at 11:47

1 Answer 1

1

I think you need a group by clause for a aggregation function (sum)

SELECT 
    `table1`.`id` AS `id`,
    `table1`.`name` AS `name`,
    `table2`.`name` AS `rb_name`,
    SUM((`table3`.`netto` - `table3`.`tolerance`)) AS `min`,
    SUM((`table3`.`netto` - `table3`.`tolerance`)) AS `max`,
FROM
    ((`table1`
    JOIN `table3` ON ((`table1`.`id` = `table3`.`id`)))
    JOIN `table2` ON ((`table2`.`t1_id` = `table3`.`t1_id`)))
GROUP BY   `table1`.`id` ,    `table2`.`name` ;
Sign up to request clarification or add additional context in comments.

3 Comments

this was what I needed. It now shows me a list of result with the SUM of min and max for every item. Thank you !!
@Kingfox A question: What's the difference between your `min` and `max` columns? They are calculated the same. A typo?
Yeah its a typo, but I first realised that after it was working :) I have edited the post, so it doesnt contain it any more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.