Skip to content
Advertisement

Vertica: How to create a new column by subtracting two other columns?

I have a Vertica table named Start_End like:

It has about 100,000 rows.

How can I use months_between to subtract Started from Ended?

The resulting table would look like:

The following will add a blank column:

How can I use months_between?

Advertisement

Answer

Check here: https://www.vertica.com/docs/9.3.x/HTML/Content/Authoring/SQLReferenceManual/Functions/Date-Time/MONTHS_BETWEEN.htm?zoom_highlight=MONTHS%20BETWEEN

to find out if the behaviour of MONTHS_BETWEEN() (when to return INTEGER, when to return FLOAT, last-day-of-month behaviour, etc) is the one you need. Otherwise, you might want to DATE_TRUNC() the two operands first, for example, as @Gordon Linoff suggests. Or use TIMESTAMPDIFF(month, …) instead: https://www.vertica.com/docs/9.3.x/HTML/Content/Authoring/SQLReferenceManual/Functions/Date-Time/TIMESTAMPDIFF.htm?zoom_highlight=timestampdiff

That said, try this for size:

Good luck …

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement