- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个设施/节点表,以及一个带有简单查询/连接的 list 表,以显示当前打开的 list (具有空的 CLOSE_DATE 字段)。
节点可能有也可能没有检查表,只有一个打开的检查表,多个关闭的检查表。相当标准的东西。
但现在我还需要显示最近创建但已关闭的 list ,并且希望在开始将其集成到实际代码库之前检查我的解决方案的健全性。
注意:Oracle SQL
CREATE TABLE test_user (user_id number, user_name varchar2(16));
CREATE TABLE test_node (node_id number, node_name varchar2(16));
CREATE TABLE test_list (list_id number, node_id number, user_id number, create_date date, close_date date);
INSERT INTO test_user VALUES (1, 'apple');
INSERT INTO test_user VALUES (2, 'pear');
INSERT INTO test_node VALUES (1, 'facility 1');
INSERT INTO test_node VALUES (2, 'facility 2');
INSERT INTO test_node VALUES (3, 'facility 3');
INSERT INTO test_node VALUES (4, 'facility 4');
INSERT INTO test_list VALUES (1, 1, 1, '1-jan-2015', NULL);
INSERT INTO test_list VALUES (2, 2, 1, '1-jan-2015', '1-feb-2015');
INSERT INTO test_list VALUES (3, 2, 1, '1-apr-2015', '1-jun-2015');
INSERT INTO test_list VALUES (4, 2, 2, '1-mar-2015', '2-mar-2015');
INSERT INTO test_list VALUES (5, 2, 1, '1-nov-2015', NULL);
INSERT INTO test_list VALUES (6, 4, 2, '1-nov-2015', '15-nov-2015');
用户表主要是其中的一部分,以使左连接更复杂一些以模拟实际数据库。
我正在尝试创建一个 SQL,它将返回每个节点/设施、关于其当前打开的 list 的信息(如果有的话)以及关于最近关闭的 list 的信息(如果有的话)。
我最初尝试解决我的问题:
WITH list_breakout_open AS (
SELECT ll.list_id, ll.node_id, ll.create_date,
ll.user_id, uu.user_name
FROM test_list ll
JOIN test_user uu ON ll.user_id = uu.user_id
WHERE ll.close_date IS NULL
), list_breakout_close AS (
SELECT ll.list_id, ll.node_id, ll.create_date, ll.close_date,
ll.user_id, uu.user_name,
RANK() OVER (PARTITION BY ll.node_id ORDER BY ll.create_date DESC) AS close_rank
FROM test_list ll
JOIN test_user uu ON ll.user_id = uu.user_id
WHERE ll.close_date IS NOT NULL
)
SELECT nn.node_id, nn.node_name,
lbo.list_id AS open_list_id, lbo.user_name AS open_user_name, lbo.create_date AS open_create_date,
lbc.list_id AS close_list_id, lbc.user_name AS close_user_name, lbc.create_date AS close_create_date, lbc.close_date AS close_close_date
FROM test_node nn
LEFT JOIN list_breakout_open lbo ON nn.node_id = lbo.node_id
LEFT JOIN list_breakout_close lbc ON nn.node_id = lbc.node_id
AND close_rank = 1;
表格永远不会变得很大(几千行,而不是几百万),所以性能不是大问题。我最关心的是代码维护,想要一个干净的查询,当有人在 5 年后查看这个烂摊子时会有意义。
我的解决方案看起来合理吗?我是否遗漏了一些明显的东西,以后可能会误导我??
编辑:添加了关于我的最终查询应该做什么的注释。
最佳答案
您可以扫描每个表一次并使用分析函数进行聚合,如下所示:
SQL> SELECT n.node_id,
2 n.node_name,
3 min(case when l.close_date is null then l.list_id end) keep (dense_rank first order by nvl2(l.close_date, null, l.create_date) desc nulls last) open_list_id,
4 min(case when l.close_date is null then u.user_name end) keep (dense_rank first order by nvl2(l.close_date, null, l.create_date) desc nulls last) open_user_name,
5 min(case when l.close_date is null then l.create_date end) keep (dense_rank first order by nvl2(l.close_date, null, l.create_date) desc nulls last) open_create_date,
6 min(case when l.close_date is not null then l.list_id end) keep (dense_rank first order by nvl2(l.close_date, l.create_date, null) desc nulls last) close_list_id,
7 min(case when l.close_date is not null then u.user_name end) keep (dense_rank first order by nvl2(l.close_date, l.create_date, null) desc nulls last) close_user_name,
8 min(case when l.close_date is not null then l.create_date end) keep (dense_rank first order by nvl2(l.close_date, l.create_date, null) desc nulls last) close_create_date,
9 min(case when l.close_date is not null then l.close_date end) keep (dense_rank first order by nvl2(l.close_date, l.create_date, null) desc nulls last) close_close_date
10 FROM test_node n
11 LEFT JOIN test_list l ON l.node_id = n.node_id
12 LEFT JOIN test_user u ON u.user_id = l.user_id
13 GROUP BY n.node_id, n.node_name
14 ORDER BY node_id;
NODE_ID NODE_NAME OPEN_LIST_ID OPEN_USER_NAME OPEN_CREATE_DATE CLOSE_LIST_ID CLOSE_USER_NAME CLOSE_CREATE_DATE CLOSE_CLOSE_DATE
---------- ---------------- ------------ ---------------- ---------------- ------------- ---------------- ----------------- ----------------
1 facility 1 1 apple 01/01/2015
2 facility 2 5 apple 01/11/2015 3 apple 01/04/2015 01/06/2015
3 facility 3
4 facility 4 6 pear 01/11/2015 15/11/2015
不完全确定它是否像其他人提到的那样是正确的输出。我使用您发布的解决方案对其进行了验证。
关于sql - Oracle SQL 健全性检查 - 加入以获取最近创建的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34028387/
我需要获取过去 24 小时的记录,但不能像这样按小时分组: SELECT HOUR(CompDate) AS hour, COUNT(1) AS action FROM mytable WHERE (
我们有一个自动完成列表,当您向某人发送电子邮件时会填充该列表,这一切都很好,直到列表变得非常大,您需要输入越来越多的地址才能找到您想要的地址, 这违背了自动完成的目的 我在想应该添加一些逻辑,以便自动
我在 android 的锁屏上工作我们如何禁用导航软按钮,已经尝试了所有方法,systemoverlay但它不起作用,在按下主页按钮时它会终止服务和 Activity 。 最佳答案 后退按钮可以通过覆
我有一个报告创建时间为 2016-05-30,现在我需要从报告时间开始的最后 7 天。我怎样才能使用时刻? report_create_time = moment('2016-05-30').form
我想找出向量中最接近的三个数字。 就像是 v = c(10,23,25,26,38,50) c = findClosest(v,3) c 23 25 26 我试过 sort(colSums(as.ma
考虑以下表结构: id speed 1 100 2 200 3 300 4 400 5 500 考虑以下查询:"SELECT * FROM records WHERE
我正在开发一个依赖 YouTube 直播和实时聊天(也来自 YouTube)的网络应用。事情进展顺利,突然嵌入的聊天功能无法在移动设备上运行。 我试图在我这边找到一个错误或一些无效的配置,但我找不到。
我正在制作一个 React Native 应用程序,它有一个安全部分,用户必须在其中输入密码才能解锁 protected 内容。问题是,当用户在锁定该部分之前切换到另一个应用程序时,将生成屏幕截图以及
我有一条 SQL 语句 (SQL Server Management Studio),我通过仪表板软件将数据传递到 where 语句中。用户可以选择年份(2013 年或现在的 2014 年)和月份(作
我有一个脚本可以添加一组行,使您能够在 SharePoint 列表表单中捕获其他访问者的信息。我有两个 anchor 标记,一个用于添加,另一个用于删除。当我添加一个新的访问者时它有效,当我删除访问者
我正在学习斯坦福 iOS 类(class),我有一个问题,我认为与最近的更新有关。这是代码部分: func evaluate(ops: [Op]) -> (result: Double?,remain
我注意到我的应用有一个奇怪的行为。每当我按下主页按钮时,我的应用程序就会被杀死。我没有在应用程序堆栈中看到该应用程序。我可以看到之前启动的其他应用程序。最初我怀疑 android:launchMode
我需要获取过去 7 天内的所有付费和临时条目,但我总是收到所有退回的内容。我不确定我做错了什么,我已经阅读了这里的很多帖子,但无法理解它是什么。 MySQL 5.6(如果它与我一直在做的事情有什么不同
我的表有一列以 mysql time() 格式格式化。 当它是一个值分配给名为 $preRemainOt 的 php 变量时我想重新安排到最近的 15 分钟 function roundTime($w
我想获取过去 7 天内每天每个产品的最后时间戳。数据库中有数千条记录。我怎样才能通过查询来做到这一点。大约有 25 种不同的产品,每种产品每天大约有 50 个时间戳。 表:构建数据 'Timesta
我现在的代码正在从 SQL 中获取移动应用程序中的数据,首先添加显示,我需要将其设置为在我的 Android 应用程序中显示最后添加的第一个。我有如下所示的 api 代码,最新的是根据我的要求显示的,
我有一张 table ,说 table 的描述为: | ID | SNO | c1 | c2 | c3 | ___________________________________ |
我有两个大小相等的向量,例如 A=[2.29 2.56 2.77 2.90 2.05] and B=[2.34 2.62 2.67 2.44 2.52]. 我有兴趣在两个相同大小的向量 A 和 B 中
之前,我在这里发布了一个问题,询问有关如何从驱动器读取和写入数据的建议,而不是通过像“aaa.txt”这样的文件标签,而只是扇区..我被建议尝试阅读和写作....但新问题出现了……毛茸茸的参数 int
我想删除在给定时间段内未登录的用户,但我稍后会根据结果选择时间段。 所以我需要报告,其中我将收到过去 1 个月、2 个月...... n 个月内未登录的用户数量。 我不太清楚如何在单个 mysql 查
我是一名优秀的程序员,十分优秀!