General information
Category:
Coding, syntax & commands
Title:
SQL -aggregation & group by
Hi - this query sums investments made through a given salesperson
for each investor which is not invested in a given receiver(investment). It outputs the top N investors and the amount invested. Is it possible to rewrite it for more than 1 salesperson at a time?
LOCAL lcSalesmanid,lcReceiver,ldDate,lnTop,lcTopclause,lcReceiverName
*Get values we need for SELECT statement
lcSalesmanid=THISFORM.lstSalesNames.VALUE
lcReceiver=THISFORM.lstreceivers.VALUE
lcReceiverName=THISFORM.lstreceivers.DISPLAYVALUE
ldDate=THISFORM.sttxtdate1.VALUE
lnTop=THISFORM.sttxtnumeric1.VALUE
lcReceiverName=""
*Build top clause for SELECT statement
IF lnTop>0
lcTopclause="TOP "+ALLT(STR(lnTop))
ELSE
lcTopclause=SPACE(1)
ENDIF
*Build receiver clause in case we want to include ALL receivers
IF EMPTY(lcReceiver) &&no receiver to exclude
lcReceiverClause=SPACE(1)
ELSE
lcReceiverClause="AND T.receiverid <> lcReceiver"
ENDIF
*This query selects the top N investors for a given salesperson
* who are NOT invested in a given receiver
SELECT &lcTopclause T.broker_i1 AS InvBroker1,T.iaccountid,;
T.TIMESTAMP,T.mdate_i AS Matures,ldDate AS Mdate,;
SUM(T.prin_i) AS TotalPrincipal,i.i_ussname,;
i.i_phone AS Telephone,i.contact AS contact,;
i.i_fax AS Fax,i.taxid AS taxid,s.s_fname,s.s_lname,;
lcReceiverName AS lcReceiverName;
FROM trade T,investor i,salesmen s;
INTO CURSOR InvestNotIn;
GROUP BY T.iaccountid,s.s_lname;
ORDER BY TotalPrincipal DESC;
WHERE T.iaccountid=i.iaccountid;
AND T.broker_i1=s.salesmanid;
AND T.broker_i1=lcSalesmanid;
AND T.mdate_i >= ldDate;
&lcReceiverClause
Thanks- Any suggestions will be much appreciated.
Carlie
Next
Reply
View the map of this thread
View the map of this thread starting from this message only
View all messages of this thread
View all messages of this thread starting from this message only