Count Where

Johnathon Warren shared this question 7 years ago
Completed

Hey,


Our client has a large number of sites. Each site can order items from any of 20 different suppliers.


Our client wishes to see how many sites are purchasing over $100 worth of items from each supplier. They also plan on running more advanced filters and queries on this data, so this isn't something I can do for them with a freehand SQL query each time they need it.


Traditionally, this is how the SQL query would look:


  1. SELECT a.Supplier, a.Date, COUNT(b.Site)
  2. FROM Sales a
  3. LEFT JOIN (
  4. SELECT Site, Supplier, Date, SUM(Amount)
  5. FROM Sales
  6. GROUP BY Site, Supplier, Date
  7. -- WHERE -- Other filters would go here, such as by product
  8. HAVING SUM(Amount) > 100 -- The "100" could potentially be changed using a parameter
  9. ) b
  10. ON a.Supplier = b.Supplier AND a.Date = b.Date
  11. GROUP BY a.Supplier, a.Date


I can only figure out how to do this with sub-totals. But this makes VERY ugly looking reports that are difficult to manage and can't build charts on. Is there another way to do this?


I feel like it might work best with a Sub-Query, but I can't figure out how to filter by the SUM on a per-site basis while using count or count-distinct.


I tried to use a calculated field to count the number of sites in the sub-query, which would have worked if Yellowfin allowed me to hide the sub-query in its entirety, and thus eliminate all the extra rows it generates.


Thanks

Replies (3)

photo
1

Hi Johnathon,

Thank you for getting in touch. I think that the best way to do this will be through the use of a custom SQL filter on the concatenation of supplier and site.

Essentially the query will return a list of concatenated(site,supplier) where the sum of their purchases are > 100.

In the example below I have the foundational data-set; a list of booking methods (representing supplier), countries (representing sites), invoiced amounts (filtered by >100k), and the view level calculated field (concat booking/country initials)

b2843aadf6838f0e64cfa472c07a53a2

Once I have this report, I grab the Freehand SQL statement, and remove all but the concatenated field from the select clause:


  1. SELECT DISTINCT "ATHLETEFACT"."BOOKINGMETHOD" + "PERSON"."ISOCODE", #There were 3 other fields in this select clause, I have removed them.
  2. FROM "ATHLETEFACT"
  3. INNER JOIN "PERSON"ON ( "ATHLETEFACT"."PERSONID" = "PERSON"."PERSONID")
  4. GROUP BY "PERSON"."ISOCODE",
  5. "ATHLETEFACT"."BOOKINGMETHOD",
  6. "ATHLETEFACT"."BOOKINGMETHOD" + "PERSON"."ISOCODE"
  7. HAVING SUM("ATHLETEFACT"."INVOICEDAMOUNT") > 100000

Now that I have the statement I want, I can create a new report using just the supplier (booking), and filtering the results on my concatenated field.

a61390b3a93b62b43f52eb5fd1cbc7fc


Please let me know if this works for you.

Regards,

Nathan

photo
1

Hi Johnathan,


Have you had any luck in resolving this?


Regards,

Nathan

photo
1

Hi Jonathan, we're under the impression this has resolved the issue, so going to flag as answered.

Please let us know in the coming days if this is not the case.


Regards,

David

Leave a Comment
 
Attach a file