有趣的SQL题

本文提供了一系列SQL查询示例,涉及用户资料管理、刷题行为分析、视频观看习惯研究、金币获取策略以及商品销售统计。这些查询展示了如何从不同角度理解和挖掘用户数据,包括用户留存率、商品动销和滞销情况、订单金额统计等。
摘要由CSDN通过智能技术生成

文章内容摘自互联网,所有SQL语句是基于Oracle19c版本做的修改,其中有包含自增列语法,所以建议在Oracle12c及以上版本里执行。

牛客网

DROP TABLE USER_PROFILE PURGE;
DROP TABLE QUESTION_PRACTICE_DETAIL PURGE;
DROP TABLE QUESTION_DETAIL PURGE;


CREATE TABLE USER_PROFILE (
ID NUMBER(10) PRIMARY KEY,
DEVICE_ID NUMBER(10) NOT NULL,
GENDER VARCHAR2(14) NOT NULL,
AGE NUMBER(10) ,
UNIVERSITY VARCHAR2(32) NOT NULL,
GPA NUMBER(10,2),
ACTIVE_DAYS_WITHIN_30 NUMBER(10) ,
QUESTION_CNT NUMBER(10) ,
ANSWER_CNT NUMBER(10));

CREATE TABLE QUESTION_PRACTICE_DETAIL (
ID NUMBER(10) PRIMARY KEY,
DEVICE_ID NUMBER(10) NOT NULL,
QUESTION_ID NUMBER(10) NOT NULL,
RESULT VARCHAR2(32) NOT NULL,
DATE_ DATE NOT NULL);

CREATE TABLE QUESTION_DETAIL (
ID NUMBER(10) PRIMARY KEY,
QUESTION_ID NUMBER(10) NOT NULL,
DIFFICULT_LEVEL VARCHAR2(32) NOT NULL);


INSERT INTO USER_PROFILE VALUES(1,2138,'MALE',21,'北京大学',3.4,7,2,12);
INSERT INTO USER_PROFILE VALUES(2,3214,'MALE',NULL,'复旦大学',4.0,15,5,25);
INSERT INTO USER_PROFILE VALUES(3,6543,'FEMALE',20,'北京大学',3.2,12,3,30);
INSERT INTO USER_PROFILE VALUES(4,2315,'FEMALE',23,'浙江大学',3.6,5,1,2);
INSERT INTO USER_PROFILE VALUES(5,5432,'MALE',25,'山东大学',3.8,20,15,70);
INSERT INTO USER_PROFILE VALUES(6,2131,'MALE',28,'山东大学',3.3,15,7,13);
INSERT INTO USER_PROFILE VALUES(7,4321,'MALE',28,'复旦大学',3.6,9,6,52);
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(1,2138,111,'WRONG',TO_DATE('2021-05-03','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(2,3214,112,'WRONG',TO_DATE('2021-05-09','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(3,3214,113,'WRONG',TO_DATE('2021-06-15','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(4,6543,111,'RIGHT',TO_DATE('2021-08-13','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(5,2315,115,'RIGHT',TO_DATE('2021-08-13','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(6,2315,116,'RIGHT',TO_DATE('2021-08-14','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(7,2315,117,'WRONG',TO_DATE('2021-08-15','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(8,3214,112,'WRONG',TO_DATE('2021-05-09','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(9,3214,113,'WRONG',TO_DATE('2021-08-15','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(10,6543,111,'RIGHT',TO_DATE('2021-08-13','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(11,2315,115,'RIGHT',TO_DATE('2021-08-13','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(12,2315,116,'RIGHT',TO_DATE('2021-08-14','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(13,2315,117,'WRONG',TO_DATE('2021-08-15','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(14,3214,112,'WRONG',TO_DATE('2021-08-16','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(15,3214,113,'WRONG',TO_DATE('2021-08-18','YYYY-MM-DD'));
INSERT INTO QUESTION_PRACTICE_DETAIL VALUES(16,6543,111,'RIGHT',TO_DATE('2021-08-13','YYYY-MM-DD'));
INSERT INTO QUESTION_DETAIL VALUES(1,111,'HARD');
INSERT INTO QUESTION_DETAIL VALUES(2,112,'MEDIUM');
INSERT INTO QUESTION_DETAIL VALUES(3,113,'EASY');
INSERT INTO QUESTION_DETAIL VALUES(4,115,'EASY');
INSERT INTO QUESTION_DETAIL VALUES(5,116,'MEDIUM');
INSERT INTO QUESTION_DETAIL VALUES(6,117,'EASY');
COMMIT;
SELECT * FROM QUESTION_DETAIL;

SELECT * FROM QUESTION_PRACTICE_DETAIL;

 

SELECT * FROM USER_PROFILE;

题目1:

现在运营想要查看用户在某天刷题后第二天还会再来刷题的平均概率, 请你取出相应数据。你的查询应返回以下结果:

avg_ret
0.3000

题目2:

现在运营想要了解复旦大学的每个用户在8月份练习的总题目数和回答正确的题目数情况,

请取出相应明细数据,对于在8月份没有练习过的用户,答题数结果返回0.

根据示例,你的查询应返回以下结果:

device_id

university

question_cnt

right_question_cnt

3214

复旦大学

3

0

4321

复旦大学

0

0

题目3:

现在运营想要了解浙江大学的用户在不同难度题目下答题的正确率情况,请取出相应数据,并按照准确率升序输出。

根据示例,你的查询应返回以下结果:

difficult_level

correct_rate

easy

0.5000

medium

1.0000

抖音

摘自牛客网

DROP TABLE TB_USER_VIDEO_LOG PURGE;
DROP TABLE TB_VIDEO_INFO PURGE;


CREATE TABLE TB_USER_VIDEO_LOG (
    ID NUMBER(10) PRIMARY KEY,
    USERID NUMBER(10) NOT NULL,
    VIDEO_ID NUMBER(10) NOT NULL,
    START_TIME DATE,
    END_TIME DATE,
    IF_FOLLOW NUMBER(10),
    IF_LIKE NUMBER(10),
    IF_RETWEET NUMBER(10),
    COMMENT_ID NUMBER(10)
);

CREATE TABLE TB_VIDEO_INFO (
    ID NUMBER(10),
    VIDEO_ID NUMBER(10),
    AUTHOR NUMBER(10) NOT NULL,
    TAG VARCHAR2(16) NOT NULL,
    DURATION NUMBER(10) NOT NULL,
    RELEASE_TIME DATE NOT NULL,
    CONSTRAINT TVI_PK PRIMARY KEY (ID,VIDEO_ID)
);


INSERT INTO TB_USER_VIDEO_LOG
VALUES (1, 101, 2001, TO_DATE('2021-09-24 10:00:00', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-09-24 10:00:30', 'YYYY-MM-DD HH24:MI:SS'), 1, 1, 1, NULL);
INSERT INTO TB_USER_VIDEO_LOG
VALUES (2, 101, 2001, TO_DATE('2021-10-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-01 10:00:31', 'YYYY-MM-DD HH24:MI:SS'), 1, 1, 0, NULL);
INSERT INTO TB_USER_VIDEO_LOG
VALUES (3, 102, 2001, TO_DATE('2021-10-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-01 10:00:35', 'YYYY-MM-DD HH24:MI:SS'), 0, 0, 1, NULL);
INSERT INTO TB_USER_VIDEO_LOG
VALUES (4, 103, 2001, TO_DATE('2021-10-03 11:00:50', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-03 11:01:35', 'YYYY-MM-DD HH24:MI:SS'), 1, 1, 0, 1732526);
INSERT INTO TB_USER_VIDEO_LOG
VALUES (5, 106, 2002, TO_DATE('2021-10-02 10:59:05', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-02 11:00:04', 'YYYY-MM-DD HH24:MI:SS'), 2, 0, 1, NULL);
INSERT INTO TB_USER_VIDEO_LOG
VALUES (6, 107, 2002, TO_DATE('2021-10-02 10:59:05', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-02 11:00:06', 'YYYY-MM-DD HH24:MI:SS'), 1, 0, 0, NULL);
INSERT INTO TB_USER_VIDEO_LOG
VALUES (7, 108, 2002, TO_DATE('2021-10-02 10:59:05', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-02 11:00:05', 'YYYY-MM-DD HH24:MI:SS'), 1, 1, 1, NULL);
INSERT INTO TB_USER_VIDEO_LOG
VALUES (8, 109, 2002, TO_DATE('2021-10-03 10:59:05', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-03 11:00:01', 'YYYY-MM-DD HH24:MI:SS'), 0, 1, 0, NULL);
INSERT INTO TB_USER_VIDEO_LOG
VALUES (9, 105, 2002, TO_DATE('2021-09-25 11:00:00', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-09-25 11:00:30', 'YYYY-MM-DD HH24:MI:SS'), 1, 0, 1, NULL);
INSERT INTO TB_USER_VIDEO_LOG
VALUES (10, 101, 2003, TO_DATE('2021-09-26 11:00:00', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-09-26 11:00:30', 'YYYY-MM-DD HH24:MI:SS'), 1, 0, 0, NULL);
INSERT INTO TB_USER_VIDEO_LOG
VALUES (11, 101, 2003, TO_DATE('2021-09-30 11:00:00', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-09-30 11:00:30', 'YYYY-MM-DD HH24:MI:SS'), 1, 1, 0, NULL);

INSERT INTO TB_VIDEO_INFO
VALUES (1, 2001, 901, '旅游', 30, TO_DATE('2021-09-05 7:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO TB_VIDEO_INFO
VALUES (2, 2002, 901, '旅游', 60, TO_DATE('2021-09-05 7:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO TB_VIDEO_INFO
VALUES (3, 2003, 902, '影视', 90, TO_DATE('2021-09-05 7:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO TB_VIDEO_INFO
VALUES (4, 2004, 902, '影视', 90, TO_DATE('2021-09-05 8:00:00', 'YYYY-MM-DD HH24:MI:SS'));

COMMIT;
SELECT * FROM TB_USER_VIDEO_LOG;

SELECT * FROM TB_VIDEO_INFO;

问题:找出近一个月发布的视频中热度TOP3的视频

注:

热度=(a*视频完播率+b*点赞数+c*评论数+d*转发数)*新鲜度;

新鲜度=1/(最近无播放天数+1);

当前配置的参数a,b,c,d分别为100、5、3、2;

最近播放日期以end_time-结束观看时间为准,假设为T,则最近一个月按[T-29, T]闭区间统计;

结果中热度保留为整数,并按热度降序排序.你的最终结果应该是这样的:

video_idhot_index
2001122
200256
20031

百度

摘自牛客网


DROP TABLE TB_USER_LOG PURGE;


CREATE TABLE TB_USER_LOG
(   ID         NUMBER(10) GENERATED AS IDENTITY START WITH 1 INCREMENT BY 1 PRIMARY KEY,
    USERID     NUMBER(10) NOT NULL,
    ARTICLE_ID NUMBER(10) NOT NULL,
    IN_TIME    DATE,
    OUT_TIME   DATE,
    SIGN_IN    NUMBER(10) DEFAULT 0);

INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (101, 0, TO_DATE('2021-07-07 10:00:00', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-07-07 10:00:09', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (101, 0, TO_DATE('2021-07-08 10:00:00', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-07-08 10:00:09', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (101, 0, TO_DATE('2021-07-09 10:00:00', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-07-09 10:00:42', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (101, 0, TO_DATE('2021-07-10 10:00:00', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-07-10 10:00:09', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (101, 0, TO_DATE('2021-07-11 23:59:55', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-07-11 23:59:59', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (101, 0, TO_DATE('2021-07-12 10:00:28', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-07-12 10:00:50', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (101, 0, TO_DATE('2021-07-13 10:00:28', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-07-13 10:00:50', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (102, 0, TO_DATE('2021-10-01 10:00:28', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-01 10:00:50', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (102, 0, TO_DATE('2021-10-02 10:00:01', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-02 10:01:50', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (102, 0, TO_DATE('2021-10-03 11:00:55', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-03 11:00:59', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (102, 0, TO_DATE('2021-10-04 11:00:45', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-04 11:00:55', 'YYYY-MM-DD HH24:MI:SS'), 0);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (102, 0, TO_DATE('2021-10-05 11:00:53', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-05 11:00:59', 'YYYY-MM-DD HH24:MI:SS'), 1);
INSERT INTO SCOTT.TB_USER_LOG (USERID, ARTICLE_ID, IN_TIME, OUT_TIME, SIGN_IN)
VALUES (102, 0, TO_DATE('2021-10-06 11:00:45', 'YYYY-MM-DD HH24:MI:SS'),
        TO_DATE('2021-10-06 11:00:55', 'YYYY-MM-DD HH24:MI:SS'), 1);
COMMIT;
SELECT * FROM TB_USER_LOG;

题目:

计算每个用户2021年7月以来每月获得的金币数(该活动到10月底结束,11月1日开始的签到不再获得金币)。结果按月份、ID升序排序。

场景逻辑说明:

artical_id-文章ID代表用户浏览的文章的ID, 特殊情况artical_id-文章ID为0表示用户在非文章内容页

(比如App内的列表页、活动页等)。注意:只有artical_id为0时sign_in值才有效。

从2021年7月7日0点开始,用户每天签到可以领1金币, 并可以开始累积签到天数,

连续签到的第3、7天分别可额外领2、6金币。

每连续签到7天后重新累积签到天数(即重置签到天数:连续第8天签到时记为新的一轮签到的第一天, 领1金币)

注: 如果签到记录的in_time-进入时间和out_time-离开时间跨天了, 也只记作in_time对应的日期签到了。

你的最终结果应该是这样的:

uidmonthcoin
10120210715
1022021107

       

京东

摘自牛客网


DROP TABLE TB_ORDER_OVERALL PURGE;
DROP TABLE TB_PRODUCT_INFO PURGE;
DROP TABLE TB_ORDER_DETAIL PURGE;


CREATE TABLE TB_ORDER_OVERALL
(   ID           NUMBER(10) GENERATED AS IDENTITY START WITH 1 INCREMENT BY 1 PRIMARY KEY,
    ORDER_ID     NUMBER(10)    NOT NULL,
    USERID       NUMBER(10)    NOT NULL,
    EVENT_TIME   DATE,
    TOTAL_AMOUNT NUMBER(10, 4) NOT NULL,
    TOTAL_CNT    NUMBER(10)    NOT NULL,
    STAT         NUMBER(10)    NOT NULL);

CREATE TABLE TB_PRODUCT_INFO
(   ID           NUMBER(10) GENERATED AS IDENTITY START WITH 1 INCREMENT BY 1 PRIMARY KEY,
    PRODUCT_ID   NUMBER(10)    NOT NULL,
    SHOP_ID      NUMBER(10)    NOT NULL,
    TAG          VARCHAR(12),
    IN_PRICE     NUMBER(10, 4) NOT NULL,
    QUANTITY     NUMBER(10)    NOT NULL,
    RELEASE_TIME DATE);

CREATE TABLE TB_ORDER_DETAIL
(   ID         NUMBER(10) GENERATED AS IDENTITY START WITH 1 INCREMENT BY 1 PRIMARY KEY,
    ORDER_ID   NUMBER(10)    NOT NULL,
    PRODUCT_ID NUMBER(10)    NOT NULL,
    PRICE      NUMBER(10, 4) NOT NULL,
    CNT        NUMBER(10)    NOT NULL);


INSERT INTO TB_ORDER_DETAIL (ORDER_ID, PRODUCT_ID, PRICE, CNT)
VALUES (301004, 8002, 180.0000, 1);
INSERT INTO TB_ORDER_DETAIL (ORDER_ID, PRODUCT_ID, PRICE, CNT)
VALUES (301005, 8002, 170.0000, 1);
INSERT INTO TB_ORDER_DETAIL (ORDER_ID, PRODUCT_ID, PRICE, CNT)
VALUES (301002, 8001, 85.0000, 1);
INSERT INTO TB_ORDER_DETAIL (ORDER_ID, PRODUCT_ID, PRICE, CNT)
VALUES (301002, 8003, 180.0000, 1);
INSERT INTO TB_ORDER_DETAIL (ORDER_ID, PRODUCT_ID, PRICE, CNT)
VALUES (301003, 8002, 150.0000, 1);
INSERT INTO TB_ORDER_DETAIL (ORDER_ID, PRODUCT_ID, PRICE, CNT)
VALUES (301003, 8003, 180.0000, 1);

INSERT INTO TB_ORDER_OVERALL (ORDER_ID, USERID, EVENT_TIME, TOTAL_AMOUNT, TOTAL_CNT, STAT)
VALUES (301004, 102, TO_DATE('2021-09-30 10:00:00', 'YYYY-MM-DD HH24:MI:SS'), 170.0000, 1, 1);
INSERT INTO TB_ORDER_OVERALL (ORDER_ID, USERID, EVENT_TIME, TOTAL_AMOUNT, TOTAL_CNT, STAT)
VALUES (301005, 104, TO_DATE('2021-10-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'), 160.0000, 1, 1);
INSERT INTO TB_ORDER_OVERALL (ORDER_ID, USERID, EVENT_TIME, TOTAL_AMOUNT, TOTAL_CNT, STAT)
VALUES (301003, 101, TO_DATE('2021-10-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS'), 300.0000, 2, 1);
INSERT INTO TB_ORDER_OVERALL (ORDER_ID, USERID, EVENT_TIME, TOTAL_AMOUNT, TOTAL_CNT, STAT)
VALUES (301002, 102, TO_DATE('2021-10-03 11:00:00', 'YYYY-MM-DD HH24:MI:SS'), 235.0000, 2, 1);

INSERT INTO TB_PRODUCT_INFO (PRODUCT_ID, SHOP_ID, TAG, IN_PRICE, QUANTITY, RELEASE_TIME)
VALUES (8001, 901, '日用', 60.0000, 1000, TO_DATE('2020-01-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO TB_PRODUCT_INFO (PRODUCT_ID, SHOP_ID, TAG, IN_PRICE, QUANTITY, RELEASE_TIME)
VALUES (8002, 901, '零食', 140.0000, 500, TO_DATE('2020-01-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO TB_PRODUCT_INFO (PRODUCT_ID, SHOP_ID, TAG, IN_PRICE, QUANTITY, RELEASE_TIME)
VALUES (8003, 901, '零食', 160.0000, 500, TO_DATE('2020-01-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS'));

COMMIT;
SELECT * FROM TB_PRODUCT_INFO;

SELECT * FROM TB_ORDER_DETAIL;

SELECT * FROM TB_ORDER_OVERALL;

题目: 请计算店铺901在2021年国庆头3天的7日动销率和滞销率,结果保留3位小数,按日期升序排序。

注: 动销率定义为店铺中一段时间内有销量的商品占当前已上架总商品数的比例(有销量的商品/已上架总商品数)。

滞销率定义为店铺中一段时间内没有销量的商品占当前已上架总商品数的比例。(没有销量的商品/已上架总商品数)。

只要当天任一店铺有任何商品的销量就输出该天的结果,即使店铺901当天的动销率为0。

你的最终结果应该是这样的:

dtsale_rateunsale_rate
2021-10-010.3330.667
2021-10-020.6670.333
2021-10-031.0000.000

波峰波谷

DROP TABLE BFBG PURGE;

CREATE TABLE BFBG (U_ID NUMBER(2),DT DATE, PRICE NUMBER(10,2));

INSERT INTO BFBG VALUES (1,TO_DATE('20140601','YYYYMMDD'),-20);
INSERT INTO BFBG VALUES (1,TO_DATE('20140602','YYYYMMDD'),50);
INSERT INTO BFBG VALUES (1,TO_DATE('20140604','YYYYMMDD'),15);
INSERT INTO BFBG VALUES (1,TO_DATE('20140605','YYYYMMDD'),13);
INSERT INTO BFBG VALUES (1,TO_DATE('20140606','YYYYMMDD'),100);
INSERT INTO BFBG VALUES (2,TO_DATE('20140602','YYYYMMDD'),33);
INSERT INTO BFBG VALUES (2,TO_DATE('20140605','YYYYMMDD'),66);
INSERT INTO BFBG VALUES (2,TO_DATE('20140606','YYYYMMDD'),101);
INSERT INTO BFBG VALUES (2,TO_DATE('20140607','YYYYMMDD'),26);
INSERT INTO BFBG VALUES (2,TO_DATE('20140608','YYYYMMDD'),30);
INSERT INTO BFBG VALUES (2,TO_DATE('20140610','YYYYMMDD'),43);

COMMIT;
select * from BFBG;

题目:

列出波峰波谷, (无需区分波峰或波谷).

你的最终结果应该是这样的:

电影院连续座位问题

DROP TABLE SEAT PURGE;

DROP TABLE SEAT;
CREATE TABLE SEAT
(   ID   NUMBER(4) GENERATED AS IDENTITY START WITH 1 INCREMENT BY 1 UNIQUE,
    FREE NUMBER(1));

INSERT INTO SEAT(FREE) VALUES(0);
INSERT INTO SEAT(FREE) VALUES(0);
INSERT INTO SEAT(FREE) VALUES(1);
INSERT INTO SEAT(FREE) VALUES(1);
INSERT INTO SEAT(FREE) VALUES(1);
INSERT INTO SEAT(FREE) VALUES(0);
INSERT INTO SEAT(FREE) VALUES(0);
INSERT INTO SEAT(FREE) VALUES(1);
INSERT INTO SEAT(FREE) VALUES(1);
INSERT INTO SEAT(FREE) VALUES(0);
INSERT INTO SEAT(FREE) VALUES(0);
INSERT INTO SEAT(FREE) VALUES(0);
INSERT INTO SEAT(FREE) VALUES(1);
INSERT INTO SEAT(FREE) VALUES(1);
INSERT INTO SEAT(FREE) VALUES(1);
INSERT INTO SEAT(FREE) VALUES(1);
INSERT INTO SEAT(FREE) VALUES(0);
INSERT INTO SEAT(FREE) VALUES(1);
INSERT INTO SEAT(FREE) VALUES(0);
INSERT INTO SEAT(FREE) VALUES(1);
COMMIT;
SELECT * FROM SEAT;

题目:

列出所有连续的座位。你的最终结果应该是这样的:

学生互换座位

DROP TABLE SEAT PURGE;

CREATE TABLE SEAT
(ID NUMBER(20),
STUD VARCHAR2(20));

INSERT INTO SEAT VALUES(1,'Tom');
INSERT INTO SEAT VALUES(2,'Jerry');
INSERT INTO SEAT VALUES(3,'SpongeBOB');
INSERT INTO SEAT VALUES(4,'Green');
INSERT INTO SEAT VALUES(5,'Black');
COMMIT;
SELECT * FROM SEAT;

题目: 给同学们互换座位 (如果学生总数是奇数的话, 最后一个学生的座位就不需要调整)。

你的最终结果应该是这样的:

 按月统计收入

DROP TABLE SALES;

CREATE TABLE SALES
(   ID     NUMBER(2) generated as identity START WITH 1 INCREMENT BY 1,
    MONTH  NUMBER(2),
    DEP    NUMBER(2),
    INCOME NUMBER(3));

INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (1, 1, 100);
INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (1, 1, 110);
INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (1, 2, 150);
INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (1, 2, 160);
INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (1, 3, 200);
INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (2, 1, 160);
INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (2, 2, 140);
INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (2, 2, 150);
INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (2, 3, 120);
INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (3, 1, 170);
INSERT INTO SALES (MONTH, DEP, INCOME) VALUES (3, 2, 200);
COMMIT;
SELECT * FROM SALES;

题目: 你的最终结果应该是这样的:

如何实现?

球队连续夺冠问题

DROP TABLE TEAM;

CREATE TABLE TEAM
(   T VARCHAR2(20),
    Y NUMBER(4));

INSERT INTO TEAM VALUES ('火花塞', 1990);
INSERT INTO TEAM VALUES ('母牛', 1991);
INSERT INTO TEAM VALUES ('母牛', 1992);
INSERT INTO TEAM VALUES ('母牛', 1993);
INSERT INTO TEAM VALUES ('导弹', 1994);
INSERT INTO TEAM VALUES ('导弹', 1995);
INSERT INTO TEAM VALUES ('母牛', 1996);
INSERT INTO TEAM VALUES ('母牛', 1997);
INSERT INTO TEAM VALUES ('母牛', 1998);
INSERT INTO TEAM VALUES ('马蜂刺', 1999);
INSERT INTO TEAM VALUES ('河人', 2000);
INSERT INTO TEAM VALUES ('河人', 2001);
INSERT INTO TEAM VALUES ('河人', 2002);
INSERT INTO TEAM VALUES ('马蜂刺', 2003);
INSERT INTO TEAM VALUES ('火花塞', 2004);
INSERT INTO TEAM VALUES ('马蜂刺', 2005);
INSERT INTO TEAM VALUES ('冰冷', 2006);

COMMIT;
SELECT * FROM TEAM;

题目:

列出连冠的球队, 连续夺冠的第一年及最后一年。

你的最终结果应该是这样的:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值