首页 > 试题广场 >

SQL编程(不限定何种数据库,可使用mysql,oracle

[问答题]
SQL编程(不限定何种数据库,可使用mysql,oracle,sqlserver,hive等任何一种语法)
已知有以下3个表的结构信息:
订单表O(订单号orderid, 用户userid, 日期date)   -- 订单明细表,10亿条(近3年数据)
用户表U(用户userid, 身份证sid,用户等级level,性别sex)     -- 用户基本信息,1亿条 
身份证S(身份证sid , 城市cityid)      --居民身份证信息表,5000W条

说明:
1. 一个用户有可能不会下订单;
2. 一个用户有可能没有身份证信息;不同用户可能会使用同一个身份证信息;
3. 实名用户:有身份证的信息的用户


要求在一个SQL语句完成,统计每个用户等级的以下数据统计

  用户等级

用户数 c1

居民数 c2

苏州实名用户的订单数c3

有下订单的用户数 c4

非实名用户所下的订单数 c5

2017年的有下订单的居民数 c6


结果示例:

用户等级

c1

c2

c3

c4

c5

c6

Level0

100

101

102

103

104

105

Level1

200

201

202

203

204

205

...

 

 

 

 

 

 

 

 


select
      level
      ,count(distinct U.userid) as c1
      ,count(distinct U.sid) as c2
      ,count(distinct case when cityid='苏州'  then orderid else null) as c3
      ,count(distinct case when orderid is not null then U.userid else null) as c4
      ,count(distinct case when U.sid is null then orderid else null) as c5
      ,count(distinct case when substr(date,1,4)='2017' and  orderid is not null then U.sid else null) as c5
from U 
left join O on U.userid = O.userid 
left join S on U.sid = S.sid
group by level


发表于 2021-08-13 14:08:12 回复(0)
Select u.level
,count(u.userid)

, count(distinct sid)

,sum(case when s.cityid=苏州and sid is not null then o. ordercnt end) as suzhoucnt

,sum(case when o.ordercnt >0 then 1 else 0 end) as orderusercnt

,sum(case when s.sid is null then ordercnt else 0 end) as nonameusercnt

,count( distnct case when 0.2017cnt>0 then sid end ) as 2017ordersid

from U u  

left join (

Select

userid

,count(orderid) as ordercnt

, sum(case when date between 20170101 and 20171231 then 1 else 0) as 2017cnt

from O group by userid

) o on u.userid=o.userid

Left join S s on u.sid=s.sid
group by u.level
发表于 2022-03-09 22:20:42 回复(0)
select a.level, a.c1,a.c2,c.c3,d.c4,e.c5,f.c6 from 
(select level ,count(1)  c2, count(distinct sid) c1  from U group by level) a join
(select b.level , count(1) c3 from O left join (select U.userid, U.sid, U.level from U left join S on U.sid=S.sid where U.sid is not null and S.cityid=='苏州') b
on O.userid=b.userid group by b.level where b.sid is not null )c  on a.level=c.level join 
(select U.level,count(distinct U.sid) c4 from O left join U on O.userid=U.userid group by U.level) d on a.level=d.level join 
(select U.level,count(1) c5 from O left join U on O.userid=U.userid group by U.level where U.userid is null ) e on a.level=e.level join 
(select U.level ,count(distinct O.userid) c6 from  O left join U on O.userid=U.userid  group by O.level where to_year(O.date)=='2017' )f on a.level=f.level 
发表于 2021-07-30 11:36:56 回复(0)
SELECT a.level,a.c1,a.c2,b.c3,c.c4,d.c5,e.c6
from (
    SELECT level,count(distinct userid) as c1,count(DISTINCT sid) as c2
    from U
    group by level
) a join (
    SELECT level,count(orderid) as c3
    from O left join U
    on o.userid = u.userid
    left join S
    on s.sid = u.sid
    where s.cityid = '苏州'
    and u.sid is not null
    group by u.level
) b on a.level = b.level
join (
    SELECT u.level,count(DISTINCT userid) as c4
    from O left join U
    on o.userid = u.userid
    group by level
) c
on b.level = c.level
join (
    SELECT level,count(orderid) as c5
    from O left join U
    on o.userid = u.userid
    where u.sid is null
    group by u.level
) d
on d.level = c.level
join (
    SELECT level,count(DISTINCT sid) as c6
    from O left join U
    on o.userid = u.userid
    where u.sid is not null
    and year(date)='2017'
    group by level
) e
on e.level = d.level
发表于 2021-04-19 11:04:37 回复(0)