- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个结果集,为简单起见,我将其称为包含三列的表“选项卡”:类别、子类别和日期,按类别和日期排序。该数据集是一个网格,我希望在该网格之上执行其他处理。我的问题是唯一标识(或顺序标记)数据集中的组。基于前 3 列的存在,下面的 SQL 是我所追求的(GID1 或 GID2 都可以)。我已经尝试过 group_id、grouping_id、rank、dense_rank,要么错过了其中一个技巧,要么正在尝试一些非常尴尬的事情。 GID 的顺序并不重要,但重要的是组号分配基于排序的数据(类别然后是日期)。
CREATE TABLE Tab
("Category" varchar2(1), "SubCategory" varchar2(7), "Date" int, "GID1" int, "GID2" int);
INSERT ALL
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'bannana', 20120101, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'grape', 20120102, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'pear', 20120103, 1, 1)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'pear', 20120104, 1, 1)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'bannana', 20120105, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'pear', 20120106, 2, 2)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'pear', 20120107, 2, 2)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'apple', 20120108, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('A', 'pear', 20120109, 3, 3)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'apple', 20120101, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'bannana', 20120102, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'apple', 20120103, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'bannana', 20120104, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'pear', 20120105, 1, 4)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'pear', 20120106, 1, 4)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'pear', 20120107, 1, 4)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'pear', 20120108, 1, 4)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('B', 'pear', 20120109, 1, 4)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'grape', 20120101, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'grape', 20120102, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'apple', 20120103, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'bannana', 20120104, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'grape', 20120105, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'pear', 20120106, 1, 5)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'apple', 20120107, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'apple', 20120108, NULL, NULL)
INTO Tab ("Category", "SubCategory", "Date", "GID1", "GID2")
VALUES ('C', 'apple', 20120109, NULL, NULL)
SELECT * FROM dual
;
最佳答案
好吧,如果它只是梨的话:
SQL> select "Category", "SubCategory", "Date",
2 case
3 when "SubCategory" = 'pear'
4 then
5 count(rn) over (partition by "Category" order by "Date") else null
6 end GID1 ,
7 case
8 when "SubCategory" = 'pear'
9 then
10 count(rn) over ( order by "Category", "Date") else null
11 end GID2
12 from (select "Category", "SubCategory", "Date", lag("SubCategory") over (partition by "Category" order by "Date"),
13 case
14 when lag("SubCategory") over (partition by "Category" order by "Date") != "SubCategory"
15 and "SubCategory" = 'pear'
16 then 1
17 when row_number() over (partition by "Category" order by "Date") = 1 and "SubCategory" = 'pear' then 1
18 else null
19 end rn
20 from tab)
21 order by 1, 3;
Category SubCate Date GID1 GID2
---------- ------- ---------- ---------- ----------
A bannana 20120101
A grape 20120102
A pear 20120103 1 1
A pear 20120104 1 1
A bannana 20120105
A pear 20120106 2 2
A pear 20120107 2 2
A apple 20120108
A pear 20120109 3 3
B apple 20120101
B bannana 20120102
B apple 20120103
B bannana 20120104
B pear 20120105 1 4
B pear 20120106 1 4
B pear 20120107 1 4
B pear 20120108 1 4
B pear 20120109 1 4
C grape 20120101
C grape 20120102
C apple 20120103
C bannana 20120104
C grape 20120105
C pear 20120106 1 5
C apple 20120107
C apple 20120108
C apple 20120109
打破这个。
我们查看按“日期”排序的前一行(对于每个“类别”),看看它是否是不同的“子类别”以及当前类别 = pear。如果是这样,我们用“1”标记该行(与我们使用的内容无关,只是 NON NULL)。
lag("SubCategory") over (partition by "Category" order by "Date") != "SubCategory"
and "SubCategory" = 'pear'
同样对于第一行,我们分配相同的。这给了我们:
Category SubCate Date LAG("SU RN
---------- ------- ---------- ------- ----------
A bannana 20120101
A grape 20120102 bannana
A pear 20120103 grape 1
A pear 20120104 pear
A bannana 20120105 pear
A pear 20120106 bannana 1
A pear 20120107 pear
A apple 20120108 pear
A pear 20120109 apple 1
B apple 20120101
B bannana 20120102 apple
B apple 20120103 bannana
B bannana 20120104 apple
B pear 20120105 bannana 1
B pear 20120106 pear
B pear 20120107 pear
B pear 20120108 pear
B pear 20120109 pear
C grape 20120101
C grape 20120102 grape
C apple 20120103 grape
C bannana 20120104 apple
C grape 20120105 bannana
C pear 20120106 grape 1
C apple 20120107 pear
C apple 20120108 apple
C apple 20120109 apple
现在,我们简单地计算()非空“RN”值在日期上再次排序(GID1 的每个类别,而不是 GID2 [gid2 我们也按它排序!)。这是这些行:count(rn) over(按“类别”分区,按“日期”排序)
(GID1)
和count(rn) 超过(按“类别”、“日期”排序)
(GID2)
关于sql - Oracle SQL 顺序组号赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13574407/
你能解释一下这个作业是如何完成的吗, var fe, f = document.forms[0], h; 哪个等于哪个。 最佳答案 以上等同于 var fe; var f = document.for
据我测试,这两种方法都有效,但我不知道哪一种最好,也不知道它们之间的区别,这就是我想知道的。 以下是两种方法: window.location = 'http://www.google.com'; w
我正在处理用字符串填充的 numpy 数组。我的目标是分配给第一个数组 a 的切片,值包含在较小尺寸的第二个数组 b 中。 我想到的实现如下: import numpy as np a = np.em
在我使用过的其他语言(如 Erlang 和 Python)中,如果我正在拆分字符串并且不关心其中一个字段,我可以使用下划线占位符。我在 Perl 中试过这个: (_,$id) = split('
我认为这似乎很简单,但我对调用、应用、绑定(bind)感到困惑。等等 我有一个事件监听器 red.addEventListener("click", function() { j = 0;
这个问题在这里已经有了答案: What is the python "with" statement designed for? (11 个答案) 关闭 7 年前。 使用有什么区别: iFile =
这个问题在这里已经有了答案: What is the python "with" statement designed for? (11 个答案) 关闭 7 年前。 使用有什么区别: iFile =
几周前我们开始写一篇关于 Haskell 的论文,刚刚接到我们的第一个任务。我知道 SO 不喜欢家庭作业问题,所以我不会问怎么做。相反,如果有人能将我推向正确的方向,我将不胜感激。鉴于它可能不是一个特
我正在尝试为我的函数的变量根分配一个值,但似乎不起作用。我不明白这个问题。 hw7.c:155:7:警告:赋值使指针来自整数而不进行强制转换[默认启用] root = 负载(&fp, 大小); 此代码
我昨天花了大约 5 个小时来完成这个工作,并使用这个网站的帮助让代码可以工作,但我认为我这样做的方式是一种作弊方式,我使用了 scanf 命令。无论如何,我想以正确的方式解决这个问题。多谢你们!哦,代
我需要一些帮助来解决问题。 我有这个文本文件: 我将文本内容输入到字符串二维数组中,并将其转换为整数二维数组。当我转换为 int 数组时,nan 被替换为零。现在,我继续查找二维数组中每行的最大值和最
假设我有一个只能移动的类型。我们停止现有的默认提供的构造函数,但 Rvalue 引用引入了一种新的“ flavor ”,我们可以将其用于签名的移动版本: class CantCopyMe { priv
假设我有两个简单的对象,我想创建第三个对象来连接它们的属性。这非常有效: (()=>{ const a1 = {a: 2, b: 3} const b1 = {a: 100, c: 5}
我想知道我是否可以稍后在这样的代码中为 VAR 赋值 var myView: UIView func createView() { myView = UIView() { let _view =
我遇到了一些 Javascript/HTML/CSS 代码的问题。我对创建网站还很陌生,所以请多多包涵。 我最终想做的是从 javascript 中提取一个动态值并使用它对一些 div(在容器中)进行
#include class Box{ public: int x; Box(){ x=0; std::cout No move construction thanks to RV
我发现在javascript中&=运算符是按位赋值: var test=true; test&=true; //here test is an int variable javascript中是否存在
请帮助完成赋值重载函数的执行。 这是指令: 赋值运算符 (=),它将源字符串复制到目标字符串中。请注意,目标的大小需要调整为与源相同。 加法 (+) 和赋值 (=) 运算符都需要能够进行级联运算。这意
我有一个名为 SortedArrayList 的自定义结构它根据比较器对其元素进行排序,我想防止使用 operator[] 进行分配. 示例: 数组列表.h template class Array
我是 python 的新手,我看到了这种为列表赋值的形式 color= ['red' if v == 0 else 'green' for v in y] 但是如果我尝试用 3 个数字来做,例如 co
我是一名优秀的程序员,十分优秀!