Compare date columns - teradata

I need to retrieve the rows that service_date is greater than prod_date.
The data type for prod_date is VARCHAR(10) (2/20/2014 for example) and the data type for service_date is DATE (YYYYMMDD). If I query service_date using "select service_date from service where service_date ='20140201'", the result is showing "2/1/2014" in the result grid. However, it does not work in the query below when I convert service_date to varchar to compare with prod_date. It pulls out all the rows instead of the ones that have greater service_date.
SELECT P.PROD_ID, P.PROD_DESC, S.PROD_ID, S.SERVICE_LOC
FROM PRODUCT P
INNER JOIN SERVICE S
WHERE P.PROD_ID = S.PROD_ID
AND CAST(S.SERVICE_DATE AS VARCHAR(10)) >= P.PROD_DATE

I suggest you use date ordering instead of string/varchar ordering if possible for simplicity and since its [ probably ] closer to what your interested in and less likely to confuse
For example
'01/02/2014' >= '04/01/2013' -- if these are dates or cast to dates
but
'01/02/2014' < '04/01/2013' -- if these are strings
So to keep things simple, it makes sense to cast PROD_DATE to a date when comparing these two fields like :
SELECT P.PROD_ID, P.PROD_DESC, S.PROD_ID, S.SERVICE_LOC
FROM PRODUCT P
INNER JOIN SERVICE S
WHERE P.PROD_ID = S.PROD_ID
AND S.SERVICE_DATE >= cast(P.PROD_DATE as date format 'DD/MM/YYYY')
;
if theres any doubts on prod_dates quality as valid dates can check the conversion on all dates first ( before running/adjusting above )

This isn't 100% error proof given your date is character and could have unexpected values. It does show how you can append a leading 0 to the month value and cast it to a date when the month is determined to be a single digit based on the location of the / in the second position of the PROD_DATE value for a given row.
SELECT CASE WHEN POSITION('/' IN TRIM(P.PROD_DATE)) = 2
THEN CAST('0'|| TRIM(P.PROD_DATE) AS CHAR(10)) AS DATE FORMAT 'MM/DD/YYYY')
ELSE CAST(P.PROD_DATE AS DATE FORMAT 'MM/DD/YYYY')
END AS PROD_DATE_
FROM PRODUCT P;

Related

Creating datetime data on hour frequency by using date and hour column in integer type in Hive

I have a table including date column and hour column which is an integer type column varying from 0 to 24. I need to combine these two fields and create an hourly composite datetime field.
However, I was able to create that kind of variable by using || and cast. But I am unable to transform this code to Hive editor syntax. Can you help me with this problem
SQL Code:
CAST(CAST(CAST(DATE_OF_TRANSACTION AS FORMAT 'yyyy-mm-dd') AS VARCHAR(11))||' '||CAST(CAST( BasketHour AS FORMAT '99:') AS VARCHAR(10))||'00:00' AS TIMESTAMP(0)) Date_Time
Thank you very much
For example like this:
cast(concat(DATE_OF_TRANSACTION, ' ', lpad(BasketHour ,2,0),':00:00.0' ) as timestamp)

SQL: Convert dd/mm/yyyy to yyyyQ1

How to convert date to financial quarters:
3/31/2018 to 2018q1
I pulled a dataset from the FDIC website. Their date format is currently dd/mm/yyyy.
I am interested in creating a Scatter Plot/Bubble Chart using Gapminder.
However Gapminder needs each date to be converted to financial quarters. e.g. yyyyq1, yyyyq2, yyyyq3, or yyyyq4. e.g. 20017q1, 20017q2, 20017q3, or 2017q4.
This query needs to convert the date to financial quarters, but doesn't already do so. What needs to be added to convert "repdte" output dd/mm/yyyy to yyyyq1?
SELECT
PCR.name,
PCR.repdte as Quarter,
PCR.idlncorr as NetLoansAndLeasesToCoreDeposits,
CAST(LD.IDdeplam as int) as DepositAccounts$GreaterThan$250k
from All_Reports_20180630_Performance_and_Condition_Ratios as PCR
join
'All_Reports_20180630_Deposits_Based_on_the_$250,000_Reporting_Threshold'
as LD on PCR.cert = LD.cert
UNION ALL
SELECT
PCR.name,
PCR.repdte as Quarter,
PCR.idlncorr as NetLoansAndLeasesToCoreDeposits,
CAST(LD.IDdeplam as int) as DepositAccounts$GreaterThan$250k
FROM All_Reports_20180331_Performance_and_Condition_Ratios as PCR
JOIN
'All_Reports_20180331_Deposits_Based_on_the_$250,000_Reporting_Threshold'
as LD on PCR.cert = LD.cert
What I currently have
Quarter
03/31/2018
The format that Gapminder needs to render the Bubble Chart:
ReportDate
2009q1
I believe that using
substr(PCR.repdte,7,4)||'q'||CAST(1+((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER)
will convert the date for you.
For example, consider the following :-
DROP TABLE IF EXISTS PCR;
CREATE TABLE IF NOT EXISTS PCR (repdte);
INSERT INTO PCR VALUES('01/31/2009'),('02/31/2009'),('03/31/2009'),('04/31/2009'),('05/31/2009'),('06/31/2009'),('07/31/2009'),('08/31/2009'),('09/31/2009'),('10/31/2009'),('11/31/2009'),('12/31/2009');
SELECT PCR.repdte,
substr(PCR.repdte,7,4)||'q'||CAST(1+((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER) FROM PCR;
Which results in :-
Additional
Re comment :-
It works. However, I'm getting an output of '018q2' instead of
'2018q2'. What would I change to add a '2' to '018q2'?
This would appear to be due to the date have a variable length day part, that is if the day part is less then 10 then it is a single numeric rather than being padded with 0 and two numerics when 10 or more.
The following could be used :-
replace(substr(PCR.repdte,6),'/','')||'q'||CAST(1+((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER)
this works by taking the year from from the 6th character and removing the / if it exists, consider the following
:-
DROP TABLE IF EXISTS PCR;
CREATE TABLE IF NOT EXISTS PCR (repdte);
INSERT INTO PCR VALUES('01/31/2009'),('02/1/2009'),('03/31/2009'),('04/31/2009'),('05/1/2009'),('06/31/2009'),('07/31/2009'),('08/1/2009'),('09/31/2009'),('10/31/2009'),('11/31/2009'),('12/31/2009');
SELECT PCR.repdte,
substr(PCR.repdte,7,4)||'q'||CAST(1+((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER), -- OLD
replace(substr(PCR.repdte,6),'/','')||'q'||CAST(1+((substr(PCR.repdte,1,2)-1) / 3) AS INTEGER) -- MODIFIED
FROM PCR;
Which results in :-

Difference between table row values in the same

I have a SQLite table:
CREATE TABLE `Readings` ( `ID` TEXT, `Reading` TEXT, `Date` TEXT )
Every Date I have real Readings from different sensors identified by IDs. Is it possible to get a result table with differences between Readings from sensors with the same ID but for different Dates?
Assuming that you are using a proper date format, you can look up the corresponding previous value with a correlated subquery:
SELECT ID,
Date,
Reading - (SELECT Reading
FROM Readings AS R2
WHERE R2.ID = Readings.ID
AND R2.Date < Readings.Date
ORDER BY Date DESC
LIMIT 1
) AS Difference
FROM Readings;

SQlite: Select rows within a specific time range, ignoring the date

I have a table called messages that stores messages from a chat with the following columns: username, message, datetime, where the type of datetime is TEXT and it is stored in the following format: "yyyy/MM/dd hh:mm:ss". I want to retrieve the average count of rows within a specific time range, without bothering with the date. For instance:
SELECT avg(count(message))
FROM messages
WHERE datetime < "2016/mm/dd 13:00:00" AND
datetime > "2016/mm/dd 12:00:00"
Is there some operator that allows any character to take the place of "mm" and "dd". Essentially, I am trying to construct a query that retrieves the average amount of messages within a specific time range, not the amount of messages on a specific date.
If I read your question correctly, you want to use your WHERE clause to restrict to any calendar date in 2016 between 12 and 13 hours. In this case, you can use STRFTIME to extract the year and hour in string format from your datetime column.
SELECT COUNT(message)
FROM messages
WHERE STRFTIME('%Y', datetime) = '2016' AND
STRFTIME('%H', datetime) < '13' AND
STRFTIME('%H', datetime) > '12'
Note that the reason while the inequalities should work with strings is because numerical strings still sort based on their lexigraphical order.
Update:
Since your datetime column is in a non standard format, you may be able to workaround this by substringing off the various pieces you need to use in the WHERE clause:
SELECT COUNT(message)
FROM messages
WHERE SUBSTR(datetime, 1, 4) = '2016' AND
SUBSTR(datetime, 12, 2) < '13' AND
SUBSTR(datetime, 12, 2) > '12'

Error in repeating table sum in InfoPath

I'm using InfoPath 2003 to produce a form which includes a repeating table of records that include a date field (StartDate) and a value field (TotalElapsed). I have date pickers for start and end dates on the form (beginDate and endDate), and there is a text box after the table which I want to have show the total sum of the integer field for records that have a date between the start and end date selections. The text box value parameter generated when I use the 'Insert Field or Group...' and 'Filter Data...' options, is below:
sum(#TotalElapsed[msxsl:string-compare(#StartDate, beginDate) >= 0 and msxsl:string-compare(#StartDate, endDate) <= 0])
This gives almost the correct sum calculation, with the exception that any records with a date that matches the end date are not included in the sum. Any records with dates from (and including) the start date, up to the day before the end date, are all included in the sum. Any ideas why the end date records aren't included in the sum?
Thanks
The above equation works fine. To use the >= and <= the Start Date should be in type of Date.
sum(Total[msxsl:string-compare(StartDate, BeginDate) >= 0 and msxsl:string-compare(StartDate, EndDate) <= 0])

Resources