当前在线人数13786
首页 - 分类讨论区 - 电脑网络 - 数据库版 -阅读文章
未名交友
[更多]
[更多]
文章阅读:Re: 出个更难的sql题
[同主题阅读] [版面: 数据库] [作者:nmamtf] , 2019年11月07日11:31:56
nmamtf
进入未名形象秀
我的博客
[上篇] [下篇] [同主题上篇] [同主题下篇]

发信人: nmamtf (nmamtf), 信区: Database
标  题: Re: 出个更难的sql题
发信站: BBS 未名空间站 (Thu Nov  7 11:31:56 2019, 美东)

CREATE TABLE THEMATRIXDATA
(
    PRODUCT    VARCHAR2 (1),
    SEQ        INTEGER,
    P1         INTEGER,
    P2         INTEGER
);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('A',
             1,
             0,
             5);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('A',
             2,
             0,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('A',
             3,
             3,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('A',
             4,
             3,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('A',
             5,
             0,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('A',
             6,
             0,
             6);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('A',
             7,
             0,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('A',
             8,
             4,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('B',
             1,
             0,
             5);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('B',
             2,
             0,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('B',
             3,
             3,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('B',
             4,
             3,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('B',
             5,
             0,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('B',
             6,
             0,
             6);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('B',
             7,
             0,
             0);

INSERT INTO THEMATRIXDATA (PRODUCT,
                           SEQ,
                           P1,
                           P2)
     VALUES ('B',
             8,
             4,
             0);

COMMIT;



  SELECT PRODUCT,
         SEQ,
         P1,
         P2,
         LAG (P1) OVER (ORDER BY PRODUCT, SEQ)     PRE_P1
    FROM THEMATRIXDATA
ORDER BY PRODUCT, SEQ;


  SELECT PRODUCT,
         SEQ,
         P1,
         P2,
         DECODE (P1,  NULL, -1,  0, -1,  1)        AS P1_FLAG, -- CHECK IF
P1 VALUE IS USABLE
         DECODE (P2,  NULL, -1,  0, -1,  1)        AS P2_FLAG, -- CHECK IF
P2 VALUE IS USABLE
         LAG (P1) OVER (ORDER BY PRODUCT, SEQ)     AS PRE_ROW_P1,
         LAG (P2) OVER (ORDER BY PRODUCT, SEQ)     AS PRE_ROW_P2
    FROM THEMATRIXDATA
ORDER BY PRODUCT, SEQ;



SELECT PRODUCT,
       SEQ,
       P1,
       P2,
       P1_FLAG,
       P2_FLAG,
       PRE_ROW_P1,
       PRE_ROW_P2,
       PRE_ROW_P1_FLAG,
       PRE_ROW_P2_FLAG,
       DECODE (
           P1_FLAG,
           1, P1,
           DECODE (PRE_ROW_P1_FLAG,
                   1, PRE_ROW_P1,
                   LAG (PRE_ROW_P1) OVER (ORDER BY PRODUCT, SEQ)))    AS NEW
_P1
  FROM (  SELECT PRODUCT,
                 SEQ,
                 P1,
                 P2,
                 DECODE (P1,  NULL, -1,  0, -1,  1)
                     AS P1_FLAG,                -- CHECK IF P1 VALUE IS
USABLE
                 DECODE (P2,  NULL, -1,  0, -1,  1)
                     AS P2_FLAG,                -- CHECK IF P2 VALUE IS
USABLE
                 LAG (P1) OVER (ORDER BY PRODUCT, SEQ)
                     AS PRE_ROW_P1,
                 LAG (P2) OVER (ORDER BY PRODUCT, SEQ)
                     AS PRE_ROW_P2,
                 DECODE (LAG (P1) OVER (ORDER BY PRODUCT, SEQ), 0, -1, 1)
                     AS PRE_ROW_P1_FLAG,
                 DECODE (LAG (P2) OVER (ORDER BY PRODUCT, SEQ), 0, -1, 1)
                     AS PRE_ROW_P2_FLAG
            FROM THEMATRIXDATA
        ORDER BY PRODUCT, SEQ);
--
※ 来源:·WWW 未名空间站 网址:mitbbs.com 移动:在应用商店搜索未名空间·[FROM: 152.]

[上篇] [下篇] [同主题上篇] [同主题下篇]
[转寄] [转贴] [回信给作者] [修改文章] [删除文章] [同主题阅读] [从此处展开] [返回版面] [快速返回] [收藏] [举报]
 
回复文章
标题:
内 容:

未名交友
将您的链接放在这儿

友情链接


 

Site Map - Contact Us - Terms and Conditions - Privacy Policy

版权所有,未名空间(mitbbs.com),since 1996