I am trying to create a report that shows how many items are available within a specific date range, entered by the user. It should coordinate with requested variable start and end dates…
Start: 2021-01-10 / End: 2021-01-15 results should be…
1234 | table | 050 | 2021-01-10 | 2021-01-12 | 085 1234 | chair | 200 | 2021-01-10 | 2021-01-12 | 300 1234 | light | 002 | 2021-01-10 | 2021-01-12 | 005 5678 | table | 025 | 2021-01-12 | 2021-01-15 | 085 5678 | light | 003 | 2021-01-12 | 2021-01-15 | 005 9876 | table | 010 | 2021-01-10 | 2021-01-14 | 085 9876 | chair | 100 | 2021-01-10 | 2021-01-14 | 300 2323 | light | 010 | 2021-01-12 | 2021-01-15 | 010
Start: 2021-01-13 / End: 2021-01-18 results should be…
5678 | table | 025 | 2021-01-12 | 2021-01-15 | 035 5678 | light | 003 | 2021-01-12 | 2021-01-15 | 013 9876 | table | 010 | 2021-01-10 | 2021-01-14 | 035 9876 | chair | 100 | 2021-01-10 | 2021-01-14 | 100 2323 | light | 010 | 2021-01-12 | 2021-01-15 | 013
SELECT
t1.*,
(SELECT CAST(SUM(t2.quantity) AS CHAR)
FROM timetable t2
WHERE t2.item = t1.item) AS itm_sum
FROM timetable t1
SELECT
t1.*,
(SELECT CAST(SUM(t2.quantity) AS CHAR)
FROM timetable t2
WHERE t2.item = t1.item) AS itm_sum
FROM timetable t1
WHERE ('2021-01-13 10:00:00' <= dt_END) AND
(dt_start <= '2021-01-18 10:00:00')
I am getting incorrect results.
The beginning Fiddle is here….
https://www.db-fiddle.com/f/o5WtkgisvjN4VvwwKai3BP/3#&togetherjs=JLKJ4PjgsU
Best Answer
It is quite simole your inner SELECT counts every row that it gets, from timetable so you have to rstrict also the inner SEELCT
db<>fiddle here