首页 > 试题广场 >

在oracle数据库中,有一张表waybill_consty

[问答题]
在oracle数据库中,有一张表waybill_constype记录了客户使用快递的信息。
(consign_day int comment “揽收日期” ,waybill_no varchar(30) “运单号“,class varchar(10) “托寄物类型”,userid  varchar(20) comment “会员号”),请统计各个会员在4月份揽收的文件、娱乐、食品的件量以及主要托寄物类型(托寄物类型件量最多的就是主要托寄物类型)
waybill_constype中waybill_no是运单号,class是托寄物类型()


select  userid,sum(case when class='文件' then 1 else 0 end) as filessum,sum(case when class=' 娱乐' then 1 else 0 end) as eSum,sum(case when class=' 食品' then 1 else 0 end) as foodSum,max(filessum,sSum,foodSum) as '主要物托寄类型' from waybill_constype where month(consign_day)=4 group by userid
发表于 2019-08-29 00:14:57 回复(4)
select user_id,mainclass,sum(cnt_class) as total from 
(SELECT user_id,class, count(1) as cnt_class, last_value(class) over (partition by user_id order by count(1))as main_class
 from waybill_constype 
where month(consign-day)=4
group by user_id,class ) group by user_id

发表于 2019-07-30 11:30:22 回复(3)

想的很基础,一步一步来的,暂未想优化做法,各位前辈如有好方法,可以多多指教~

-- 第一部分:
-- 各个会员的托寄类型分别是文件、娱乐和食品的件量
select
userid,
sum(case when class = '文件' then 1 else 0 end) as sum_wenjian,
sum(case when class = '娱乐' then 1 else 0 end) as sum_yule,
sum(case when class = '食品' then 1 else 0 end) as sum_shipin
from waybill_constype
where substr(consign_day,5,2) = '04'
group by userid;

-- select userid,
-- row_number() over(partition by userid order by class desc)
-- from waybill_constype;

-- 第二部分:
-- 可以使用窗口函数处理,因为我的工具是MySQL,该版本还不支持窗口函数,故
-- 使用非窗口函数
-- 对每个会员的每种类型进行数量降序操作
select 
userid,class,count(userid) as class_nums
from waybill_constype
where substr(consign_day,5,2) = '04'
group by userid,class
order by userid,count(userid) desc
;

-- 先获取每个会员的不同托运类型的最大件数

select 
a.userid,max(class_nums) as class_max_num
from (
    select 
    userid,class,count(userid) as class_nums
    from waybill_constype
    where substr(consign_day,5,2) = '04'
    group by userid,class
    order by userid,count(userid) desc
)a 
group by a.userid;

-- 将上面两个结果进行拼接

select 
t1.userid,t1.class,t1.class_nums
from (
    select 
    userid,class,count(userid) as class_nums
    from waybill_constype
    where substr(consign_day,5,2) = '04'
    group by userid,class
    order by userid,count(userid) desc

)t1 
join (
    select 
    a.userid,max(class_nums) as class_max_num
    from (
        select 
        userid,class,count(userid) as class_nums
        from waybill_constype
        where substr(consign_day,5,2) = '04'
        group by userid,class
        order by userid,count(userid) desc
    )a 
    group by a.userid
)t2 
on t1.userid = t2.userid
and t1.class_nums = t2.class_max_num;

-- 最终将 第一部分 和 第二部分 进行表连接处理
-- 得到最终的结果:
select 
tt1.userid,tt1.sum_wenjian,tt1.sum_yule,tt1.sum_shipin,
tt2.class,tt2.class_nums
from (
    select
    userid,
    sum(case when class = '文件' then 1 else 0 end) as sum_wenjian,
    sum(case when class = '娱乐' then 1 else 0 end) as sum_yule,
    sum(case when class = '食品' then 1 else 0 end) as sum_shipin
    from waybill_constype
    where substr(consign_day,5,2) = '04'
    group by userid
) tt1 
join (
    select 
    t1.userid,t1.class,t1.class_nums
    from (
        select 
        userid,class,count(userid) as class_nums
        from waybill_constype
        where substr(consign_day,5,2) = '04'
        group by userid,class
        order by userid,count(userid) desc

    )t1 
    join (
        select 
        a.userid,max(class_nums) as class_max_num
        from (
            select 
            userid,class,count(userid) as class_nums
            from waybill_constype
            where substr(consign_day,5,2) = '04'
            group by userid,class
            order by userid,count(userid) desc
        )a 
        group by a.userid
    )t2 
    on t1.userid = t2.userid
    and t1.class_nums = t2.class_max_num
) tt2 
on tt1.userid = tt2.userid ;



我是自己创建了一个数据表,也通过代码测试。相关数据如下:
这里,个人感到满意的地方是,这种要求的数据可能会有多个第一,目前代码可以将其都进行相关展示。
see you ~
编辑于 2021-02-27 20:39:24 回复(0)
select  userid,class,count(waybill_no) 
from waybill_constype 
where consign_day <= '20180430' and consign_day >= '20180401'  
group by userid,class
发表于 2019-07-29 20:26:59 回复(0)
第一步:求各会员在4月份揽收的文件、娱乐、食品的件量
select userid, class, count(class) as count
from waybill_constype
where consign_day in (20180401, 20180430)
group by userid;

第二步:求主要托寄物类型(托寄物类型件量最多的就是主要托寄物类型)
select class, max(count) 
from (
    select userid, class, count(class) as count 
    from waybill_constype
    where consign_day in (20180401, 20180430)
    group by class) a
order by count desc
limit 1;

发表于 2020-08-20 12:55:50 回复(0)
    int a[4]={-1,2,1,-4},i,j,k,target=1,b[100][4],n=0;
    for(i=0;i<4;i++)
    {
        for(j=i+1;j<4;j++)
        {
            for(k=j+1;k<4;k++)
            {
                b[n][0]=a[i];
                b[n][1]=a[j];
                b[n][2]=a[k];
                b[n][3]=fabs(a[i]+a[j]+a[k]-target);
                n++;
            }
        }
    }
    j=b[0][3];
    k=0;
    for(i=1;i<n;i++)
        if(b[i][3]<j)
        {
            j=b[i][3];
            k=i;
        }
    printf("%d",b[k][0]+b[k][1]+b[k][2]);
发表于 2020-04-27 11:25:15 回复(0)