Get Inspire insights from former attendees in our AMA discussion thread on Inspire Buzz. ACEs and other community members are on call all week to answer!

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Seeking SQL to pull data from current and prior year only

rdasilva
6 - Meteoroid

Hello, 

 

Anyone know the correct code to pull data from current and prior year only? 

 

rdasilva_0-1656094748998.png

 

Thank you all. 

6 REPLIES 6
FrederikE
13 - Pulsar

Try: 

where

YEAR("INVOICE_DATE_PK")='2021' OR 

YEAR("INVOICE_DATE_PK'")='2022'

 

Works only if the field is stored in the database as a data field type. Otherwise, you could try to use SUBSTRING/LEFT. 

DataNath
17 - Castor

Try replacing your ‘where’ line with:

 

 

WHERE YEAR(“INVOICE_DATE_PK”) > 2020

Robin_McIntosh
11 - Bolide

And if you want to make it dynamic, you could use the below WHERE statement and then update YRS by passing the number of years to go back.

 

WHERE year(invoice_date_pk) >= year(current date) -YRS

 

 

rdasilva
6 - Meteoroid

Hi Robin - Thank you for the suggestion. I tired but did not work. 

 

rdasilva_0-1656110522629.png

 

DataNath
17 - Castor

@rdasilva I believe Robin just put 'current year' and 'YRS' there as placeholders. To get this working in your case it'd be

 

WHERE YEAR(“INVOICE_DATE_PK”) >= YEAR(GETDATE()) -2

 

Where you would change the -2 as mentioned if you wanted to go further back any more years.

Robin_McIntosh
11 - Bolide

@rdasilva This was based on DB2 SQL so the "current date" reference could be different depending on what you're using.

Labels