首页 > 试题广场 >

订单表tbl_table用户表tbl_user1.2020年

[问答题]
订单表tbl_table

用户表tbl_user


1. 2020年第一季度(1-3月),全国整体业务同比下滑最大三个城市是哪几个?(用sql实现计算逻辑)
2. 公司希望各条业务线完成双十一100%的订单增长目标,帮助供应链团队计算下2020年双十一当天各省不同轮胎需要备多少库存(用sql实现轮胎条数计算的逻辑)
上面很多答案都运行不了,我专门建表跑了一下,下面的代码在我的测试数据下是ok的。
我的数据是四个用户分别在四个城市,然后算同比,方便调试代码。

with t1 as (
select  u.*,sum(salesamount) as smt  from tbl_table t join tbl_user u
on t.userid = u.userid
where date(orderdate)  between '2019-01-01' and '2019-03-31' group by City
),   # t1是2019年第一季度的表 
t2 as(
select u.*,sum(salesamount) as smt  from tbl_table t join tbl_user u
on t.userid = u.userid
where date(orderdate)  between '2020-01-01' and '2020-03-31' group by City
)    # t2是2020年第一季度的表 
select City from (
select t2.*,row_number() over(order by t2.smt/t1.smt )  as rk from   t1 join t2 on t1.City= t2.City
) b
where rk<4


发表于 2021-04-21 11:00:30 回复(1)
1. 
-- 业务指标 : 订单量 ,因为 tbl_tabl没有提供商品销量
  select 
      t_1.city
      ,t_1.oder_num_20- t_2.oder_num_19)/t_2.oder_num_19 as dec_rank 
  from 
  -- 2020年第一季度(1-3月)全国各城市订单量
  (select
       c.city
    -- 同比订单下滑量 (20 -19)/19 
      ,count b.oderid as oder_num_20
  from tbl_table b  left join  tbl_user c on b.userid = a.userid 
  where  b.oderdata between ’2020-01-01‘ and ’2020-01-03‘ 
  group by c.City ) as t_1 
  left join 
   -- 2019 年第一季度(1-3月)全国各城市订单量
  (select
       c.city
      ,count b.oderid as oder_num_19 
  from tbl_table b  left join  tbl_user c on b.userid = a.userid 
  where  b.oderdata between ’2019-01-01‘ and ’2019-01-03‘ 
  group by c.City ) as t_2 
  on t_1.city = t_2.c.ty 
  order by dec_rank 
  limit 3


2. 

-- 双十一100%的订单增长 :2020各省轮胎数 = 2019 各省轮胎数* 2 。
-- 由于没有具体的销量,只能用订单数代替销量.

selet 
 b.Province
  ,(count a.orderid ) *2 as luntai_predicted_value
from  tbl_table a
left join tbl_user b on a.userid=b.userid 
group by b.Province
having a.orderdata = '2019-11-11' and a.catagory = 轮胎 


发表于 2021-01-26 09:57:59 回复(3)
SELECT DISTINCT city
FROM
  (SELECT b.city,
          sum(CASE
                  WHEN a.orderdate BETWEEN '2020-01-01 00:00:00' AND '2020-03-31 23:59:59' THEN a.salesamount
                  ELSE 0
              END) AS sum_1,
          sum(CASE
                  WHEN a.orderdate BETWEEN '2019-01-01 00:00:00' AND '2019-03-31 23:59:59' THEN a.salesamount
                  ELSE 0
              END) AS sum_0
   FROM tbl_table a
   LEFT JOIN tbl_user b ON a.userid = b.userid
   GROUP BY b.city ) AS c
ORDER BY (sum_1-sum_0)/sum_0
LIMIT 3 ;


编辑于 2021-04-21 12:04:53 回复(6)
(1)
select t1.City
from 
    (select u.City,sum(t.salesamout) as sales-20 from tbl_table t
    left join tbl_user u on t.userid=u.userid where year(orderdate)=2020 and month(orderdate) between 1 and 3
    group by u.City) t1
left join 
    (select u.City,sum(t.salesamout) as sales-19 from tbl_table t
    left join tbl_user u on t.userid=u.userid where year(orderdate)=2019 and month(orderdate) between 1 and 3
    group by u.City) t2
on t1.City=t2.City
order by ((sales-20-sales-19)/sales-19) 
limit 3
(2)
select u.Province,count(category)*2 as kucun
from tbl_table t
left join tbl_user u on t.userid=u.userid 
where year(orderdate)=2019 and month(orderdate)=11 and day(orderdate)=11
and t.category='轮胎'
group by u.Province

发表于 2021-09-13 12:29:17 回复(0)
select 
    *
from 
(
select 
    city,
    sum(case when period = '今年' then sale_sum end) as this_year_sum,
    sum(case when period = '去年' then sale_sum end) as last_year_sum,
    sum(case when period = '今年' then sale_sum end)/sum(case when period = '去年' then sale_sum end) as growth --增长率
from
(
    select 
        City,
        case 
            when dt between '2020-01-01' and '2020-03-31' then '今年'
            when dt between '2019-01-01' and '2019-03-31' then '去年'
            else null
        end as period,
        sum(salesamount) as sale_sum
    from 
    (
        select 
            City,
            to_date(orderdate) as dt,
            orderid,
            userid,
            salesamount
        from 
            tbl_table
        where 
            (to_date(orderdate) between '2020-01-01' and '2020-03-31')
           &nbs***bsp;(to_date(orderdate) between '2019-01-01' and '2019-03-31')
    )t0
    left join 
    (
        select 
            userid,
            City
        from 
            tbl_user
    )t1
    on t0. userid = t1. userid
    group by 
        City,
        case 
            when dt between '2021-01-01' and '2021-03-31' then '今年'
            when dt between '2020-01-01' and '2020-03-31' then '去年'
            else null
        end
)dbase
group by 
    city
)a
order by growth asc --增长率升序,第一位增长率最低
limit 3
;

编辑于 2021-08-20 15:25:57 回复(1)
发表于 2021-07-25 19:43:34 回复(0)

生成数据进行验证

CREATE TABLE IF NOT EXISTS tbl_table(
orderid INT(6) NOT NULL PRIMARY KEY,
userid VARCHAR(20) NOT NULL,
orderdate DATETIME NOT NULL,
pid VARCHAR(20) NOT NULL,
salesamount NUMERIC(10,2) NOT NULL,
category VARCHAR(20) NOT NULL)
INSERT INTO tbl_table VALUES(1,3227,'2019-02-02','tr-dp',100,'轮胎'),
(2,3228,'2019-02-02','tr-dp',200,'轮胎'),
(3,3229,'2020-03-02','tr-dp',150,'保养'),
(4,3210,'2020-03-02','tr-dp',250,'轮胎'),
(5,3211,'2020-03-02','tr-dp',250,'轮胎'),
(6,3212,'2019-03-02','tr-dp',250,'轮胎'),
(7,3511,'2020-03-02','tr-dp',250,'轮胎'),
(8,3512,'2019-03-02','tr-dp',250,'轮胎');
SELECT * FROM tbl_table;
CREATE TABLE IF NOT EXISTS tbl_user (
userid VARCHAR(20) NOT NULL PRIMARY KEY,
province VARCHAR(20) NOT NULL,
city VARCHAR(20) NOT NULL);
INSERT INTO tbl_user VALUES(3227,'浙江','杭州'),
(3228,'广东','广州'),
(3229,'浙江','杭州'),
(3210,'广东','广州'),
(3211,'江西','南昌'),
(3212,'江西','南昌'),
(3511,'北京','北京'),
(3512,'北京','北京');
SELECT * FROM tbl_user;
##第一问
WITH t1 AS (SELECT tu.city,SUM(salesamount) AS cnt FROM tbl_table tt
LEFT JOIN tbl_user tu
ON tt.userid=tu.userid
WHERE orderdate BETWEEN '2019-01-01' AND '2019-03-31'
GROUP BY city) ,t2 AS (SELECT tu.city,SUM(salesamount) AS cnt FROM tbl_table tt
LEFT JOIN tbl_user tu
ON tt.userid=tu.userid
WHERE orderdate BETWEEN '2020-01-01' AND '2020-03-31'
GROUP BY city) 
SELECT b.city,t_rank  FROM 
(SELECT t1.city,row_number() over (ORDER BY (t2.cnt-t1.cnt)/t1.cnt) AS t_rank 
FROM t1 
LEFT JOIN t2 
ON t1.city=t2.city ) b
WHERE t_rank <4
#第二问
SELECT province, COUNT(*)*2 FROM tbl_table tt
LEFT JOIN tbl_user tu
ON tt.userid=tu.userid
WHERE orderdate BETWEEN '2019-01-01' AND '2019-12-31' AND category LIKE '%轮胎%'
GROUP BY province
编辑于 2021-05-28 09:14:48 回复(0)
#第1题题解:题目没有给出对应每张订单号的购买商品数量,故也无法得出商品出售总额,也只能从订单号的数量去衡量整体业务
SELECT City,
             # 2020年1-3月同比2019年1-3月
             (order_num_2020 - order_num_2019)/order_num_2019 AS tongbi
FROM 
      (
      SELECT City,
                     # 2019年1-3月的订单数量
                     COUNT(CASE WHEN orderdate BETWEEN '2019-01-01' AND '2019-03-31' THEN orderid ELSE NULL END) AS order_num_2019,
                     # 2020年1-3月的订单数量
                     COUNT(CASE WHEN orderdate BETWEEN '2020-01-01' AND '2020-03-31' THEN orderid ELSE NULL END) AS order_num_2020
      FROM tbl_user AS u 
      JOIN tbl_table AS t
      ON t.userid = u.userid
      GROUP BY City
      ) AS t1
# 降序排序
ORDER BY tongbi DESC
# 显示前三名
LIMIT 0,3


#第2题题解:
#'公司希望各条业务线完成双十一100%的订单增长目标',意思就是2020年比2019年增长100%,也即2020年销量=2019年销量*2;
#'帮助供应链团队计算下2020年双十一当天各省不同轮胎需要备多少库存',分别求出2019年各省不同轮胎的销量为多少(由于题目没有给出销量,故也只能用订单号代替)
#代码如下:
SELECT Province,
             pid,
             COUNT(orderid)*2 AS num_20201111 #2020年双十一需要准备的库存量
FROM tbl_table AS t
JOIN tbl_user AS u
ON u.userid = t.userid
WHERE category = '轮胎' #筛选出品类为轮胎的商品
AND orderdate = '2019-11-11' #筛选出2019年11月11日当天的销售信息
GROUP BY Province,pid

编辑于 2021-04-10 12:51:01 回复(1)
根据大家的思路做些修改,请帮忙看看有什么问题。
(1)(主要参考@是咸蛋黄呀)
select Province, (sum_2020 - sum_2019) / sum_2019
(select b.Province
    sum(case when a.orderdate between '2020-01-01' and '2020-03-31' then a.salesamount else 0 end) as sum_2020,
    sum(case when a.orderdate between '2019-01-01' and '2019-03-31' then a.salesamount else 0 end) as sum_2019
from tbl_table a, 
        tbl_user b
where a.userid = b.userid
group by b.Province )
order by 2 
limit 3 ;

(2)
select b.Province, count(a.orderid) * 2 
from tbl_table a, 
        tbl_user b
where a.userid = b.userid
    and a.category = '轮胎'  
    and orderdate = 2020-11-11
group by b.Province ;

发表于 2021-03-23 17:47:37 回复(0)
select City,,(sum(if(year(t1.d) == 2019,t1.s,0)-sum(if(year(t1.d) == 2020,t1.s,0))/sum(if(year(t1.d) == 2019,t1.s,0) as ratio from (select t.orderid, t.userid,data_format(orderdate,'%y-%m') as d,salesamout as s ,u.City from tbl_table as t inner join tbl_user as u on t.userid = u.userid where year(orderdate) in (2019,2020) and month (orderdate) in(1,2,3) group by t.orderid,) t1 group by City order by radio desc limit 3;
发表于 2021-03-09 15:33:53 回复(0)
第二小题
select  u.province,ifnull(count(*),0)*2 num from tbl_table join tbl_user u on t.user_id=u.user_id where date_format(t.orderdate,"%Y%m%d")='2019-11-11' and t.category="轮胎" group by u.province;
发表于 2021-03-05 18:36:20 回复(0)
1.select e.city as city, ((c.saleamount_2020s1-d.saleamount_2019s1)/saleamount_2019s1) as desecresing_ratio
from tbl_user as e,
(select b.city as city, sum(saleamount) as saleamount_2020s1
from tbl_table as a left join tbl_user as b on a.user_id=b.user_id
where a.orderdate>'2020-01-01' and a.orderdate<'2020-03-31') as c,
(select b.city as city, sum(saleamount) as saleamount_2019s1
from tbl_table as a left join tbl_user as b on a.user_id=b.user_id
where a.orderdate>'2019-01-01' and a.orderdate<'2019-03-31') as d
where e.city=c.city and e.city=d.city
order by desecresing_ratio
limit 3

2.select b.procince, a.pid, count(orderid)*2 as amount
from tbl_table as a left join tbl_user as b on a.user_id=b.user_id
where date(orderdate)='2019-11-11'
group by b.province, a.pid
发表于 2021-02-05 14:40:09 回复(1)
1.
select c1.city as city , c1.saletotal/c2.saletotal-1 as salegrowthrate 
from
(select u1.city as city, sum(t1.salesamout) as saletotal
from tbl_table as t1 left join tbl_user as u1 on t1.userid = u1.userid
where t1.orderdate between '2020-1-1' and  '2020-3-31'
group by city)as c1,
(select u1.city as city, sum(t1.salesamout) as saletotal
from tbl_table as t1 left join tbl_user as u1 on t1.userid = u1.userid
where t1.orderdate between '2019-1-1' and  '2019-3-31'
group by city)as c2
where c1.city = c2.city
order by salegrowthrate ASC
limit 3
2.
select u1.province as province, t1.pid as pid, count(t1.orderid)*2 as product_stock_20201111
from tbl_table as t1
left join tbl_user as u1 on t1.userid=u1.userid 
where t1.category = '轮胎' and date(t1.orderdate) = '2019-11-11'





发表于 2021-01-14 15:24:28 回复(0)