Teradata SQL - how to get data between current month and current month+8 (current month+8 will fall into 2023) - teradata

I have this code but returns 0 row:
SELECT
EXTRACT(MONTH FROM POST_DATE)
FROM
MY_TABLE
WHERE
EXTRACT(MONTH FROM POST_DATE) BETWEEN EXTRACT(MONTH FROM CURRENT_DATE) AND EXTRACT(MONTH FROM ADD_MONTHS(CURRENT_DATE,8))
Now it's month 5, and if my code changes to 7 instead 8, the result is showing 5,6,7,8,9,10,11,12.
And MY_TABLE has data for 2023.
Can anyone please help? Thanks.

Here we calculate the first day of this month, then we add 9 months to the last day of last month.
SELECT
EXTRACT(MONTH FROM POST_DATE)
FROM
MY_TABLE
WHERE POST_DATE BETWEEN
ADD_MONTHS(CURRENT_DATE - EXTRACT(DAY FROM CURRENT_DATE)+1, 0)
AND
ADD_MONTHS(CURRENT_DATE - EXTRACT(DAY FROM CURRENT_DATE), 9)
;

Related

Not A valid month error while subtracting timestamps

I am trying to subtract timestamp values. It gives ORA-01843: NOT A VALID MONTH error.
Below query runs fine in SQL Devloper,
But while runtime it throws not a valid month error.
I am not able to find out. Can anybody modify this query.
Select substr(TO_TIMESTAMP(TO_CHAR(end_time,'DD-MM-YY HH12:MI:SS'))-(TO_TIMESTAMP(TO_CHAR(start_time,'DD-MM-YY HH12:MI:SS')),12,8))as Duration from Job_execution
If those datatypes are TIMESTAMP, then why don't you just subtract them?
SQL> create table job_execution
2 (id number,
3 start_time timestamp(6),
4 end_time timestamp(6));
Table created.
SQL> insert into job_execution (id, start_time, end_time) values
2 (1, to_timestamp('20.11.2019 10:30:00', 'dd.mm.yyyy hh24:mi:ss'),
3 to_timestamp('25.11.2019 14:00:00', 'dd.mm.yyyy hh24:mi:ss'));
1 row created.
SQL> select end_time - start_time diff from job_execution where id = 1;
DIFF
---------------------------------------------------------------------------
+000000005 03:30:00.000000
SQL>
"Not a valid month" can be result of timestamp values stored into VARCHAR2 columns where you think everything is entered correctly, but there are values such as 25.18.2019 (dd.mm.yyyy), as there's no 18th month in any year.
That's why I asked for the datatype.
[EDIT: how to format the result]
If you want a "nice" displayed result, then it requires some more typing. For example:
SQL> with difference as
2 (select end_time - start_Time as diff
3 from job_execution
4 where id = 1
5 )
6 select extract (day from diff) ||' '||
7 lpad(extract (hour from diff), 2, '0') ||':'||
8 lpad(extract (minute from diff), 2, '0') ||':'||
9 lpad(extract (second from diff), 2, '0') result
10 from difference;
RESULT
--------------------------------------------------------------
5 03:30:00
SQL>

TERADATA case when statement in WHERE clause

I need to write a case statement in the WHERE clause, which is -
when current_date is 1st of Month then select data BETWEEN 1st day of prev month AND last day prev month
ELSE FROM 1st of Curr month till date. I have written this so far but it is not working. '05/01/2017' will be input date.
SELECT *
FROM MyTable
WHERE calendar_date
BETWEEN
CASE WHEN extract (day from CAST( '05/01/2017' AS DATE FORMAT 'MM/DD/YYYY')) =1 --check to see date is 1st of month
THEN ADD_MONTHS((CAST( '05/01/2017' AS DATE FORMAT 'MM/DD/YYYY') - EXTRACT(DAY FROM CAST( '05/01/2017' AS DATE FORMAT 'MM/DD/YYYY'))+1), -1) --1st of prev month
AND ADD_MONTHS(CURRENT_DATE - EXTRACT(DAY FROM CURRENT_date), 0 ) --last day prev month
ELSE CAST( '05/01/2017' AS DATE FORMAT 'MM/DD/YYYY') - EXTRACT(DAY FROM CAST( '05/01/2017' AS DATE FORMAT 'MM/DD/YYYY'))+1, 0) --else 1st of Curr mont
AND CURRENT_DATE
end
order by calendar_date
select *
from mytable
where calendar_date between case
when td_day_of_month (current_date) = 1
then current_date - interval '1' month
else td_month_begin (current_date)
end
and case
when td_day_of_month (current_date) = 1
then current_date - 1
else current_date
end
order by calendar_date
#Dudu, based on your suggestion, I was able to solve the sql. here it is:
SELECT * FROM MYTABLE
WHERE CALENDAR_DATE
BETWEEN
CASE WHEN extract (day from CAST( '05/15/2017' AS DATE FORMAT 'MM/DD/YYYY')) = 1
THEN (ADD_MONTHS((CAST( '05/15/2017' AS DATE FORMAT 'MM/DD/YYYY') - EXTRACT(DAY FROM CAST( '05/15/2017' AS DATE FORMAT 'MM/DD/YYYY'))+1), -1) )
ELSE ADD_MONTHS(CURRENT_DATE - EXTRACT(DAY FROM CURRENT_DATE)+1, 0)
END
AND
CASE WHEN extract (day from CAST( '05/15/2017' AS DATE FORMAT 'MM/DD/YYYY')) > 1
THEN CURRENT_DATE
ELSE ADD_MONTHS(CURRENT_DATE - EXTRACT(DAY FROM CURRENT_date), 0 )
END
AND REPORTNAME_MASTER_ID IN (2565,5216,5364)
order by CALENDAR_DATE

SQL SELECT query between 2 unknown dates

I was trying to find symptoms between 2 dates. The date is obtained from a table and is not current date. The range is date +1 day to date+ 5 days. I wrote this code but it does not seem to work.
Where am I going wrong?
SELECT answer
FROM column
WHERE BETWEEN date(date,'+1 day') AND date(date, '+5 days')
--maybe like this :)
--dateadd to +5 days
SELECT x
FROM date a
WHERE a.yourDate BETWEEN a.yourDate and DateAdd(day, 5, a.yourDate)
--using GateDate();
SELECT x
FROM date a
WHERE a.yourDate BETWEEN getdate() and DateAdd(day, 5, getdate())

How to calculate previous month data on starting of next month in Teradata

I am using condition below to show data for current month only/previous month(if its new month started).
let's say today is 3rd june so it should only give 1st an 2nd data,if its 10 it should give data from 1 to 9th,similarly it should be like this until month end but when there will be 1st of next month it should give data from previous month. the conditions I am using are giving blank data on every 1st. . Here is the condition I am using:
where Datestarted between CURRENT_DATE - EXTRACT( DAY FROM CURRENT_DATE) + 1
AND
ADD_MONTHS(( CURRENT_DATE - EXTRACT (DAY FROM CURRENT_DATE) + 1), 1) - 1
You probably need to base your calculation on yesterday, not today:
where Datestarted between (CURRENT_DATE-1) - EXTRACT( DAY FROM CURRENT_DATE-1) + 1
AND
CURRENT_DATE-1
Do you maybe mean previous month to date?
Try this:
select
calendar_date
from
sys_calendar.calendar
where
calendar_date between cast((ADD_MONTHS(CURRENT_DATE,-1)/100*100)+1 as DATE) and
add_months(current_date,-1)-1
EDIT: Based on your comments, let's try this:
SELECT
calendar_date
FROM
sys_calendar.CALENDAR
WHERE
(EXTRACT(DAY FROM CURRENT_DATE) = 1 --previous month
AND
Calendar_Date BETWEEN CAST((ADD_MONTHS(CURRENT_DATE, - 1)/100 * 100) + 1 AS DATE)
AND CURRENT_DATE - EXTRACT(DAY FROM CURRENT_DATE))
OR--current mtd
(
Calendar_Date BETWEEN CAST((CURRENT_DATE / 100 * 100) + 1 AS DATE)
AND CURRENT_DATE - 1
)
ORDER BY calendar_date

T-SQL - Determine number of specific days between 2 dates based on sales

The Problem: Given a day of the week (1, 2, 3, 4, 5, 6, 7), a starting date and an ending date, compute the number of times the given day of the week appears between the starting and ending dates not inclusive of a date for which there were no sales.
Context:
Table "Ticket" has the following structure and sample content:
i_ticket_id c_items_total dt_create_time dt_close_time
----------------------------------------------------------------------------
1 8.50 '10/1/2012 10:23:00' '10/1/2012 11:05:05'
2 10.50 '10/1/2012 11:00:00' '10/1/2012 11:45:05'
3 8.50 '10/2/2012 08:00:00' '10/2/2012 09:25:05'
4 8.50 '10/4/2012 08:00:00' '10/4/2012 09:25:05'
5 7.50 '10/5/2012 13:22:23' '10/5/2012 14:33:27'
.
.
233 6.75 '10/31/2012 23:20:00' '10/31/2012 23:55:39'
Details
There may or may not be any tickets for one or more days during a month. (i.e. the place was closed that/those day/s)
Days in which the business is closed are not regular. There is no predictable pattern.
Based on Get number of weekdays (Sundays, Mondays, Tuesdays) between two dates SQL,
I have derived a query which returns the number of times a given day of the week occurs between the start date and the end date:
DECLARE #dtStart DATETIME = '10/1/2013 04:00:00'
DECLARE #dtEnd DATETIME = '11/1/2013 03:59:00'
DECLARE #day_number INTEGER = 1
DECLARE #numdays INTEGER
SET #numdays = (SELECT 1 + DATEDIFF(wk, #dtStart, #dtEnd)-
CASE WHEN DATEPART(weekday, #dtStart) #day_number THEN 1 ELSE 0 END -
CASE WHEN DATEPART(weekday, #dtEnd) <= #day_number THEN 1 ELSE 0 END)
Now I just need to filter this so that any zero-dollar days are not included in the count. Any help you can provide to add this filter based on the contents of the tickets table is greatly appreciated!
If I understand correctly, you can use a calendar table to count the number of days where the day of week is n and between the start and end and is a date that has ticket sales, which I guess is when the date exists in tickets and has the sum(c_items_total) > 0
WITH cal AS
(
SELECT cast('2012-01-01' AS DATE) dt, datepart(weekday, '2012-01-01') dow
UNION ALL
SELECT dateadd(day, 1, dt), datepart(weekday, dateadd(day, 1, dt))
FROM cal
WHERE dt < getdate()
)
SELECT COUNT(1)
FROM cal
WHERE dow = 5
AND dt BETWEEN '2012-04-01' AND '2012-12-31'
AND EXISTS (
SELECT 1
FROM tickets
WHERE cast(dt_create_time AS DATE) = dt
GROUP BY cast(dt_create_time AS DATE)
HAVING sum(c_items_total) > 0
)
OPTION (MAXRECURSION 0)
SQLFiddle

Resources