Query to select only quater data based on column value.

Database

Query to select only quater data based on column value.

I need to write a teradata query. One of the column in source is defined as DATE (END_DT). Source records should be selected as per below condition.

1. END_DT is the column which stores the date.

2. Records should be selected only for the latest completed quater.

Eg:

END_DT

----------

31-03-12

31-03-12

29-03-12

31-12-11

30-09-11

Then latest completed quater is 31-03-12 so only first three records should be selected.

Tags (2)
1 REPLY

Re: Query to select only quater data based on column value.

Hi,

This is the data i tried,

ed_dt

01/17/2012

02/17/2012

03/17/2012

04/17/2012

05/07/2012

05/16/2012

SEL a.* FROM dat  a 
JOIN Sys_Calendar.calendar b
ON a.ed_dt=b.calendar_date

JOIN (SEL  calendar_date, CASE WHEN quarter_of_year=1 THEN year_of_calendar - 1 ELSE   year_of_calendar END AS years,
CASE WHEN    quarter_of_year =1 THEN 4 ELSE quarter_of_year - 1  END AS qtr FROM Sys_Calendar.calendar ) c
ON c.years=b.year_of_calendar
AND b.quarter_of_year=c.qtr
AND c.calendar_date=DATE

Thanks,

Karthik. N