--строим все возможные значения даты-времени через рекурсивную CTE
with dates as (
select convert(datetime, '20190101') AS cdate --начальная дата
UNION ALL
SELECT DATEADD(minute, 1, cdate) AS cdate --прибавляем по минуте
FROM dates
WHERE cdate <'20190102' --конечная дата
)
select * from dates d
left join tokens t
on t.ts = d.cdate
where t.ts is null --смотрим пропуски
option (maxrecursion 0)
with
d as (
select idGood, sum(amount) total from delivery group by idGood
),
s as (
select idGood, sum(amount) total from sale group by idGood
)
select d.idGood, d.total - isnull(s.total, 0) as amounts
from d left join s on s.idGood = s.idGood