# Failure SQL query

**URL:** <https://community.dremio.com/t/failure-sql-query/9065>\
**Category:** Uncategorized\
**Created:** [April 18, 2022, 5:51pm UTC](https://community.dremio.com/t/failure-sql-query/9065 "2022-04-18T17:51:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![2Claudia\_Lara](https://avatars.discourse-cdn.com/v4/letter/2/839c29/32.png) [@2Claudia\_Lara](https://community.dremio.com/u/2Claudia_Lara)\
**Post date:** [April 18, 2022, 5:51pm UTC](https://community.dremio.com/t/failure-sql-query/9065/1 "2022-04-18T17:51:36Z")

</div>

Hello

I’m new to using Dremio, I’m here looking for help.  
I have the following problem.

I want to use the DIFF\_DATE function, but before this function I am also using the LAG function.

When I try it with the LAG function, the software runs without any failure.  
But when I add DIFF\_DATE function the failure appears

I would like to know how to identify my fault in this consultation.

This is the query:

\*SELECT STORE\_NBR, ITEM\_NBR, SELL\_EACH\_QTY, SELL\_AVERAGE\_QTY, SELL\_DEVIATION\_QTY, SELL\_TREND\_QTY, SELL\_FCST\_QTY, SELL\_LATE\_QTY, SELL\_ERROR\_QTY, SHIP\_QTY, SHIP\_ERROR\_QTY, MUMD\_ERROR\_QTY, TOTAL\_ERROR\_QTY, SPOIL\_QTY, SEND\_PI\_IND, SUSPEND\_IND, STORE\_ONHAND\_ADJMT\_QTY, DIMINISH\_ONHAND\_ADJMT\_QTY, DROP\_SALE\_ONHAND\_ADJMT\_QTY, BOUNTY\_ONHAND\_ADJMT\_QTY, FINAL\_ONHAND\_ADJMT\_QTY, EXCLUDE\_ONHAND\_QTY, LAST\_NIGHT\_ONHAND\_QTY, NEXTDAY\_EST\_ONHAND\_QTY, ORDER\_POINT\_ONHAND\_QTY, SAFETY\_STOCK\_ONHAND\_QTY, LOW\_SALES\_POINT\_QTY, BIN\_ALRT, BIN\_QTY, BIN\_DYS\_TO\_WAIT, BIN\_CNTR, BIN\_DATE, LOGC\_ACTVD, FNL\_PRDCT\_PI\_QTY, CPI\_ADJMT\_QTY, BKRM\_ADJMT\_QTY, WHSE\_RCVG\_QTY, YEST\_RCVG\_PRBLM\_FLG, INIT\_TRNST\_QTY, YTDY\_TRNST\_QTY, ACCUM\_RCVD\_QTY, RCVNG\_CPLTN\_FLG, CAST(REPORT\_DATE as TIMESTAMP) AS REPORT\_DATE, CAST(NEXT\_DATE as TIMESTAMP) AS NEXT\_DATE

FROM  
( SELECT \*, LAG(REPORT\_DATE, 1)  
OVER (  
ORDER BY “REPORT\_DATE” ) NEXT\_DATE,

DATE\_DIFF (TIMESTAMP “REPORT\_DATE”, TIMESTAMP “NEXT\_DATE”) SALES\_DATE

FROM UserArea.CAM.NonSecure.“CAM\_replenishment”.Teradata.“K1\_WM\_VM.DLY\_FRESH\_ONHD\_SNAPSHOT” LIMIT 5 ) nested\_0\* .

Thank you

---

<div class="post-metadata">

**Author:** ![balaji.ramaswamy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.dremio.com/balaji.ramaswamy/32/843_2.png) [@balaji.ramaswamy](https://community.dremio.com/u/balaji.ramaswamy)\
**Post date:** [April 20, 2022, 7:44pm UTC](https://community.dremio.com/t/failure-sql-query/9065/2 "2022-04-20T19:44:05Z")

</div>

@2Claudia_Lara What is the error you are getting? Can you try using TIMESTAMPDIFF instead?

[https://docs.dremio.com/software/sql-reference/sql-functions/functions/timestampdiff/](https://docs.dremio.com/software/sql-reference/sql-functions/functions/timestampdiff/)
