I have a problem with counting the ids that are inside the grouped query. The problem is that this query is grouped according to another variable. For example I need to count how many products are ordered inside the invoice, while this invoice is inside the company that made multiple number of the invoices. I hope it was clear; anyway I hope everything will be more clear with this SQL:
SELECT C.FULLNAME,C.COMPANY_ID,SUM(I.GROSSTOTAL) AS TOTAL,
SUM(I.OTHER_MONEY_VALUE/1.18) AS OTHER_TOTAL,
COUNT(IR.PRODUCT_ID) AS NO_PRODUCTS
FROM COMPANY C
JOIN #DSN2_ALIAS#.INVOICE I ON C.COMPANY_ID=I.COMPANY_ID
JOIN #DSN2_ALIAS#.INVOICE_ROW IR ON I.INVOICE_ID=IR.INVOICE_ID
JOIN #DSN3_ALIAS#.PRODUCT P ON P.PRODUCT_ID=IR.PRODUCT_ID
JOIN #DSN3_ALIAS#.PRODUCT_CAT PC ON P.PRODUCT_CATID=PC.PRODUCT_CATID
WHERE P.PRODUCT_ID=<cfqueryparam value="#attributes.product_id#" cfsqltype="cf_sql_integer">
GROUP BY C.COMPANY_ID,C.FULLNAME
ORDER BY TOTAL DESC
After I define the product id, the query lists in the output the list of companies, with its total sum of sales, and the NO_PRODUCTS has the same value as the number of invoices, because query is grouped by company_id and fullname of it, but the inside each invoice there are multiple number of the products, thus the variable no_products is defined wrongly 🙁
How can I get the counts correct?
I hope i understood what you are trying to do, sorry if i didn’t