I have this query (below) and it returns one record per customer.SELECT CUSTOMER.CSCODE, CUSTOMER.CSNAME, SUM(INVHIST_HDR.IHTOT_AR_AMT) AS TotalSales, CUSTOMER.CSADDR1, CUSTOMER.CSADDR2, CUSTOMER.CSADDR3, CUSTOMER.CSCITY, CUSTOMER.CSST, CUSTOMER.CSZIP, CCONTACT.CCTITLE, CCONTACT.CCFNAME, CCONTACT.CCLNAME, CCONTACT.CCJOBDESC, CCONTACT.CCNAME, CCONTACT.CCADDR1, CCONTACT.CCADDR2, CCONTACT.CCADDR3, CCONTACT.CCCITY, CCONTACT.CCST, CCONTACT.CCZIP, CCONTACT.CCCOUNTRY, CCONTACT.CCTELENO, CCONTACT.CCFAXNO, CCONTACT.CCEMAILID, SALESAGENT.SANAMEFROM CUSTOMER AS CUSTOMER INNER JOIN INVHIST_HDR AS INVHIST_HDR ON CUSTOMER.CSCODE = INVHIST_HDR.CSCODE INNER JOIN CCONTACT ON CUSTOMER.CSCODE = CCONTACT.CSCODE INNER JOIN SALESAGENT ON CUSTOMER.SACODE = SALESAGENT.SACODEWHERE (INVHIST_HDR.IHINVOICE_NO > 0) AND (INVHIST_HDR.PLT_NO = 1) AND (INVHIST_HDR.IHINV_DATE >= '09/01/2013') AND (INVHIST_HDR.IHINV_DATE <= '09/01/2014')GROUP BY CUSTOMER.CSCODE, CUSTOMER.CSNAME, CUSTOMER.CSADDR1, CUSTOMER.CSADDR2, CUSTOMER.CSADDR3, CUSTOMER.CSCITY, CUSTOMER.CSST, CUSTOMER.CSZIP, CCONTACT.CCTITLE, CCONTACT.CCFNAME, CCONTACT.CCLNAME, CCONTACT.CCJOBDESC, CCONTACT.CCNAME, CCONTACT.CCADDR1, CCONTACT.CCADDR2, CCONTACT.CCADDR3, CCONTACT.CCCITY, CCONTACT.CCST, CCONTACT.CCZIP, CCONTACT.CCCOUNTRY, CCONTACT.CCTELENO, CCONTACT.CCFAXNO, CCONTACT.CCEMAILID, SALESAGENT.SANAME
I want to join another table to the query which would calculate Total Orders for each customer. When I run the below query it is returning one record for each order for the customer. How can I have it just return the Total Orders for each one customer record coming from the other tables?SELECT CUSTOMER.CSCODE, CUSTOMER.CSNAME, SUM(INVHIST_HDR.IHTOT_AR_AMT) AS TotalSales, CUSTOMER.CSADDR1, CUSTOMER.CSADDR2, CUSTOMER.CSADDR3, CUSTOMER.CSCITY, CUSTOMER.CSST, CUSTOMER.CSZIP, CCONTACT.CCTITLE, CCONTACT.CCFNAME, CCONTACT.CCLNAME, CCONTACT.CCJOBDESC, CCONTACT.CCNAME, CCONTACT.CCADDR1, CCONTACT.CCADDR2, CCONTACT.CCADDR3, CCONTACT.CCCITY, CCONTACT.CCST, CCONTACT.CCZIP, CCONTACT.CCCOUNTRY, CCONTACT.CCTELENO, CCONTACT.CCFAXNO, CCONTACT.CCEMAILID, SALESAGENT.SANAME, COUNT(ORDERS.ORDER_NO) AS TotalOrdersFROM CUSTOMER AS CUSTOMER INNER JOIN INVHIST_HDR AS INVHIST_HDR ON CUSTOMER.CSCODE = INVHIST_HDR.CSCODE INNER JOIN CCONTACT ON CUSTOMER.CSCODE = CCONTACT.CSCODE INNER JOIN SALESAGENT ON CUSTOMER.SACODE = SALESAGENT.SACODE INNER JOIN ORDERS ON CUSTOMER.CSCODE = ORDERS.CSCODEWHERE (INVHIST_HDR.IHINVOICE_NO > 0) AND (INVHIST_HDR.PLT_NO = 1) AND (INVHIST_HDR.IHINV_DATE >= '09/01/2013') AND (INVHIST_HDR.IHINV_DATE <= '09/01/2014')GROUP BY CUSTOMER.CSCODE, CUSTOMER.CSNAME, CUSTOMER.CSADDR1, CUSTOMER.CSADDR2, CUSTOMER.CSADDR3, CUSTOMER.CSCITY, CUSTOMER.CSST, CUSTOMER.CSZIP, CCONTACT.CCTITLE, CCONTACT.CCFNAME, CCONTACT.CCLNAME, CCONTACT.CCJOBDESC, CCONTACT.CCNAME, CCONTACT.CCADDR1, CCONTACT.CCADDR2, CCONTACT.CCADDR3, CCONTACT.CCCITY, CCONTACT.CCST, CCONTACT.CCZIP, CCONTACT.CCCOUNTRY, CCONTACT.CCTELENO, CCONTACT.CCFAXNO, CCONTACT.CCEMAILID, SALESAGENT.SANAME, ORDERS.ORDER_NO
Hope that makes sense.Thanks,Stacy Walker