I have two queries with a union all between them. I am trying to calculate the differences between two dates, one of them in the 1st query, and the other is in the 2nd query.
The 1st date in the 1st query is t0.duedate
, the 2nd date in the 2nd query is t0.docdate
.
I'll use this query in crystal report where i'll put a condition to order the resualt based on the field t0.[u_a_id] which available in both queries
the resualt right now is like this
DocENTRY slpname DocNum SeriesName Series DocType DocDate DocdueDate CardCode CardName DocTotal u_a_id type
1 - 1 Primary 12 C 2006-01-31 00:00:00.000 2006-01-31 00:00:00.000 C20000 Norm Thompson 14023.800000 NULL A/R INCOMING PAYMENT
323 Sales Manager 323 Primary 1 I 2012-06-09 00:00:00.000 2012-07-09 00:00:00.000 C20000 Maxi-Teq 4253.250000 NULL A/R INVOICE
i'll need to have one more column need Days Difference which will calculate the diffrences between days of the two dates "2012-07-09 - 2006-01-31" like this:
Days Difference
1921 Day
Here is the query :
SELECT DISTINCT
t0.[DocENTRY] ,
oslp.slpname ,
t0.[DocNum] ,
nnm1.SeriesName ,
t0.[Series] ,
t0.[DocType] ,
t0.[DocDate] ,
t0.[DocdueDate] ,
t0.[CardCode] ,
t0.[CardName] ,
t0.[DocTotal] ,
t0.[u_a_id] ,
'A/R INVOICE' AS type
FROM OINV t0
INNER JOIN NNM1 ON nnm1.series = t0.Series
INNER JOIN OSLP ON oslp.slpcode = t0.slpcode
WHERE t0.DOCSTATUS = 'C'
UNION ALL
SELECT DISTINCT
t0.[DocENTRY] ,
'-' AS slpname ,
t0.[DocNum] ,
nnm1.SeriesName ,
t0.[Series] ,
t0.[DocType] ,
t0.[DocDate] ,
t0.[DocdueDate] ,
t0.[CardCode] ,
t0.[CardName] ,
t0.[DocTotal] ,
t0.[u_a_id] ,
'A/R INCOMING PAYMENT' AS type
FROM orct t0
INNER JOIN NNM1 ON nnm1.series = t0.Series
ORDER BY t0.[CardCode]