SQL题
7、登录表playerlogin如下:Vopenid ddatetime
小a 2017060506
小a 2017060611
小b 2017060606
小b 2017060506
小c 2017060606
小c 2017060506
金币流水表moneyflow如下:
Vopenid ddatetime money AddOrReduce(充值即增加,消费即减少)
小a 2017060506 500 增加
小a 2017060608 1000 减少
小a 2017060612 1000 减少
小b 2017060606 100 减少
小d 2017060106 1050 减少
1)用sql写出6月6日,有过登录的人数,有过充值的人数,及充值金额(一条sql写出查询三个数值)
2)求出游戏中所有的玩家数
建表sql:
-- 登入表
create or replace temporary view playerlogin (Vopenid, ddatetime) as
values ("小a", "2017060506"),
("小a", "2017060611"),
("小b", "2017060606"),
("小b", "2017060506"),
("小c", "2017060606"),
("小c", "2017060506");
-- 金币流水表(充值即增加,消费即减少)
create or replace temporary view moneyflow (Vopenid,ddatetime,money,AddOrReduce) as
values ("小a","2017060506",500,"增加"),
("小a","2017060608",1000,"减少"),
("小a","2017060612",1000,"减少"),
("小b","2017060606",100,"减少"),
("小d","2017060106",1050,"减少");
用sql写出6月6日,有过登录的人数,有过充值的人数,及充值金额(一条sql写出查询三个数值)
这三个条件同时满足吗??
select vopenid,addorreduce,money from moneyflow where ddatetime="20170606";
要是彼此独立的,我不认为用一条语句就能写出来 2)求出游戏中所有的玩家数
select vopenid from moneyflow UNIONselect vopenid from playerlogin; 本帖最后由 wp231957 于 2022-3-8 16:16 编辑
嗯,彼此独立,还真能用一条语句写出来
不知道是不是符合要求:
select vopenid,money,addorreduce from moneyflow where ddatetime="20170606" UNIONselect vopenid,0 ,0 from playerlogin where ddatetime="20170606";
本帖最后由 512189654 于 2022-3-8 22:53 编辑
-- 1)用sql写出6月6日,有过登录的人数,有过充值的人数,及充值金额(一条sql写出查询三个数值)
with t1 as (
select Vopenid as id1, ddatetime
from playerlogin
where substr(ddatetime, 5, 4) = "0606"),
t2 as (
select Vopenid as id2, ddatetime, money, AddOrReduce
from moneyflow
where substr(ddatetime, 5, 4) = "0606"
and AddOrReduce = "增加")
select "0606" as ddatetime,
count(distinct id1) as `登入人数`,
count(distinct id2) as `充值人数`,
sum(money) as total_money
from t1
full outer join t2 on id1 = id2;
-- 2)求出游戏中所有的玩家数
select count(*)
from (select Vopenid
from playerlogin
union
select Vopenid
from moneyflow);
页:
[1]