Avatar of armgon
armgon
Flag for United States of America

asked on 

SQL Subquery Syntax Assistance

Help greatly appreciated with formatting a SQL Query

I need to select from an ARDOC table  Credit Memo documents that are older than the oldest Invoice in the same table.
Both of which have a DOCBAL<>0 and for the same CUSTID.

I am basically identifying Credit Memos with DOCBAL <>0 that are older than the oldest Invoice with a  DOCBAL <>0 within a CUSTID.

One document per row in the table. Table Contains both CM and IN type entries.

Credit Memos are DOCTYPE='CM'
Invoices are DOCTYPE='IN'

Here are a couple of queries I have:

Your help in formatting these to yield the results is truly appreciated:

select CustId,RefNbr,DocType,DocDate,OrigDocAmt,DocBal
from ARDoc
where DocType = 'CM'
and DocBal<>0


select CustId,RefNbr,DocType,DocDate,OrigDocAmt,DocBal
from ARDoc
where DocType = 'IN'
and DocBal<>0
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
armgon

8/22/2022 - Mon