PTA Gets the average price score of the specified condition commodity 4

This topic requires writing SQL statements,

In the sh_goods table, query the average price of products whose product quantity is greater than 2 under different product categories (category_id). The query result display fields are set according to the output sample.

Tip: Please use the SELECT statement to answer.

Table Structure:

Please write the SQL statement that defines the table structure here. For example:

CREATE TABLE sh_goods (
  id INT  PRIMARY KEY,                         --商品id
  category_id INT  NOT NULL DEFAULT 0 ,        -- 商品分类id
  name VARCHAR(120) NOT NULL,                  --商品名称
  keyword VARCHAR(255) NOT NULL,               -- 关键词编号
  content TEXT NOT NULL ,                      --商品详情
  price DECIMAL(10, 2)  NOT NULL DEFAULT 0 ,   --价格
  stock INT  NOT NULL DEFAULT 0,               -- 库存
  score DECIMAL(3, 2)  NOT NULL DEFAULT 0 ,    -- 用户评分
  comment_count INT  NOT NULL DEFAULT 0        -- 评论数量
) ;

table sample

sh_goods table:

Sample output:

Code length limit 16 KB

Time limit 400 ms

Database SQL Server

The result output requires a strict comparison of the order and data

select category_id, avg(price) average
from sh_goods
group by category_id
having count(*) > 2

Guess you like

Origin blog.csdn.net/weixin_70206677/article/details/129360255