Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 2355

Help with HANA SQL Query please?

$
0
0

Hi all,

 

I am trying to build a query to capture the following scenario - if a customer has previously bought a product, but has not reordered it within the last 30 days, the query will pull a list of customers, with the last invoice date for the product, product information, last quantity sold, and then in this case, the salespersons name also.

 

I have the query below written, which I thought was working fine, but I have just noticed that some items are being pulled onto the query, even though they have been ordered / invoiced within the last 30 days...

 

I'm not sure where I have gone wrong, I was hoping someone here could help?

 

Thanks,

 

Ali

 

SELECT Z."CardCode", Z."CardName", MAX(Z."DocDate"), Z."ItemCode", Z1."ItemName", SUM(Z."Quantity"), Z."SlpName"

 

FROM (

 

SELECT T0."CardCode", T0."CardName", MAX(T0."DocDate") AS "DocDate", T2."ItemCode", SUM(T2."Quantity") AS "Quantity", T3."SlpName"

FROM OINV T0
LEFT JOIN OCRD T1 ON T0."CardCode" = T1."CardCode"
INNER JOIN INV1 T2 ON T0."DocEntry" = T2."DocEntry"
INNER JOIN OSLP T3 ON T0."SlpCode" = T3."SlpCode"

WHERE T0."CANCELED" = 'N' AND YEAR(T0."DocDate") = YEAR(CURRENT_DATE) AND T2."Quantity" > 0

GROUP BY T0."CardCode", T0."CardName", T2."ItemCode", T3."SlpName"

 

UNION ALL

 

SELECT T0."CardCode", T0."CardName", MAX(T0."DocDate") AS "DocDate", T2."ItemCode", SUM(T2."Quantity") AS "Quantity", T3."SlpName"

FROM ORDR T0 
INNER JOIN OCRD T1 ON T0."CardCode" = T1."CardCode"
INNER JOIN RDR1 T2 ON T0."DocEntry" = T2."DocEntry"
INNER JOIN OSLP T3 ON T0."SlpCode" = T3."SlpCode"

WHERE T0."CANCELED" = 'N' AND T0."DocStatus" = 'O' AND YEAR(T0."DocDate") = YEAR(CURRENT_DATE)

GROUP BY T0."CardCode", T0."CardName", T2."ItemCode", T3."SlpName"


ORDER BY T0."CardCode" ) AS Z

 

INNER JOIN OITM Z1 ON Z."ItemCode" = Z1."ItemCode"

 

WHERE Z."DocDate" <= ADD_DAYS (CURRENT_DATE,-30) AND Z."SlpName" = 'John Doe'

 

GROUP BY Z."CardCode", Z."CardName", Z."ItemCode", Z1."ItemName", Z."SlpName"


Viewing all articles
Browse latest Browse all 2355

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>