select (sum(field) - max(field)) from table group by field

fish man

I have 2 tables:

CREATE TABLE IF NOT EXISTS `prodotti` (
  `ID` smallint(3) NOT NULL,
  `Denominazione` varchar(15) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `prodotti` (`ID`, `Denominazione`) VALUES
(111, 'latte'),
(222, 'uova');


CREATE TABLE IF NOT EXISTS `fornitori` (
  `ID` int(3) NOT NULL,
  `CF` varchar(5) NOT NULL,
  `date` varchar(8) NOT NULL,
  `Prezzo` int(3) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `fornitori` (`ID`, `CF`, `date`, `Prezzo`) VALUES
(111, 'AAAAA', '22/11/09', 100),
(222, 'AAAAA', '22/11/09', 200),
(222, 'BBBBB', '28/10/09', 400),
(111, 'CCCCC', '30/12/07', 100),
(222, 'CCCCC', '30/12/07', 200);

Now here is some sql query:

SELECT P.ID, COUNT(*) 
  FROM prodotti P, 
       (SELECT (SUM(Prezzo) - MAX(Prezzo)) AS T
          FROM Fornitori
        GROUP BY 
               prezzo
        )F
 WHERE F.T <200
GROUP BY 
       P.ID

But i have some question,

SELECT (
SUM( Prezzo ) - MAX( Prezzo )
) AS T
FROM Fornitori

this return 600, but why add GROUP BY prezzo, the result return 100 200 0? and what is the mean of WHERE F.T <200 thanks.

siride

When you group by Prezzo, you'll have one row for every distinct price, aggregated from the rows in Fornitori. Then, for each row, the sum of Prezzo is computed (which is simply the sum of a single item, since you grouped by Prezzo), as is the max, and then those two are subtracted from each other. Without the GROUP BY, all rows, and thus, all prices are aggregated and you get the sum of ALL prices minus the maximum of ALL prices.

The question for you is: why are you grouping by Prezzo and then using aggregates on it? Usually, you group by one or more columns and then use aggregates on other columns (i.e., not the ones you group by). It seems to me that you probably want to be grouping by something other than Prezzo, or, if you do want to group by Prezzo (for example, to get information about products of each of the different prices), then you want to be aggregating some other column.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select rows from table that have duplicate field, but prioritize group by

From Dev

Select (Select field from FieldTable) from Table

From Dev

select field from table in another field from another table

From Dev

Exclude a field from a SELECT * FROM $table

From Dev

Select from Table Where field equals a variable

From Dev

Select from mysql table WHERE field in '$array'?

From Dev

Select fields from table with DISTINCT field

From Dev

Select Additional Field from another Table

From Dev

Call field from another table in select firebase

From Dev

Select multiple columns from a table, but group by one

From Dev

How to DELETE With SELECT SUM() FROM Table GROUP BY

From Dev

Select Data from multiple table and Group by Category

From Dev

mysql select * from table group by id with rollup

From Dev

SQLite, select where field 'like' field from another table

From Dev

Replace field with field from other table in SELECT query

From Dev

Is it possible to select a field from a table, but override it with a new value in the select statement?

From Dev

Is it possible to select a field from a table, but override it with a new value in the select statement?

From Dev

Select field from table A where subselect from table B = table A field

From Dev

SELECT * FROM table WHERE "substring" IN table_field?

From Dev

select from table with group by where date>date in mysql table

From Dev

Select all fields from table A but single field from B?

From Dev

Display stored value from mysql table into select input field

From Dev

BigQuery: select from regexp_match where field in other table

From Dev

MySQL SELECT DISTINCT field from second table using a relationship

From Dev

Select rows from DB in dependence of field value in child table

From Dev

Dynamic table name in where clause using a field from select

From Dev

Based on select field generate a table from json file

From Dev

MySQL Query from one table - Select same field twice

From Dev

select multiple columns from another table where field contains array

Related Related

  1. 1

    Select rows from table that have duplicate field, but prioritize group by

  2. 2

    Select (Select field from FieldTable) from Table

  3. 3

    select field from table in another field from another table

  4. 4

    Exclude a field from a SELECT * FROM $table

  5. 5

    Select from Table Where field equals a variable

  6. 6

    Select from mysql table WHERE field in '$array'?

  7. 7

    Select fields from table with DISTINCT field

  8. 8

    Select Additional Field from another Table

  9. 9

    Call field from another table in select firebase

  10. 10

    Select multiple columns from a table, but group by one

  11. 11

    How to DELETE With SELECT SUM() FROM Table GROUP BY

  12. 12

    Select Data from multiple table and Group by Category

  13. 13

    mysql select * from table group by id with rollup

  14. 14

    SQLite, select where field 'like' field from another table

  15. 15

    Replace field with field from other table in SELECT query

  16. 16

    Is it possible to select a field from a table, but override it with a new value in the select statement?

  17. 17

    Is it possible to select a field from a table, but override it with a new value in the select statement?

  18. 18

    Select field from table A where subselect from table B = table A field

  19. 19

    SELECT * FROM table WHERE "substring" IN table_field?

  20. 20

    select from table with group by where date>date in mysql table

  21. 21

    Select all fields from table A but single field from B?

  22. 22

    Display stored value from mysql table into select input field

  23. 23

    BigQuery: select from regexp_match where field in other table

  24. 24

    MySQL SELECT DISTINCT field from second table using a relationship

  25. 25

    Select rows from DB in dependence of field value in child table

  26. 26

    Dynamic table name in where clause using a field from select

  27. 27

    Based on select field generate a table from json file

  28. 28

    MySQL Query from one table - Select same field twice

  29. 29

    select multiple columns from another table where field contains array

HotTag

Archive