mysql aggregate_mysql: aggregation

/***************

* Aggregation *

***************/

-- Find minimum msrp of all products

select min(msrp)

from product;

-- Find maximum msrp of all products

select max(msrp)

from product;

-- Top 2 MSRP of all products;

select *

from product

order by msrp

limit 2;

-- Find average msrp of all products

select avg(msrp) from product;

-- Find how many kind of products we have

select count(product_id)

from product;

-- Find total of msrp of all kind of products

select sum(product_id)

from product;

-- Find total sales revenue(amount);

select sum(price*quantity)

from sales;

select * from sales;

/***************

* Group by *

***************/

-- Find total sales revenue by product. Order by sales_amount desendingly;

select *, price*quantity, sum(price*quantity)

from sales

group by product_id

order by quantity desc;

-- total # of transactions by product

select p.*, count(1) as tran_count

from sales s, product p

where s.product_id = p.product_id

group by s.product_id;

-- count distinct product and client that have sales;

select p.name, count(s.product_id), count(s.client_id)

from sales s,client c,product p

where s.client_id = c.client_id and s.product_id = p.product_id;

-- how many different/unique products each client purchased;

select c.name,count(distinct s.product_id)

from sales s,client c

where s.client_id = c.client_id

group by s.client_id;

-- find clients that purchase more than 1 unique products;

select c.name, count(distinct s.product_id) dis_product

from client c, sales s

where s.client_id = c.client_id

group by s.client_id

having dis_product > 2;

-- practice: find products that are sold less than 20 units;

select p.name, sum(s.quantity) as total_sale

from product p, sales s

where p.product_id = s.product_id

group by s.product_id

having total_sale < 20;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值