SQL Connect By Level 有时有效,有时不明白为什么
SQL Connect By Level sometimes works, sometimes doesn't can't understand why
我正在尝试 运行 Oracle 中的查询,如果我将轮数更改为 0,我会得到一个结果,但是只要有小数,我在使用 connect by 时就不会得到结果水平部分。但是如果我 运行 我从 n.n= 之后的查询我得到结果。
我尝试使用 connect by level 的原因是我需要将我的整个查询放入 where 子句,因为在应用程序中有一个限制来执行我需要的 group by 子句。
SELECT n.n
FROM
(SELECT TO_NUMBER( LEVEL) - 1 n FROM DUAL CONNECT BY LEVEL <= 1000 ) n
WHERE n.n =
(subquery)
我拥有的在 HOURS 中工作的值示例似乎是整数,但是当它们相加时它们仍然是整数
5
10
5
5
20
但是我看到查询不起作用的地方是我有十进制值,例如:
3.68
2.45
5
10
5
Table:ASSIGNMENTS_M
Table: RESULT_VALUES
Columns: Result_ID, Assignment_ID, Date_Earned, Hours
INSERT INTO RESULT_VALUES(Result_ID, Assignment_ID, Date_Earned, Hours) VALUES(50,123456,to_date('01/02/2020', 'DD/MM/YYYY'),3.68 51,230034,to_date('02/02/2020', 'DD/MM/YYYY'),5 52,123456,to_date('03/02/2020', 'DD/MM/YYYY'),10 53,123456,to_date('04/02/2020', 'DD/MM/YYYY'),5 60,123456,to_date('05/02/2020', 'DD/MM/YYYY'),5 90,123456,to_date('06/02/2020', 'DD/MM/YYYY'),5 2384,123456,to_date('07/02/2020', 'DD/MM/YYYY'),10);
预期结果 = 38.68
这是一个解决方案,尽管您想要这样做很奇怪:
调整后fiddle:
这会增加 0.1 以找到匹配的行:
SELECT n.n
FROM ( SELECT TO_NUMBER(LEVEL)/10 - 1 n FROM DUAL CONNECT BY LEVEL <= 1000 ) n
WHERE n.n = (
SELECT round((sum(P2.HOURS)),1) FTE
FROM ASSIGNMENTS_M P1, RESULT_HOURS P2
WHERE P2.date_earned BETWEEN to_date('2020/01/01','YYYY/MM/DD') AND to_date('2020/10/31','YYYY/MM/DD')
AND P1.ASSIGNMENT_ID = 123456
GROUP BY P1.ASSIGNMENT_ID
)
;
这会增加 1 以找到匹配的行,但会调整计算以允许这样做:
SELECT n.n / 10
FROM ( SELECT TO_NUMBER(LEVEL) - 1 n FROM DUAL CONNECT BY LEVEL <= 1000 ) n
WHERE n.n = (
SELECT round((sum(P2.HOURS)),1) FTE
FROM ASSIGNMENTS_M P1, RESULT_HOURS P2
WHERE P2.date_earned BETWEEN to_date('2020/01/01','YYYY/MM/DD') AND to_date('2020/10/31','YYYY/MM/DD')
AND P1.ASSIGNMENT_ID = 123456
GROUP BY P1.ASSIGNMENT_ID
) * 10
;
结果:
None 个结果与 n
派生的 table:
生成的数字序列匹配
SELECT p1.assignment_id, round((sum(P2.HOURS)),1) FTE
FROM ASSIGNMENTS_M P1, RESULT_HOURS P2
WHERE P2.date_earned BETWEEN to_date('2020/01/01','YYYY/MM/DD') AND to_date('2020/10/31','YYYY/MM/DD')
AND P1.ASSIGNMENT_ID = 123456
GROUP BY P1.ASSIGNMENT_ID
;
结果:
+---------------=+
| id | fte |
+----------------+
| 123456 | 43.7 |
+----------------+
就是这个原因。现在你想如何改变这个逻辑?
你想要一个近似比较还是你想要你的序列以 0.1 为增量?
我正在尝试 运行 Oracle 中的查询,如果我将轮数更改为 0,我会得到一个结果,但是只要有小数,我在使用 connect by 时就不会得到结果水平部分。但是如果我 运行 我从 n.n= 之后的查询我得到结果。
我尝试使用 connect by level 的原因是我需要将我的整个查询放入 where 子句,因为在应用程序中有一个限制来执行我需要的 group by 子句。
SELECT n.n
FROM
(SELECT TO_NUMBER( LEVEL) - 1 n FROM DUAL CONNECT BY LEVEL <= 1000 ) n
WHERE n.n =
(subquery)
我拥有的在 HOURS 中工作的值示例似乎是整数,但是当它们相加时它们仍然是整数
5
10
5
5
20
但是我看到查询不起作用的地方是我有十进制值,例如:
3.68
2.45
5
10
5
Table:ASSIGNMENTS_M
Table: RESULT_VALUES
Columns: Result_ID, Assignment_ID, Date_Earned, Hours
INSERT INTO RESULT_VALUES(Result_ID, Assignment_ID, Date_Earned, Hours) VALUES(50,123456,to_date('01/02/2020', 'DD/MM/YYYY'),3.68 51,230034,to_date('02/02/2020', 'DD/MM/YYYY'),5 52,123456,to_date('03/02/2020', 'DD/MM/YYYY'),10 53,123456,to_date('04/02/2020', 'DD/MM/YYYY'),5 60,123456,to_date('05/02/2020', 'DD/MM/YYYY'),5 90,123456,to_date('06/02/2020', 'DD/MM/YYYY'),5 2384,123456,to_date('07/02/2020', 'DD/MM/YYYY'),10);
预期结果 = 38.68
这是一个解决方案,尽管您想要这样做很奇怪:
调整后fiddle:
这会增加 0.1 以找到匹配的行:
SELECT n.n
FROM ( SELECT TO_NUMBER(LEVEL)/10 - 1 n FROM DUAL CONNECT BY LEVEL <= 1000 ) n
WHERE n.n = (
SELECT round((sum(P2.HOURS)),1) FTE
FROM ASSIGNMENTS_M P1, RESULT_HOURS P2
WHERE P2.date_earned BETWEEN to_date('2020/01/01','YYYY/MM/DD') AND to_date('2020/10/31','YYYY/MM/DD')
AND P1.ASSIGNMENT_ID = 123456
GROUP BY P1.ASSIGNMENT_ID
)
;
这会增加 1 以找到匹配的行,但会调整计算以允许这样做:
SELECT n.n / 10
FROM ( SELECT TO_NUMBER(LEVEL) - 1 n FROM DUAL CONNECT BY LEVEL <= 1000 ) n
WHERE n.n = (
SELECT round((sum(P2.HOURS)),1) FTE
FROM ASSIGNMENTS_M P1, RESULT_HOURS P2
WHERE P2.date_earned BETWEEN to_date('2020/01/01','YYYY/MM/DD') AND to_date('2020/10/31','YYYY/MM/DD')
AND P1.ASSIGNMENT_ID = 123456
GROUP BY P1.ASSIGNMENT_ID
) * 10
;
结果:
None 个结果与 n
派生的 table:
SELECT p1.assignment_id, round((sum(P2.HOURS)),1) FTE
FROM ASSIGNMENTS_M P1, RESULT_HOURS P2
WHERE P2.date_earned BETWEEN to_date('2020/01/01','YYYY/MM/DD') AND to_date('2020/10/31','YYYY/MM/DD')
AND P1.ASSIGNMENT_ID = 123456
GROUP BY P1.ASSIGNMENT_ID
;
结果:
+---------------=+
| id | fte |
+----------------+
| 123456 | 43.7 |
+----------------+
就是这个原因。现在你想如何改变这个逻辑?
你想要一个近似比较还是你想要你的序列以 0.1 为增量?