admin管理员组文章数量:1418922
I have this 2 tables:
Table 1 has a STARTTIME and ENDTIME representing the interval of time Table 2 has the same with an extra param COUNT. However Table 2 has a lot smaller intervals.
A visual representation of how the intervals could go:
Table 1 | Table 2 |
---|---|
row1 | row1 |
row1 | row2 |
row1 | row3 |
row2 | row4 |
row2 | row5 |
row2 | row6 |
row2 | row7 |
row3 | row8 |
row3 | row9 |
I have this 2 tables:
Table 1 has a STARTTIME and ENDTIME representing the interval of time Table 2 has the same with an extra param COUNT. However Table 2 has a lot smaller intervals.
A visual representation of how the intervals could go:
Table 1 | Table 2 |
---|---|
row1 | row1 |
row1 | row2 |
row1 | row3 |
row2 | row4 |
row2 | row5 |
row2 | row6 |
row2 | row7 |
row3 | row8 |
row3 | row9 |
I need a query that given a row_id of the table 1 gives me the sum of COUNT on table 2 corresponding to the interval of times
Table 1 | Table 2 |
---|---|
t_start | t_start to t_end |
t_start to t_end | |
t_end | t_start to t_end |
What I did:
SELECT
STARTTIME,
ENDTIME
FROM TABLE1
WHERE ROOM_ID = :ROOM_ID
AND :STARTTIME < ENDTIME
AND STARTTIME < :ENDTIME
ORDER BY STARTTIME DESC
And for each row that the query above I need this query:
SELECT
SUM(COUNT) AS SUM_TOTAL
FROM TABLE2
WHERE ROOM_ID = :ROOM_ID
AND :STARTTIME < ENDTIME
AND ENDTIME <= :ENDTIME
In this second one I use the STARTTIME and ENDTIME from the rows of the first query.
Data example from Table2:
STARTTIME | ENDTIME | COUNT |
---|
Here is a visual representation of what I need:
For each state on the table1 (upper one) I need the sum of the value COUNT on the table2 (down one) from that specific period.
I have tried this unsuccessfully:
SELECT
STARTTIME,
ENDTIME,
MAX(
SELECT SUM(DIFVALUE)
FROM TABLE2
WHERE ROOM_ID = :ROOM_ID
AND :STARTTIME < ENDTIME
AND ENDTIME <= :ENDTIME
)
FROM TABLE1
WHERE ROOM_ID = :ROOM_ID
AND :STARTTIME < ENDTIME
AND STARTTIME < :ENDTIME
ORDER BY STARTTIME DESC
Share
Improve this question
edited Jan 29 at 14:47
Danielps1818
asked Jan 29 at 12:00
Danielps1818Danielps1818
131 silver badge8 bronze badges
3
|
2 Answers
Reset to default 0Join the two tables, something like:
SELECT t1.id,
t1.starttime,
t1.endtime,
SUM(t2.count) AS total
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON ( t1.id = t2.id
AND t1.starttime < t2.endtime
AND t1.endtime > t2.starttime )
WHERE t1.id = :ID
AND t1.endtime > :STARTTIME
AND t1.starttime < :ENDTIME
ORDER BY t1.starttime DESC
Note: Untested as you have not provided a minimal representative example with the DDL/DML statements for your tables or sample data.
Solution:
SELECT
TT.STARTTIME,
MAX(TT.ENDTIME) AS ENDTIME,
MAX(
(
SELECT SUM(CT.DIFVALUE)
FROM DTIM_COUNTER_TABLE CT
WHERE CT.ROOM_ID = :ROOM_ID
AND (
(TT.STARTTIME < CT.ENDTIME AND :STARTTIME < CT.ENDTIME)
AND CT.ENDTIME <= TT.ENDTIME
)
)
) AS SUMTOTAL
FROM
DTIM_TIME_TABLE TT
WHERE
TT.ROOM_ID = :ROOM_ID
AND :STARTTIME < TT.ENDTIME
AND TT.STARTTIME < :ENDTIME
GROUP BY
TT.STARTTIME
ORDER BY
TT.STARTTIME DESC;
But I'm going to use a band join to improve efficiency.
本文标签: sqlGet result from another query for each row without relationStack Overflow
版权声明:本文标题:sql - Get result from another query for each row without relation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745299860a2652305.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
CREATE TABLE
andINSERT
statements for your sample data in both tables (in a format we can copy-paste and execute - not as images); an English description of the logic that you want to implement (including edges cases where atable2
range overlaps two ranges intable1
, if such a thing can occur); and the expected output for that sample data (again, as text, not images). – MT0 Commented Jan 29 at 14:55