Datediff Function in Teradata Sql?
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...
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 :)
1 Answer
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 ((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(*))