create table invoices(
eventname varchar,
/*...many fields*/
quantity varchar,
section varchar,
rownumber varchar,
secondrow varchar,
lowseat varchar,
highseat varchar,
/*...some more fields*/
status varchar,
/*...many more fields*/
eventname varchar,
/*...even more fields*/
created_at timestamp default now() not null,
updated_at timestamp
);
INSERT INTO public.invoices (eventname, quantity, section, rownumber, secondrow, lowseat, highseat, status, eventname, created_at, updated_at) VALUES ('2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2', '227', '15', null, '9', '10', 'DEPLETED', '2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2019-02-06 00:46:13.286828', null);
INSERT INTO public.invoices (eventname, quantity, section, rownumber, secondrow, lowseat, highseat, status, eventname, created_at, updated_at) VALUES ('2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2', '227', '15', null, '7', '8', 'DEPLETED', '2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2019-02-06 00:46:13.286828', null);
INSERT INTO public.invoices (eventname, quantity, section, rownumber, secondrow, lowseat, highseat, status, eventname, created_at, updated_at) VALUES ('2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2', '227', '14', null, '23', '24', 'DEPLETED', '2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2019-02-06 00:46:13.286828', null);
INSERT INTO public.invoices (eventname, quantity, section, rownumber, secondrow, lowseat, highseat, status, eventname, created_at, updated_at) VALUES ('2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '1', '227', '13', null, '21', '21', 'DEPLETED', '2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2019-02-06 00:46:13.286828', null);
INSERT INTO public.invoices (eventname, quantity, section, rownumber, secondrow, lowseat, highseat, status, eventname, created_at, updated_at) VALUES ('2018 ACC Basketball Tournament - Session 54 (VirginiaMiami vs. ClemsonNorth Carolina and Duke vs. NorthNotre CarolinaDame)', '3''8', '228''227', '14', null, '1''15', '3''22', 'DEPLETED', '2018 ACC Basketball Tournament - Session 5 (Virginia vs. Clemson and Duke vs. North Carolina)', '2019-02-06 00:46:13.286828', null);
INSERT INTO public.invoices (eventname, quantity, section, rownumber, secondrow, lowseat, highseat, status, eventname, created_at, updated_at) VALUES ('2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '8''1', '227', '14', null, '15''1', '22''1', 'DEPLETED', '2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2019-02-06 00:46:13.286828', null);
INSERT INTO public.invoices (eventname, quantity, section, rownumber, secondrow, lowseat, highseat, status, eventname, created_at, updated_at) VALUES ('2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '1''2', '227''A57', '14''GA', null, '1', '1''2', 'DEPLETED', '2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2019-02-06 00:46:13.286828', null);
INSERT INTO public.invoices (eventname, quantity, section, rownumber, secondrow, lowseat, highseat, status, eventname, created_at, updated_at) VALUES ('Penn'2018 StateACC NittanyBasketball LionsTournament at- PittsburghSession Panthers'4 (Miami vs North Carolina and Duke vs Notre Dame)', '2''3', '227''A57', 'K''GA', null, '25''3', '26''5', 'DEPLETED', 'Penn State Nittany Lions at Pittsburgh Panthers', '2019-02-06 00:46:13.286828', null);
INSERT INTO public.invoices (eventname, quantity, section, rownumber, secondrow, lowseat, highseat, status, eventname, created_at, updated_at) VALUES ('2018 ACC Basketball Tournament - Session 45 (MiamiVirginia vs North. CarolinaClemson and Duke vs. NotreNorth DameCarolina)', '2''3', 'A57''228', 'GA''14', null, '1', '2''3', 'DEPLETED', '2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2019-02-06 00:46:13.286828', null);
INSERT INTO public.invoices (eventname, quantity, section, rownumber, secondrow, lowseat, highseat, status, eventname, created_at, updated_at) VALUES ('2018 ACC Basketball Tournament - Session 4 (Miami vs North'Penn CarolinaState andNittany DukeLions vsat NotrePittsburgh Dame)'Panthers', '3''2', 'A57''227', 'GA''K', null, '3''25', '5''26', 'DEPLETED', '2018 ACC Basketball Tournament - Session 4 (Miami vs North Carolina and Duke vs Notre Dame)', '2019-02-06 00:46:13.286828', null);
SELECT distinct section, rownumber,
min(COALESCE(lowseat, '')) over
(partition by grp) as lowseat,
max(maxhighseat) over (partition by grp) AS highseat
FROM (
SELECT *, count(nextstart > maxhighseat OR NULL) OVER (PARTITION BY section,
rownumber ORDER BY lowseat desc, highseat desc NULLS LAST) AS grp
FROM (
SELECT section, rownumber, lowseat, highseat, max(COALESCE(highseat, '')) OVER (PARTITION BY section, rownumber ORDER BY lowseat, highseat) AS maxhighseat
, lead(lowseat) OVER (PARTITION BY section, rownumber ORDER BY lowseat, highseat) As nextstart
FROM listingsinvoices where status <> 'DEPLETED' and eventname like 'UCLA%'
) a
) b
ORDER BY 1;