Skip to content

Instantly share code, notes, and snippets.

@jingyang-li
Created May 12, 2022 14:08
Show Gist options
  • Save jingyang-li/acf33035af0dc13ee3ecfbcff5f4c385 to your computer and use it in GitHub Desktop.
Save jingyang-li/acf33035af0dc13ee3ecfbcff5f4c385 to your computer and use it in GitHub Desktop.
create table datatable (TrendType varchar(12), date date, sales int)
insert into datatable values ('Monthly','5/1/2021',1234),
('Monthly','3/1/2021',1234),
('Monthly','2/1/2022',1234),
('Monthly','5/1/2022',1234),
('Monthly','8/1/2021',1234),
('Monthly','8/1/2022',1234),
('Monthly','12/1/2021',1234),
('Monthly','11/1/2022',1234)
select 'Quarterly' TrendType, DATEADD(Q, DATEDIFF(Q, 0, [date]) , 0) as [date],
Sum(sales) Sales from datatable
group by DATEADD(Q, DATEDIFF(Q, 0, [date]) , 0)
order by DATEADD(Q, DATEDIFF(Q, 0, [date]) , 0)
drop table datatable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment