Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with SELECT grouping

Status
Not open for further replies.

Judi201

Technical User
Jul 2, 2005
315
US
Hi,

I am trying to create a cursor to show invoices in a grid.
I have a textbox for Customer number and a checkbox for ALL or UNPAID invoices. I tried to put it in an IIF() but couldn't get it to work so I split it up but still don't get what I expected. I guess with all the help I have had, I still don't get the grouping.

Data looks like this:

custno Date invoice# debit credit
200 ----- 10001 500.00
200 ----- 10001 500.00
201 ----- 10002 800.00
201 ----- 10002 200.00
201 ----- 10002 600.00

I thought I could group by the invoice# and compare
debit - credit to see if invoice was paid but I can't get it to work.

Am I wrong about what I should expect here?

Code:
LOCAL lnchk, lnCustno
lnCustno = VAL(This.Parent.txtCustno.Value)
lnchk = This.Value
This.Parent.grdTicket.RecordSource =""
IF lnchk
	SELECT custno, invcnumb, invcdate, invcdesc, invcdebt, invccred, invcwght, invctype ;
		FROM ticket ;
		ORDER BY Ticket.custno, ticket.invcnumb ;
		GROUP BY Ticket.custno, ticket.invcnumb	;	
		WHERE ticket.custno = lncustno AND ;
			(ticket.invcdebt - ticket.invccred) > 0.00) ;  
		INTO CURSOR csrTicket
ELSE
	SELECT custno, invcnumb, invcdate, invcdesc, invcdebt, invccred, invcwght, invctype ;
		FROM ticket ;
		ORDER BY Ticket.custno, ticket.invcnumb ;
		WHERE ticket.custno = lncustno ;
		INTO CURSOR csrTicket
ENDIF
This.Parent.grdTicket.RecordSource = "csrTicket"
Thisform.Refresh()

Would someone give me a push in the right direction here?

Appreciate your time and any suggestions.

Judi
 
When you're grouping in a query, you want to put in the field only two kinds of fields:

1) the fields you're going to group on;
2) expressions involving the aggregate functions.

While VFP 7 and earlier let you put other fields, you get random results for them.

In your case, you want to do something like this:

Code:
    SELECT custno, invcnumb, SUM(invcdebt-invccred) AS nBalance;
        FROM ticket ;
        WHERE ticket.custno = lncustno ;
        ORDER BY Ticket.custno, ticket.invcnumb ;
        GROUP BY Ticket.custno, ticket.invcnumb    ;    
        INTO CURSOR csrTicket

To list only unpaid invoices, add this line after the GROUP BY:

Code:
HAVING nBalance > 0

Tamar
 
Tamar,

I am sitting here reading your "Taming Visual FOXPRO SQL" and trying to see how I can display the other info while doing as you suggested above.

By the time I released that post I remembered about only grouping on aggregate functions and came back change my post.

The miracle of this site, you were already here with a reply.

I understand what you are showing me but how can I show the other fields in the grid??

Thanks so much for you help.

Judi
 
Judi,

You could add a sum(invcdebt) as debit and sum(invccred) as credit to the field statement and add the other fields to both the field list and the group by statement. Individual debits and credits would be a little beyond the scope of the sql query though.

Regards,
Jim
 
Jim,

Thanks for the response. I don't need the individual debits as much as I need the date, desc and weight to show in the grid. These will be the same on all tickets.

I'm wondering what putting these fields in the SELECT list and the GROUP BY list might do to the result. Would that be safe for what I need?

Any suggestions appreciated.

Judi
 

Try doing it in two steps: First, like Tamar suggested:
Code:
SELECT custno, invcnumb, SUM(invcdebt-invccred) AS nBalance ;
    FROM ticket ;
    WHERE ticket.custno = lncustno ;
    ORDER BY Ticket.custno, ticket.invcnumb ;
    GROUP BY Ticket.custno, ticket.invcnumb ;
    HAVING nBalance > 0 ;
    INTO CURSOR csrFirst

* second, JOIN the other fields

SELECT a.custno, a.invcnumb, a.nBalance, ;
       b.invcdate, b.invcdesc, b.invcwght ;
    FROM csrFirst a ;
    JOIN ticket b ;
    ON a.custno = b.custno AND a.invcnumb = b.invcnumb ;
    INTO CURSOR csrTicket
 
TheRambler,

Thanks so much. That should do it fine. I'll try it as soon as I can and post back.

Judi
 
TheRambler and all who replied,

I have decided to use another approach on this problem (however TheRambler's code seemed to work fine) but I took that example and solved another much bigger problem I had in another place.

I can look at all these examples but until something solves a problem for me it just doesn't seem to stick.

Thanks to all for your time and help.

Judi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top