Related
i am trying to solve this this is table1 and i am trying to have below output i am not able to build up a logic that how could i fetch start date and end date from same timestemp column in SQL.
CREATE TABLE table1 (
`batch` INTEGER,
`timestemp` VARCHAR(8),
`mo` INTEGER,
`speed` INTEGER
);
INSERT INTO table1
(`batch`, `timestemp`, `mo`, `speed`)
VALUES
('1', '00:18:00', '0', '0'),
('1', '01:18:00', '0', '0'),
('1', '02:18:00', '0', '0'),
('1', '03:18:00', '1', '5'),
('1', '04:18:00', '1', '6'),
('1', '05:18:00', '1', '7'),
('1', '06:18:00', '2', '10'),
('1', '07:18:00', '2', '9'),
('1', '08:18:00', '2', '8'),
('1', '09:18:00', '3', '12'),
('1', '10:18:00', '3', '23'),
('1', '11:18:00', '3', '21'),
('1', '12:18:00', '4', '20'),
('1', '13:18:00', '4', '22');
mo=mode
batch
timestemp
mo
speed
1
00:18:00
0
0
1
01:18:00
0
0
1
02:18:00
0
0
1
03:18:00
1
5
1
04:18:00
1
6
1
05:18:00
1
7
1
06:18:00
2
10
1
07:18:00
2
9
1
08:18:00
2
8
1
09:18:00
3
12
1
10:18:00
3
23
1
11:18:00
3
21
1
12:18:00
4
20
1
13:18:00
4
22
ooutput:
batch
start time
end time
mode
1
00:18:00
03:17:00
0
1
03:18:00
06:17:00
1
1
06:18:00
09:17:00
2
1
09:18:00
12:17:00
3
1
12:18:00
13:18:00
4
Schema (MySQL v8.0)
CREATE TABLE table1 (
`batch` INTEGER,
`timestemp` TIME,
`mo` INTEGER,
`speed` INTEGER
);
INSERT INTO table1
(`batch`, `timestemp`, `mo`, `speed`)
VALUES
('1', '00:18:00', '0', '0'),
('1', '01:18:00', '0', '0'),
('1', '02:18:00', '0', '0'),
('1', '03:18:00', '1', '5'),
('1', '04:18:00', '1', '6'),
('1', '05:18:00', '1', '7'),
('1', '06:18:00', '2', '10'),
('1', '07:18:00', '2', '9'),
('1', '08:18:00', '2', '8'),
('1', '09:18:00', '3', '12'),
('1', '10:18:00', '3', '23'),
('1', '11:18:00', '3', '21'),
('1', '12:18:00', '4', '20'),
('1', '13:18:00', '4', '22');
Query
SELECT batch
, mode
, start_time
, COALESCE(SUBTIME(LEAD(start_time) OVER (ORDER BY start_time), '00:01:00'), end_time) end_time
FROM (
SELECT batch
, min(timestemp) start_time
, max(timestemp) end_time
, mo mode
FROM table1
GROUP BY batch, mo
) min_max;
batch
mode
start_time
end_time
1
0
00:18:00
03:17:00
1
1
03:18:00
06:17:00
1
2
06:18:00
09:17:00
1
3
09:18:00
12:17:00
1
4
12:18:00
13:18:00
View on DB Fiddle
I have a query here with a subquery. This shows me the complete result for the year. How do I get it to be displayed to me on a monthly basis. I've tried a few things but always get an error message
Here my query
SELECT
masch_nr, SUM(dauer) AS Prozess_Verfügbarkeit,
(SELECT SUM(dauer)
FROM [hydra1].[hydadm].[ereignis]
WHERE YEAR(begin_ts) = YEAR(CURRENT_TIMESTAMP)
AND masch_nr = 'FIMI1'
AND bmktonr IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11')) AS Verfügbarkeit
FROM
[hydra1].[hydadm].[ereignis]
WHERE
YEAR(begin_ts) = YEAR(CURRENT_TIMESTAMP)
AND masch_nr = 'FIMI1'
AND bmktonr IN ('7', '11')
GROUP BY
masch_nr
The result should look like this:
Month | Prozess_Verfügbarkeit | Verfügbarkeit
------+-----------------------+--------------
1 | 344 | 4556
2 | 445 | 5654
Thank you
You can probably simplify this by using conditional aggregation
SELECT
YEAR(begin_ts) AS [Year]
, MONTH(begin_ts) AS [Month]
, masch_nr
, SUM(CASE WHEN bmktonr IN ('7', '11')
THEN dauer END) AS Prozess_Verfügbarkeit
, SUM(CASE WHEN bmktonr IN ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11')
THEN dauer END) AS Verfügbarkeit
FROM [hydra1].[hydadm].[ereignis]
WHERE masch_nr = 'FIMI1'
AND begin_ts >= DATEADD(YEAR,DATEDIFF(YEAR,0,CURRENT_TIMESTAMP),0)
AND begin_ts < DATEADD(YEAR,DATEDIFF(YEAR,0,CURRENT_TIMESTAMP)+1,0)
GROUP BY YEAR(begin_ts), MONTH(begin_ts), masch_nr
ORDER BY [Year], [Month], masch_nr
I have a table like this in a Oracle 11g Database
TABLE REC
REC_ID NUMBER
CARD_ID NUMBER
REC_STATUS NUMBER
REC_DATE TIMESTAMP
I want to know how many cards have recharged in a given period, but I want this information grouped by date.
If a card_id has already been counted on one date, it should not be counted on another next (distinct).
Here some test data
with
REC ( REC_ID, CARD_ID, REC_STATUS, REC_DATE ) as (
select '1', '100', '1', SYSTIMESTAMP - 5 from dual union all
select '2', '100', '1', SYSTIMESTAMP - 5 from dual union all
select '3', '200', '1', SYSTIMESTAMP - 5 from dual union all
select '4', '100', '1', SYSTIMESTAMP - 4 from dual union all
select '5', '300', '1', SYSTIMESTAMP - 4 from dual union all
select '6', '200', '1', SYSTIMESTAMP - 4 from dual union all
select '7', '100', '1', SYSTIMESTAMP - 3 from dual union all
select '8', '400', '1', SYSTIMESTAMP - 3 from dual union all
select '9', '400', '1', SYSTIMESTAMP - 3 from dual union all
select '10', '400', '1', SYSTIMESTAMP - 2 from dual union all
select '11', '300', '1', SYSTIMESTAMP - 2 from dual union all
select '12', '100', '1', SYSTIMESTAMP - 2 from dual union all
select '13', '400', '1', SYSTIMESTAMP - 2 from dual
)
-- end of test data
When I execute the query like this, i have the total count of 4, which is correct.
SELECT
COUNT(DISTINCT CARD_ID) COUNT
FROM REC
WHERE REC_STATUS = 1
Result
|COUNT|
| 4 |
But, when I try this way, i have the total count of 10.
This is because when I use a "group by" on a date and use the "distinct" in the ID, the distinction will only work for the grouped date, not for the total period.
SELECT
TRUNC(REC_DATE) DAT,
COUNT(DISTINCT CARD_ID) COUNT
FROM REC
WHERE REC_STATUS = 1
GROUP BY TRUNC(REC_DATE)
Result
DAT | COUNT
14/06/19 | 2
13/06/19 | 3
15/06/19 | 3
12/06/19 | 2
What can I do to apply the distinct to the period total and still keep the grouping by date?
Perhaps you just want the earliest date for each card:
SELECT TRUNC(MIN_REC_DATE) as DAT,
COUNT(*) as COUNT
FROM (SELECT CARD_ID, MIN(REC_DATE) as MIN_REC_DATE
FROM REC
WHERE REC_STATUS = 1
GROUP BY CARD_ID
) R
GROUP BY TRUNC(MIN_REC_DATE);
What I'd like to do is to extend RxEndDates until there is no more overlap in the prescriptions. And new extensions do not overlap either.
Context: If Amy takes Humera daily and gets a refill before her current prescription runs out, then add the DaySupply of the 2nd prescription to the first prescription.
sample data:
User Drug RxStartDate DaySupply RxEndDate
Amy Humera 2/12/2017 7 2/18/2017
Amy Humera 2/28/2017 5 3/4/2017 <--Overlap with below
Amy Humera 3/3/2017 5 3/7/2017 <--Overlap with above, need to combine
Amy Humera 3/8/2017 2 3/9/2017
Amy Humera 3/10/2017 7 3/16/2017
Amy Humera 3/17/2017 30 4/15/2017 <--Overlap with all below, combine
Amy Humera 3/22/2017 2 3/23/2017 <--Overlap
Amy Humera 3/24/2017 2 3/25/2017 <--Overlap
Amy Humera 3/31/2017 3 4/2/2017 <--Overlap
Amy Humera 4/7/2017 5 4/11/2017 <--Overlap
Amy Humera 4/13/2017 30 5/12/2017 <--Overlap
So after we combine, we get
User Drug RxStartDate DaySupply RxEndDate
Amy Humera 2/12/2017 7 2/18/2017
Amy Humera 2/28/2017 10 3/9/2017 <-- Combined from above, new overlap
Amy Humera 3/8/2017 2 3/9/2017 <-- Now this overlaps with above
Amy Humera 3/10/2017 7 3/16/2017
Amy Humera 3/17/2017 72 5/27/2017
User Drug RxStartDate DaySupply RxEndDate
Amy Humera 2/12/2017 7 2/18/2017
Amy Humera 2/28/2017 12 3/11/2017 <-- Combined, again, new overlap
Amy Humera 3/10/2017 7 3/16/2017 <-- Now this overlaps with above
Amy Humera 3/17/2017 72 5/27/2017
User Drug RxStartDate DaySupply RxEndDate
Amy Humera 2/12/2017 7 2/18/2017
Amy Humera 2/28/2017 19 3/18/2017 <-- Combined, again, new overlap
Amy Humera 3/17/2017 72 5/27/2017 <-- Now this overlaps with above
User Drug RxStartDate DaySupply RxEndDate
Amy Humera 2/12/2017 7 2/18/2017
Amy Humera 2/28/2017 91 5/29/2017
There is no more overlap…finished!
Is there a way to do this automatically in a loop or something...any ideas?
I think the solution can only be implemented by recursion, as there should be a loop that calculates the accumulated DaySupply and I see no way of doing that with any non-recursive lookups. You can do this with recursive CTE - and according to the official doc, it is available starting with SQL Server 2008.
A possible implementation (I added some test data to challenge it):
DECLARE #test TABLE (
[User] VARCHAR(100),
Drug VARCHAR(100),
RxStartDate DATE,
DaySupply INT,
RxEndDate DATE
)
INSERT #test
VALUES
('Amy', 'Humera', '2/12/2017', '7', '2/18/2017'),
('Amy', 'Humera', '2/28/2017', '5', '3/4/2017'),
('Amy', 'Humera', '3/3/2017', '5', '3/7/2017'),
('Amy', 'Humera', '3/8/2017', '2', '3/9/2017'),
('Amy', 'Humera', '3/10/2017', '7', '3/16/2017'),
('Amy', 'Humera', '3/17/2017', '30', '4/15/2017'),
('Amy', 'Humera', '3/22/2017', '2', '3/23/2017'),
('Amy', 'Humera', '3/24/2017', '2', '3/25/2017'),
('Amy', 'Humera', '3/31/2017', '3', '4/2/2017'),
('Amy', 'Humera', '4/7/2017', '5', '4/11/2017'),
('Amy', 'Humera', '4/13/2017', '30', '5/12/2017'),
('Amy', 'Other', '3/24/2017', '7', '3/30/2017'),
('Amy', 'Other', '3/31/2017', '3', '4/2/2017'),
('Amy', 'Other', '4/7/2017', '5', '4/11/2017'),
('Amy', 'Other', '4/13/2017', '30', '5/12/2017'),
('Joe', 'Humera', '3/24/2017', '8', '3/31/2017'),
('Joe', 'Humera', '3/31/2017', '3', '4/2/2017'),
('Joe', 'Humera', '4/12/2017', '5', '4/16/2017'),
('Joe', 'Humera', '4/23/2017', '30', '5/22/2017'),
('Joe', 'Other', '3/24/2017', '60', '5/23/2017'),
('Joe', 'Other', '3/31/2017', '3', '4/2/2017'),
('Joe', 'Other', '4/7/2017', '5', '4/11/2017'),
('Joe', 'Other', '4/13/2017', '30', '5/12/2017')
-- You can comment this out, it is just to show progress:
SELECT * FROM #test ORDER BY [User], Drug, RxStartDate
DECLARE #test_2 TABLE (
[User] VARCHAR(100),
Drug VARCHAR(100),
RxStartDate_base DATE,
DaySupplyCumulative INT
)
;WITH CTE_RxEndDateExtended as (
SELECT [User], Drug, RxStartDate, DaySupply, DaySupply as DaySupplyCumulative, RxStartDate as RxStartDate_base, RxStartDate as RxStartDateExtended, dateadd (dd, DaySupply, RxStartDate) as RxEndDateExtended
FROM #test
-- WHERE [User] = 'Amy' and Drug = 'Humera' and RxStartDate = '2/28/2017'
UNION ALL
SELECT t.[User], t.Drug, t.RxStartDate, t.DaySupply, c.DaySupplyCumulative + t.DaySupply as DaySupplyCumulative, c.RxStartDate_base, t.RxStartDate as RxStartDateExtended, dateadd (dd, t.DaySupply, c.RxEndDateExtended) as RxEndDateExtended
FROM CTE_RxEndDateExtended as c INNER JOIN #test as t
on c.[User] = t.[User] and c.Drug = t.Drug
and c.RxEndDateExtended >= t.RxStartDate and c.RxStartDateExtended < t.RxStartDate
)
INSERT #test_2
SELECT [User], Drug, RxStartDate_base, MAX (DaySupplyCumulative) as DaySupplyCumulative -- comment this out and use this for debugging: SELECT *
FROM CTE_RxEndDateExtended
GROUP BY [User], Drug, RxStartDate_base -- comment this out for debugging
OPTION (MAXRECURSION 0) -- comment this out and use this for debugging (to avoid infinite loops): OPTION (MAXRECURSION 1000)
-- You can comment this out, it is just to show progress:
SELECT * FROM #test_2
ORDER BY [User], Drug, RxStartDate_base -- comment this out and use this for debugging: ORDER BY [User], Drug, RxStartDate_base, RxStartDate, DaySupplyCumulative
SELECT base.*, dateadd (dd, base.DaySupplyCumulative - 1, base.RxStartDate_base) as RxEndDateCumulative
FROM #test_2 as base LEFT OUTER JOIN #test_2 as filter
on base.[User] = filter.[User] and base.Drug = filter.Drug
and base.RxStartDate_base > filter.RxStartDate_base
and dateadd (dd, base.DaySupplyCumulative, base.RxStartDate_base) <= dateadd (dd, filter.DaySupplyCumulative, filter.RxStartDate_base)
WHERE filter.[User] IS NULL
ORDER BY [User], Drug, RxStartDate_base
Maybe you need to optimize it by simplifying the logic. But be careful not to make an infinite loop. When debugging use OPTION (MAXRECURSION N) with N other than zero.
PS.: this one works also if I add 'Amy', 'Humera', '2/15/2017', '11', '2/25/2017', with which I was criticizing the other solutions... I am curious if it works as you expect - please test!
You can identify where a group starts, using not exists. Then do a cumulative sum to assign a group . . . and aggregate. The following assumes a unique id, which is sort of needed to handle duplicates:
select [user], drug, grp, sum(daysupply), min(RxStartDate), max(RxEndDate)
from (select t.*, sum(flg) over (partition by [user], drug order by RxStartDate) as grp
from (select t.*,
(case when exists (select 1
from #test t2
where t2.[user] = t.[user] and t2.drug = t.drug and
t2.RxStartDate < t.RxStartDate and
t2.RxEndDate >= dateadd(day, -1, t.RxStartDate)
)
then 0 else 1
end) as flg
from #test t
) t
) t
group by [user], drug, grp;
I used a CTE Common Table Expression to perform the grouping. Since some of the days don't technically overlap, I created an alternate end date [RxEndDate_ALT] by adding 1 to the [RxEndDate] in the source_data. Then I was able to group the dates using NOT EXISTS in source_data_grouped. After that, I join back to the source_data_raw to SUM the [DaySupply].
Results
SQL
WITH
source_data_raw
AS
(
SELECT tbl.* FROM (VALUES
( 'Amy', 'Humera', 7, CAST('12-Feb-2017' AS DATE), CAST('18-Feb-2017' AS DATE))
, ( 'Amy', 'Humera', 5, '28-Feb-2017', '04-Mar-2017')
, ( 'Amy', 'Humera', 5, '03-Mar-2017', '07-Mar-2017')
, ( 'Amy', 'Humera', 2, '08-Mar-2017', '09-Mar-2017')
, ( 'Amy', 'Humera', 7, '10-Mar-2017', '16-Mar-2017')
, ( 'Amy', 'Humera', 30, '17-Mar-2017', '15-Apr-2017')
, ( 'Amy', 'Humera', 2, '22-Mar-2017', '23-Mar-2017')
, ( 'Amy', 'Humera', 2, '24-Mar-2017', '25-Mar-2017')
, ( 'Amy', 'Humera', 3, '31-Mar-2017', '15-Apr-2017')
, ( 'Amy', 'Humera', 5, '07-Apr-2017', '16-Apr-2017')
, ( 'Amy', 'Humera', 30, '13-Apr-2017', '27-May-2017')
) tbl ([User], [Drug], [DaySupply], [RxStartDate], [RxEndDate])
)
,
source_data
AS
(
SELECT
sdr.[User]
, sdr.[Drug]
, sdr.[RxStartDate]
, sdr.[RxEndDate]
, [RxEndDate_ALT] = DATEADD(DAY, 1, sdr.[RxEndDate])
FROM
source_data_raw AS sdr
)
,
source_data_grouped
AS
(
SELECT
s1.[User]
, s1.[Drug]
, s1.[RxStartDate]
, [RxEndDate] = MIN(t1.[RxEndDate])
FROM
source_data AS s1
INNER JOIN source_data AS t1 ON s1.[User] = t1.[User] AND s1.[Drug] = t1.[Drug] AND s1.[RxStartDate] <= t1.[RxEndDate_ALT]
AND NOT EXISTS
(
SELECT 1
FROM source_data AS t2
WHERE
1=1
AND t1.[User] = t2.[User]
AND t1.[Drug] = t2.[Drug]
AND t1.[RxEndDate_ALT] >= t2.[RxStartDate]
AND t1.[RxEndDate_ALT] < t2.[RxEndDate_ALT]
)
WHERE
1=1
AND NOT EXISTS
(
SELECT 1
FROM source_data AS s2
WHERE
1=1
AND s1.[User] = s2.[User]
AND s1.[Drug] = s2.[Drug]
AND s1.[RxStartDate] > s2.[RxStartDate]
AND s1.[RxStartDate] <= s2.[RxEndDate_ALT]
)
GROUP BY
s1.[User]
, s1.[Drug]
, s1.[RxStartDate]
)
SELECT
sdg.[User]
, sdg.[Drug]
, [DaySupply] = SUM(sdr.[DaySupply])
, sdg.[RxStartDate]
, sdg.[RxEndDate]
FROM
source_data_grouped AS sdg
INNER JOIN source_data_raw AS sdr ON sdr.[RxStartDate] BETWEEN sdg.[RxStartDate] AND sdg.[RxEndDate]
GROUP BY
sdg.[User]
, sdg.[Drug]
, sdg.[RxStartDate]
, sdg.[RxEndDate]
I need to be able to create a Trailing Twelve Month report using SQL (PostgreSQL) - essentially a window/rolling 12 month sum that sums up the current month's totals + the previous 11 months for each month.
I have this table:
CREATE TABLE order_test(
order_id text,
sale_date date,
delivery_date date,
customer_id text,
vendor_id text,
order_total float);
with these values:
insert into order_test
values ('1', '2016-06-01', '2016-06-10', '2', '3', 200.10),
('2', '2016-06-02', '2016-06-11', '2', '4', 150.50),
('3', '2016-07-02', '2016-07-11', '5', '4', 100.50),
('4', '2016-07-02', '2016-07-11', '1', '4', 150.50),
('5', '2016-07-02', '2016-07-11', '1', '4', 150.50),
('6', '2016-08-02', '2016-08-11', '6', '4', 300.50),
('7', '2016-08-02', '2016-08-11', '6', '4', 150.50),
('8', '2016-09-02', '2016-09-11', '1', '4', 150.50),
('9', '2016-10-02', '2016-10-11', '1', '4', 150.50),
('10', '2016-11-02', '2016-11-11', '1', '4', 150.50),
('11', '2016-12-02', '2016-12-11', '6', '4', 150.50),
('12', '2017-01-02', '2017-01-11', '7', '4', 150.50),
('13', '2017-01-02', '2017-01-11', '1', '4', 150.50),
('14', '2017-01-02', '2017-01-11', '1', '4', 100.50),
('15', '2017-02-02', '2017-02-11', '1', '4', 150.50),
('16', '2017-02-02', '2017-02-11', '1', '4', 150.50),
('17', '2017-03-02', '2017-03-11', '2', '4', 150.50),
('18', '2017-03-02', '2017-03-11', '2', '4', 150.50),
('19', '2017-04-02', '2017-04-11', '6', '4', 120.50),
('20', '2017-05-02', '2017-05-11', '1', '4', 150.50),
('21', '2017-06-02', '2017-06-11', '2', '4', 150.50),
('22', '2017-06-02', '2017-06-11', '1', '4', 130.50),
('23', '2017-07-02', '2017-07-11', '1', '4', 150.50),
('24', '2017-07-02', '2017-07-11', '5', '4', 200.50),
('25', '2017-08-02', '2017-08-11', '1', '4', 150.50),
('26', '2017-09-02', '2017-09-11', '2', '4', 100.50),
('27', '2017-09-02', '2017-10-11', '1', '4', 150.50);
These are individual sales. For each month, I need the previous 11 months + that month's total (sale month).
I've tried a window calculation like this:
select date_trunc('month', sale_date) as sale_month,
sum(order_total) over w as total_sales
from order_test
where (delivery_date < current_date) and
(sale_date >= (date_trunc('month', current_date) - interval '1 year'))
window w as (Partition by date_trunc('month', sale_date)
order by sale_date
rows between current row and 11 following)
but it's giving me this:
sale_month total_sales
1 01.09.2016 00:00:00 150,5
2 01.10.2016 00:00:00 150,5
3 01.11.2016 00:00:00 150,5
4 01.12.2016 00:00:00 150,5
5 01.01.2017 00:00:00 401,5
6 01.01.2017 00:00:00 251
7 01.01.2017 00:00:00 100,5
8 01.02.2017 00:00:00 301
9 01.02.2017 00:00:00 150,5
10 01.03.2017 00:00:00 301
11 01.03.2017 00:00:00 150,5
12 01.04.2017 00:00:00 120,5
13 01.05.2017 00:00:00 150,5
14 01.06.2017 00:00:00 281
15 01.06.2017 00:00:00 130,5
16 01.07.2017 00:00:00 351
17 01.07.2017 00:00:00 200,5
18 01.08.2017 00:00:00 150,5
19 01.09.2017 00:00:00 100,5
where there should only be one row per month.
In inner query derived table, you need to truncate Sale_Date column to month precision using date_trunc and group by the resulting column to get the Month_total sales and then in outer query, use cumulative window sum function on month_total sales data ordering by Sale_Month to get your desired result as below.
SELECT sale_Month
,month_total
,sum(month_total) OVER (
ORDER BY sale_Month ASC rows BETWEEN 11 preceding
AND CURRENT row
) AS Sum_Series
FROM (
SELECT date_trunc('month', Sale_Date) AS Sale_Month
,sum(Order_Total) AS Month_Total
FROM order_test
GROUP BY 1
ORDER BY 1
) t
Kindly note that AND CURRENT row is optional as cumulative window function includes the current row by default, so the query can be rewritten as below.
SELECT sale_Month
,month_total
,sum(month_total) OVER (
ORDER BY sale_Month ASC rows 11 preceding
) AS Sum_Series
FROM (
SELECT date_trunc('month', Sale_Date) AS Sale_Month
,sum(Order_Total) AS Month_Total
FROM order_test
GROUP BY 1
ORDER BY 1
) t
Result:
sale_month month_total sum_series
----------------------------------------------
2016-06-01T00:00:00Z 350.6 350.6
2016-07-01T00:00:00Z 401.5 752.1
2016-08-01T00:00:00Z 451 1203.1
2016-09-01T00:00:00Z 150.5 1353.6
2016-10-01T00:00:00Z 150.5 1504.1
2016-11-01T00:00:00Z 150.5 1654.6
2016-12-01T00:00:00Z 150.5 1805.1
2017-01-01T00:00:00Z 401.5 2206.6
2017-02-01T00:00:00Z 301 2507.6
2017-03-01T00:00:00Z 301 2808.6
2017-04-01T00:00:00Z 120.5 2929.1
2017-05-01T00:00:00Z 150.5 3079.6
2017-06-01T00:00:00Z 281 3010
2017-07-01T00:00:00Z 351 2959.5
2017-08-01T00:00:00Z 150.5 2659
2017-09-01T00:00:00Z 251 2759.5
You can check the demo here
If I understand it correctly, you want all months to have cumulative data for the last 11 months. But the first 11 rows won't have preceding 11 entries to calculate the rolling sum. But you have mentioned that all months should have a cumulative total.
So I believe you are looking for something like this.
with x as (
select date_trunc('month', sale_date) as sale_month,sum(order_total) as monthly_order_total from order_test
group by 1 order by 1 asc)
select sale_month, monthly_order_total,
sum(monthly_order_total ) over (order by sale_month asc rows between 11 preceding and current row)
from x