I'm curious if there is a way to slim down this code either with SQL or with C#.
-- VARS
declare @iagent_id int
declare @quarter int
declare @prev_quarter_1 int
declare @prev_quarter_2 int
declare @prev_quarter_3 int
declare @year int
declare @prev_quarter_year_1 int
declare @prev_quarter_year_2 int
declare @prev_quarter_year_3 int
set @quarter = datepart(QQ, getdate()) - 1
set @year = datepart(year, getdate())
set @prev_quarter_year_1 = @year
set @prev_quarter_year_2 = @year
set @prev_quarter_year_3 = @year
if @quarter = 0
begin
set @quarter = 4
set @year = @year - 1
set @prev_quarter_1 = 3
set @prev_quarter_year_1 = @year
set @prev_quarter_2 = 2
set @prev_quarter_year_2 = @year
set @prev_quarter_3 = 1
set @prev_quarter_year_3 = @year
end
else
begin
if @quarter = 3
begin
set @prev_quarter_1 = 2
set @prev_quarter_2 = 1
set @prev_quarter_3 = 4
set @prev_quarter_year_3 = @year - 1
end
else
begin
if @quarter = 2
begin
set @prev_quarter_1 = 1
set @prev_quarter_2 = 4
set @prev_quarter_year_2 = @year - 1
set @prev_quarter_3 = 3
set @prev_quarter_year_3 = @year - 1
end
else
begin
if @quarter = 1
begin
set @prev_quarter_1 = 4
set @prev_quarter_year_1 = @year - 1
set @prev_quarter_2 = 3
set @prev_quarter_year_2 = @year - 1
set @prev_quarter_3 = 2
set @prev_quarter_year_3 = @year - 1
end
end
end
end
select @quarter as 'Quarter', @year as 'Year', @prev_quarter_1 as 'pq1', @prev_quarter_year_1 as 'pqy1', @prev_quarter_2 as 'pq2', @prev_quarter_year_2 as 'pqy2', @prev_quarter_3 as 'pq3', @prev_quarter_year_3 as 'pqy3'