How to generate this teradata table? - html-table

I would like to create a Teradata table with a table structure that looks like below.
12-29-2008 will be start date. Basically I'd like to assign this as the start date with week number corresponding to this as 209. Everything else afterwards should follow automatically i.e. calculated.
Column 1/Weekstart/Monday Week num
12/28/2008 209
1/5/2009 210
...
12/27/2021 887
I tried below but it could only create one row.
Please see image attached for an idea of what I want as the final dataset.
Table structure image is below
SEL
WeekBegin
, (WeekBegin - DATE '2008-12-29') / 7 + 209 as week_num
FROM Sys_Calendar.BusinessCalendar
WHERE calendar_date = DATE '2008-12-29'
;
Thanks for your help!
Edit: After doing below-
SEL WeekBegin
, (WeekBegin - DATE '2008-12-29')/7 + 209 as week_num
FROM Sys_Calendar.BusinessCalendar
WHERE
calendar_date >= DATE '2008-12-29'
and calendar_date <= DATE '2021-12-27';
My result looks like this:
ResultsImage1
ResultsImage2
It is not displaying the Monday of the week. Instead I see Sunday at the end of the table. Also the weeknum is 886, instead it should be 887.

Related

Populate a field with the next Monday in April

I need to create a new field in my select query which selects the Next first Monday of April if the date is greater than '2003-03-31' is that possible.
For example i have a date 2014-04-21 so I need the calculated date to return as 2015-04-6.
Second example would be 2020-01-13 so the calculated date would need to return 2020-04-6
UPDATED
Data is below:
What I have been attempting is:
CASE
WHEN Date <= '2003-03-31' THEN
'2004-04-5'
WHEN Date > '2003-03-31' THEN
????? (First Monday of the next April)
END AS 'Test1',
This is where I m stuck sorry I was so vague.
As I believe that you have written a small mistake in your question :
Second example would be 2020-01-13 so the calculated date would need
to return 2010-04-6
You wanted to write :
Second example would be 2020-01-13 so the calculated date would need
to return 2020-04-6
Here is how you can do it:
SELECT Date_c
, case when Date_c >= DATEADD(DAY, (9 - DATEPART(dw,CONVERT(date,concat(year(Date_c), '-04-01')))), CONVERT(date,concat(year(Date_c), '-04-01')))
then DATEADD(DAY, (9 - DATEPART(dw,CONVERT(date,concat(year(Date_c)+1, '-04-01')))), CONVERT(date,concat(year(Date_c)+1, '-04-01')))
else DATEADD(DAY, (9 - DATEPART(dw,CONVERT(date,concat(year(Date_c), '-04-01')))), CONVERT(date,concat(year(Date_c), '-04-01')))
end as NEXTMONDAY
from testT
where Date_c > '2003-03-31';
Here is the DEMO

How to get last one month's data from a table based on current month and year?

I am facing some problem with the hive code.
My FROM TABLE is partitioned based on month, year and day. I came up with the following code to get the data I need. The logic is something like if the current mth is 01 then change the month to 12 and the year to yr - 1
else change month to mth - 1 and keep the year as is.
set hivevar:yr=2019;
set hivevar:mth=03;
set hivevar:dy=29;
SELECT * from
FROM table
WHERE
month = case when cast('${mth}' as int) = 01 then 12 else cast((cast('${mth}' as int) - 1) as string) end
AND year = case when cast('${mth}' as int) = 01 then cast((cast('${yr}' as int) - 1) as string) else '${yr}' end;
It is not working, my select * is coming empty. Please help.
desc table
From what i understand, you are trying to get data from the previous month given a date. If so, you can use inbuilt date functions to do it.
select *
from table
where concat_ws('-',year,month,day) >= add_months(date_add(concat_ws('-','${yr}','${mth}','${dy}'),1-'${dy}'), -1)
and concat_ws('-',year,month,day) < date_add(concat_ws('-','${yr}','${mth}','${dy}'),1-'${dy}')
The solution assumes year, month and day are of the format yyyy, MM and dd. If not, adjust them as needed
Also, you should consider storing date as a column even though you have it partitioned by year,month and day.

current month record should be display from table

please assist me to correct it.i want to show current month data i tried this but its showing error the literal is too long :
number date
10 20-Jan-2018
20 30-Oct-2018
30 24-Sep-2018
24 01-Oct-2018
select number
from table
where date <= to_char(sysdate,'mm');
It looks like your date column contains text. You should ideally always store date information in proper date columns. As a workaround, we can go in the other direction and use TO_DATE on your date column, to compare it to the first of the current month:
SELECT number
FROM yourTable
WHERE TO_DATE(date, 'dd-mon-yyyy') < TRUNC(sysdate, 'mm');

Pull data from table based on the date of the first record

I am querying some data from a sql table based on dates entered by the user as below :
dt = as.Date(some_date)
# Manipulate dates
end_date = as.Date(dt)
begin_date = as.character(as.Date(end_date) - 364)
What happens after this is that all records in the table where the date field falls between the begin and end date are pulled.
qry <- paste0("select * from table
where date>= '", begin_date, "' and date <= '", end_date; ")
But sometimes it might happen that I do not have 1 years of data but only 10 or 9 or 8 months.
So I want to be able to change the 364 value as per the first date in the table.
So is there any way in R by which I can pull the records starting with the begin date as end_date - 364 and if that date does not exist in the table change the begin date to the first available date and run the query again.
I understand that this will require two passes of the dates and the query but I want to be able to do it iteratively without manually checking for the dates.
Your query will give you one year of data or all the data available in table, which seems to be your requirement. However, if you need to know if there is more than one year of data before selecting the data, than you can use
SELECT MIN(date) FROM table
to get the earliest date available.

Time range- Sql

please help me with my problem. So, I have a table named 'RATES' which contains these columns:
id (int)
rate (money)
start_time (datetime)
end_time(datetime)
example data:
1 150 8:00am 6:00pm
2 200 6:00pm 4:00am
3 250 8:00am 4:00am (the next day)
What I have to do is to select all the id(s) to where a given time would fall.
e.g given time: 9:00 pm, the output should be 2,3
The problem is I got this time range between 8am to 4am the next day and I don't know what to do. Help, please! thanks in advance :D
Assuming that #Andriy M is correct:
Data never spans more than 24 hours
if end_time<=start_time then end_time belongs to the next day
then what you're looking for is this:
Declare #GivenTime DateTime
Set #GivenTime = '9:00 PM'
Select ID
From Rates
Where (Start_Time<End_Time And Start_Time<=#GivenTime And End_Time>=#GivenTime)
Or (Start_Time=End_Time And Start_Time=#GivenTime)
Or (Start_Time>End_Time And (Start_Time>=#GivenTime Or End_Time<=#GivenTime))
I don't really ever use MS SQL, but maybe this will help.
I was going to suggest something like this, but by the way you have your data set up, this would fail.
SELECT id FROM RATES
WHERE datepart(hh, start_time) <= 9 AND datepart(hh, end_time) >= 9;
You'll have you search using the actual date if you expect to get the correct data back.
SELECT id FROM RATES
WHERE start_time <= '2011-1-1 9:00' AND end_time >= '2011-1-1 9:00';
This may not be exactly correct, but it may help you look in the right direction.
I guess #gbn is not going to help you. I will try and fill in.
Given -- a table called timedata that has ranges only going over at most one day
WITH normalized AS
(
SELECT *
FROM timedata
WHERE datepart(day,start_time) = datepart(day,endtime)
UNION ALL
SELECT id, rate, start_time, dateadd(second,dateadd(day,datediff(day,0,end_time),0),-1) as end_time
FROM timedata
WHERE not (datepart(day,start_time) = datepart(day,endtime))
UNION ALL
SELECT id, rate,dateadd(day,datediff(day,0,end_time),0) as start_time, end_time
FROM timedata
WHERE not (datepart(day,start_time) = datepart(day,endtime))
)
SELECT *
FROM normalized
WHERE datepart(hour,start_time) < #inhour
AND datepart(hour,end_time) > #inhour
This makes use of a CTE and a trick to truncate datetime values. To understand this trick read this question and answer: Floor a date in SQL server
Here is an outline of what this query does:
Create a normalized table with each time span only going over one day by
Selecting all rows that occur on the same day.
Then for each entry that spans two days joining in
Selecting the starttime and one second before the next day as the end time for all that span.
and
Selecting 12am of the end_time date as the starttime and the end_time.
Finally you perform the select using the hour indicator on this normalized table.
If your ranges go over more than one day you would need to use a recursive CTE to get the same normalized table.