i'm new here and it's all a bit confusing, so i'm gonna excuse myself in the beginning, if i do something wrong here.
I usually used MySQL or sometimes Oracle but now I have to switch to Teradata.
Simply i need to convert this:
SELECT FLOOR(DATEDIFF(NOW(),`startdate`)/365.25) AS `years`,
COUNT(FLOOR(DATEDIFF(NOW(),`startdate`)/365.25)) AS `numberofemployees`
FROM `employees`
WHERE 1
GROUP BY `years`
ORDER BY `years`;
into teradata.
Would be great if someone could help :)
Equivalent of your query in Teradata would be :
SELECT FLOOR((CURRENT_DATE - startdate)/365.2500) AS years,
COUNT(FLOOR((CURRENT_DATE - startdate )/365.2500)) AS numberofemployees
FROM employees
--WHERE 1
GROUP BY years
ORDER BY years;
CURRENT_DATE is equivalent to NOW() (without the time part, part DATEDIFF would have ignored it, anyway).
In Teradata you can simply subtract dates to get days in between.
Also, I added two zeroes at the end of 365.25 to force Teradata to evaluate the division to 4 decimal places, because MySQL seems to perform it that way (https://dev.mysql.com/doc/refman/8.0/en/arithmetic-functions.html#:~:text=In%20division%20performed%20with%20/%2C%20the%20scale%20of%20the%20result%20when%20using%20two%20exact-value%20operands%20is%20the%20scale%20of%20the%20first%20operand%20plus%20the%20value%20of%20the%20div_precision_increment%20system%20variable%20(which%20is%204%20by%20default).
But, I am not sure if Understood your original query thoroughly:
What does WHERE 1 do?
Why do you count the years column and call it numberofemployees (why not simply do count(*))
Related
(This is all steps in containers within an Alteryx flow that is connecting to a Teradata source)
My SQL is incredibly rusty as it's been almost 8 years since I've needed to use it. I know this is a quite basic question. Basically I have several queries that need to be manually adjusted monthly to shift the month. in YYYY-MM format. They look like this:
Is the main one where I just adjust one backwards one month
select DB.TABLE.field1, DB.TABLE.Year_month
from DB.TABLE
where DB.TABLE.Year_month = '2023-01'
This is the secondary one where I adjust one backwards one month, and the others are same month or plus a month or more.
and A.B_MONTH in ('2022-12-01', '2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01','2023-05-01')
and B.Year_month = '2023-01'
How do I adjust the where clause to always be the needed relative references?
Any help is greatly appreciated
I tried using concat but it choked for some reason.
You can try this:
select DB.TABLE.field1, DB.TABLE.Year_month
from DB.TABLE
where DB.TABLE.Year_month = DATE_FORMAT( NOW() - INTERVAL 1 MONTH, '%Y/%m')
I don't understand your second need, but you can do it similar to my response.
Just play with the NOW() - INTERVAL X.
Pretty basic stuff. ADD_MONTHS to move your month around, TO_CHAR for your desired format.
To get the previous month:
select to_char(add_months(current_date,-1), 'YYYY-MM')
I know there are a lot a ways to calculate the difference between to dates in different databases. My problem is that I need a way to do that that will work in both SQL Server and DB2 using the same SQL query.
On a yearly basis I can use
CURRENT_TIMESTAMP - [BIRTHDATE] AS AGETHISYEAR
But I also want to know a persons age in decimal form i.e. 34,4 depending on the persons birthday and current date.
In SQL Server the columntype for [BIRTHDATE] is DATETIME and in DB2 the column type is DATE.
The format is YYYY-MM-DD
Is there a way to do this?
Regards,
Alfred
I think the only way you can write a function that would work in both databases would be to use year(), month(), and day(), or to write functions yourself. There is not that much overlap in the date functions between the two databases.
For instance, to get the full years between two dates, you could use:
select (year(CURRENT_TIMESTAMP) - year(birthdate) +
(case when month(CURRENT_TIMESTAMP) * 100 + day(CURRENT_TIMESTAMP) <
month(birthdate) * 100 + day(birthdate)
then -1 else 0
end)
) as AgeInYears
Fractional years would be much more complicated. Of course, each database has its own more reasonable methods for getting what you want. The methods are different.
I have the dates of when employees have begun working at a zoo, however i need to figure out how long they have been working there for. I have done my research and know what i need to do, but i cant seem to figure out the syntax for incorporating the NOW() function within the DATEDIFF function. I have to display all active employees and the amount of years (with 2 decimal places) they have been working for.
I have two columns, Joined (Date) and Resigned (date, where may equal null if employee is active)
So lets just say that someone started working on 1996-09-18 (yyyy/mm/dd).
please help, thank you kindly.
(assuming you are using MySQL - from the NOW() function)
I think you need to use COALESCE() function so either Resigned or NOW() is used in the calculation by DATEDIFF():
DATEDIFF(COALESCE(Resigned, CURDATE()), Joined) AS days
So, you could have something like:
(DATEDIFF(COALESCE(Resigned, CURDATE()), Joined)) / 365 AS years
or:
(DATEDIFF(COALESCE(Resigned, CURDATE()), Joined)) / 365.25 AS years
If you want to be extremely accurate about extreme cases and leap years, a more complex calculation will be needed.
Years with 2 decimal places (Assuming TSQL)
SELECT ROUND(
CONVERT(FLOAT,
DATEDIFF(day,joined,ISNULL(resigned,GETDATE()) ))/365,2)
Im new to sqlite3 (and databases in general for that matter). Anyway, in my database i have a date column and i wanted to get all of the data within the 3 days of the current date. Here is the query I have. I am just not sure if there is a built in way to get nearby dates in sqlite3. I dont know what to put in the '?'s.
SELECT date FROM fooTable WHERE date('now') ???????
Try this (3 days is 3*86400 seconds):
SELECT date FROM fooTable WHERE strftime('%s', now) - strftime('%s',date) < 259200
How can I optimize this query if given the following query returns either all entries in the table or entries that match only up to current date ?
btw: The Query is targeted to a Oracle Linked Server on MS Sql 2005 as an Inline function.. Do not want this to be a table value function..
ALTER function [dbo].[ftsls031nnnHades](#withExpiredEntries bit =0)
returns table as return
select *
from openQuery(Hades ,"select '010' comno,
trim(t$cuno) t$cuno,
trim(t$cpgs) t$cpgs,
t$dile,
t$qanp,
to_char(t$stdt,'dd Mon yy') t$stdt,
to_char(t$tdat,'dd Mon yy') t$tdat,
to_char(t$disc,'999.99') t$disc,
t$damt,
t$cdis,
t$gnpr,
t$refcntd,
t$refcntu
from baan.ttdsls031010
where (to_char(t$Tdat,'yyyy-mm-dd') >= To_char(current_date,'yyyy-mm-dd'))
and (to_char(t$stdt,'yyyy-mm-dd') <= To_char(current_date,'yyyy-mm-dd'))
union all
select '020' comno,
trim(t$cuno) t$cuno,
trim(t$cpgs) t$cpgs,
t$dile,t$qanp,
to_char(t$stdt,'dd Mon yy') t$stdt,
to_char(t$tdat,'dd Mon yy') t$tdat,
to_char(t$disc,'999.99') t$disc,
t$damt,
t$cdis,
t$gnpr,
t$refcntd,
t$refcntu
from baan.ttdsls031020
where (to_char(t$tdAt,'yyyy-mm-dd') >= To_char(current_date,'yyyy-mm-dd'))
and (to_char(t$stdt,'yyyy-mm-dd') <= To_char(current_date,'yyyy-mm-dd')) ")
p.s: Column naming conventions may be alien to those who are of non BaaN .. Please excuese me for bringing up 'BaaN' conventions into StackOverflow.
Never perform any functional processing of your date column (t$Tdat and t$stdt are of this type, aren't they?) unless you have the corresponding function-based index. This approach doesn't allow you to use indexes on t$stdt and t$Tdat and drops the perfomance dramatically.
Instead, I would rewrite the where clause in the following way:
where t$Tdat >= current_date and t$stdt <= current_date
if current_date is of date type. If it's not, then you can use, for example, to_date(current_date, 'DD-MM-YYYY') instead of it.
Just in case be here now's tip - which is a good one - doesn't work:
you'll need to collect some data to know where time is being spent. Please read this OTN-thread to see how to do this for Oracle: http://forums.oracle.com/forums/thread.jspa?messageID=1812597. For SQL Server, the same principles apply: use their tools to find out where this query is spending time on.
Some general information you can share is:
How many rows are in those two tables
How many rows are returned by that query
Which indexes are present on those two tables
How long does the query currently take
What response time is acceptable, i.e. when are we done tuning
Regards,
Rob.
Not sure how much this will improve performance, but the first thing I'd do is replace the date to string conversion with just date functions. That is, use trunc() instead of to_char().
In the below way you can optimize the Baan Query
In Where condition use indexes and combine field if possible.
In where condition Use "Between/Inrange" when upper and lower limit specified.
Use "Refers To" if reference is available in data dictionary
Use few overlapping "Or" condition as possible
Use only selected field of table in select statement, Which is actually required.
Use "Order by" to get record in correct sorting format
If possible Don't use NOT INRANGE,BETWEEN,IN operators because that operator can scan full table.
Use commit.transaction() to prevent line being print twice.