Here is a sample set of data:
[tt]
ACCNUM NAME REP DOCTYPE TOTAL
CUST005 Y 2 I 16348.00
CUST005 Y 2 C 16348.00
CUST005 Y 2 I 16208.00
CUST005 Y 2 C 16208.00
CUST005 Y 2 I 16188.00
CUST005 Y 2 C 450.00
CUST013 Z 2 I 17056.68
CUST013 Z 2 C 17056.68
CUST020 X 2 I 18650.40
[/tt]
... from this SQL statement:
SELECT ACCNUM, NAME, REP, DOCTYPE, TOTAL, TOTALVAT FROM INVOICES
WHERE REP = 2
ORDER BY ACCNUM ASCENDING, TOTAL DESCENDING
Doctype:
I = invoice
C = credit note
I want to end up with the following result:
[tt]
ACCNUM NAME REP INVCOUNT CRNCOUNT NETTSALES
CUST020 X 2 1 0 18650.40
CUST013 Z 2 0 0 0.00
CUST005 Y 2 1 1 15738.00
[/tt]
CUST020 has one invoice and no credit notes.
CUST013 has one invoice, but the order was cancelled, so he hasn't actually ordered anything.
CUST005 has two cancelled invoices, one invoice after that and one credit note. 16188 - 450 = 15738
How do I translate this into SQL?
I am using DBISAM version 3.21, so please keep the solution as rudimentary as possible.
------------------------
I currently use the following SQL, but it gives me the incorrect results with respect to INVCOUNT and CRNCOUNT:
SELECT COUNT(DOCTYPE)INVCOUNT, NAME, SUM(TOTAL - TOTALVAT)INVTOTAL INTO RESULT1 FROM INVOICES
WHERE (DOCTYPE = 'I' AND REP = 2)
GROUP BY NAME
ORDER BY NAME ASCENDING;
SELECT COUNT(DOCTYPE)CRNCOUNT, NAME, SUM(TOTAL - TOTALVAT)CRNTOTAL INTO RESULT2 FROM INVOICES
WHERE (DOCTYPE = 'C' AND REP = 2)
GROUP BY NAME
ORDER BY NAME ASCENDING;
SELECT * INTO RESULT3 FROM RESULT1 LEFT OUTER JOIN RESULT2 ON
RESULT1.NAME = RESULT2.NAME;
UPDATE RESULT3 SET CRNCOUNT = 0 WHERE CRNCOUNT IS NULL;
UPDATE RESULT3 SET CRNTOTAL = 0 WHERE CRNTOTAL IS NULL;
SELECT NAME, INVCOUNT, CRNCOUNT, (INVCOUNT - CRNCOUNT)NETTCOUNT, INVTOTAL, CRNTOTAL, (INVTOTAL - CRNTOTAL)NETTSALES FROM RESULT3
ORDER BY NETTCOUNT DESCENDING
Thank you.
Regards
wickyd
[tt]
ACCNUM NAME REP DOCTYPE TOTAL
CUST005 Y 2 I 16348.00
CUST005 Y 2 C 16348.00
CUST005 Y 2 I 16208.00
CUST005 Y 2 C 16208.00
CUST005 Y 2 I 16188.00
CUST005 Y 2 C 450.00
CUST013 Z 2 I 17056.68
CUST013 Z 2 C 17056.68
CUST020 X 2 I 18650.40
[/tt]
... from this SQL statement:
SELECT ACCNUM, NAME, REP, DOCTYPE, TOTAL, TOTALVAT FROM INVOICES
WHERE REP = 2
ORDER BY ACCNUM ASCENDING, TOTAL DESCENDING
Doctype:
I = invoice
C = credit note
I want to end up with the following result:
[tt]
ACCNUM NAME REP INVCOUNT CRNCOUNT NETTSALES
CUST020 X 2 1 0 18650.40
CUST013 Z 2 0 0 0.00
CUST005 Y 2 1 1 15738.00
[/tt]
CUST020 has one invoice and no credit notes.
CUST013 has one invoice, but the order was cancelled, so he hasn't actually ordered anything.
CUST005 has two cancelled invoices, one invoice after that and one credit note. 16188 - 450 = 15738
How do I translate this into SQL?
I am using DBISAM version 3.21, so please keep the solution as rudimentary as possible.
------------------------
I currently use the following SQL, but it gives me the incorrect results with respect to INVCOUNT and CRNCOUNT:
SELECT COUNT(DOCTYPE)INVCOUNT, NAME, SUM(TOTAL - TOTALVAT)INVTOTAL INTO RESULT1 FROM INVOICES
WHERE (DOCTYPE = 'I' AND REP = 2)
GROUP BY NAME
ORDER BY NAME ASCENDING;
SELECT COUNT(DOCTYPE)CRNCOUNT, NAME, SUM(TOTAL - TOTALVAT)CRNTOTAL INTO RESULT2 FROM INVOICES
WHERE (DOCTYPE = 'C' AND REP = 2)
GROUP BY NAME
ORDER BY NAME ASCENDING;
SELECT * INTO RESULT3 FROM RESULT1 LEFT OUTER JOIN RESULT2 ON
RESULT1.NAME = RESULT2.NAME;
UPDATE RESULT3 SET CRNCOUNT = 0 WHERE CRNCOUNT IS NULL;
UPDATE RESULT3 SET CRNTOTAL = 0 WHERE CRNTOTAL IS NULL;
SELECT NAME, INVCOUNT, CRNCOUNT, (INVCOUNT - CRNCOUNT)NETTCOUNT, INVTOTAL, CRNTOTAL, (INVTOTAL - CRNTOTAL)NETTSALES FROM RESULT3
ORDER BY NETTCOUNT DESCENDING
Thank you.
Regards
wickyd