TIMESTAMPDIFF fails on large resultsets

ERPgal

Member
Please Disregard this post! The problem is a data issue that stems from an apparent business process issue!

I have a query here that is causing me a headache. I am using the timestampdiff function and passing it an interval of SQL constant type SQL_TSI_DAY.

The function seems to work when dealing with tiny resultsets but fails under conditions where:
1. I have a huge result set
2. I am using the date field as part of my where clause

Tell me there is a way to get this thing to work :confused:

Select v.name
, cast(v.vendorid as varchar(25)) as vendorid
, cast(month(pr.duedate) as varchar(25))as MonthDue
, cast(year(pr.duedate)as varchar(25)) as YearDue
, rd.receiptdate
,'' as rollingAvg
, '' as OT
, cast(pr.relqty as varchar(25)) as relqty
, cast(pr.receivedqty as varchar(25)) as receivedqty
,case when (pr.duedate < curdate() AND rd.receiptdate is null ) then cast(pr.relqty as varchar(25))
end as pastDueQty
,case when (rd.receiptdate < pr.duedate and abs(timestampdiff(SQL_TSI_DAY,rd.receiptdate,pr.duedate))>6) then Cast(pr.receivedQty as varchar(25))end as earlyQty
,case when (rd.receiptdate > pr.duedate and abs(timestampdiff(SQL_TSI_DAY,rd.receiptdate,pr.duedate))>6) then Cast(pr.receivedQty as varchar(25)) end as lateQty
,case when rd.receiptdate <> pr.duedate then Cast(pr.receivedQty as varchar(25)) end as earlyLateQty
,case when (abs(timestampdiff(SQL_TSI_DAY,rd.receiptdate,pr.duedate))<=6)then Cast(pr.receivedQty as varchar(25))
end as onTimeQty
,case when pr.receivedqty = pr.relqty then '1'
else '0'
end as fillRate
,abs(timestampdiff(SQL_TSI_DAY,rd.receiptdate,pr.duedate)) as DaysOff

FROM ((((pub.podetail pd inner join pub.porel pr
on (pd.company = 'ACME'
and pd.company = pr.company
and pd.ponum = pr.ponum
and pd.poline = pr.poline
and pd.ordernum = pr.ordernum
and pd.orderline = pr.orderline
and pd.voidline = 0
)
) --1
inner join pub.poheader ph
on ( ph.ponum = pd.ponum
and ph.ordernum = pd.ordernum
and ph.vendornum = pd.vendornum
and ph.company = pd.company
and ph.voidorder = 0
and (ph.openorder = 1
or ph.openorder = 0)
)
) --2
left join pub.rcvDtl rd
on ( rd.company = ph.company
and rd.vendorNum = pd.VendorNum
and rd.partnum = pd.PartNum
and rd.ponum = pr.PONum
and rd.poline = pr.poline
and rd.porelnum = pr.porelnum
)
) --3
left join pub.vendor v
on (pd.vendornum = v.VendorNum
and pd.company = v.company
)
) --4
WHERE ph.company = 'ACME'
and ph.voidorder = 0
and (ph.openorder = 1
or ph.openorder = 0)
and pd.voidline = 0
AND (pr.duedate between '1/1/2008' AND '12/1/2009')
ORDER BY pr.duedate desc, v.vendorid desc
 
Top