Problem with max() and min() function
I created following table
create table Interiors(
no integer,
name varchar,
type varchar,
dateofstock datetime,
price decimal(6,3),
discount numeric(6,2))
i want to solve these two queries.
- Display the type, sum of price and average of discount by grouping type as 'baby cot'.
- Display the type, maximum and minimum of price by grouping type as 'baby cot'.
I wrote following stmts for each resp.
- select 开发者_高级运维type, sum(price), avg(discount) from Interiors group by 'baby cot'.
- select type, max(price), min(price) from Interiors group by 'baby cot'.
I got following error same for both like that 'type is not an aggregate fun. or it dont have GROUP BY' clause. What should I do for solving that queries.
group by 'baby cot'.
You cant group by a name of something, you have to group by a column name. For instance do:
select type, sum(price), avg(discount) from Interiors group by type
or
select type, sum(price), avg(discount) from Interiors where type = 'baby cot' group by type
You need to group by the field name, not the contents. If you wish to only include the data where the type is 'baby cot', you would include that in the WHERE clause. For example...
SELECT
type,
SUM(price) AS "sum",
AVG(price) AS "avg",
MAX(price) AS "max",
MIN(price) AS "min"
FROM
Interiors
WHERE
type = 'baby cot'
GROUP BY
type
Have a read of this: http://msdn.microsoft.com/en-us/library/aa258901%28v=sql.80%29.aspx
It's the MSDN entry for SQL Server Aggregate Functions. There's plenty of example, including what you're trying to achieve.
精彩评论