This week's giveaway is in the Spring forum.
We're giving away four copies of liveProject: Protecting User Data with Spring Security and OAuth2 and have Laurentiu Spilca on-line!
See this thread for details.
Win a copy of liveProject: Protecting User Data with Spring Security and OAuth2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Aggregated results without duplicates on multiple columns each filtered differently

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!

I have a BigQuery table that looks like this and that can't be modified:

CountryCustomerNumber of connectionsNumber of purchasesCountry Metric 1Country Metric 2
BrazilA10131000
BrazilB90531000
BrazilC80231000
NamibiaB20152000
NamibiaC150252000


About this table, please note that:
- Each combination of Country-Customer is unique.
- The country metrics, as their names suggest, only depend on the country.
- For some countries, some metrics are not available (NULL in the table).
- For some combinations Country-Customer, the number of connections/purchases are not available

I would like to obtain, in the same query, the following information:
- The mean of Country Metric 1 only taking into account combinations of Country-Customer having at least a number of purchases higher or equal than 2. In the example table, there are 3 combinations: Brazil-B, Brazil-C and Namibia-C. The mean should take into account Brazil only once so the result is (3 + 5) / 2 = 4.
- The mean of Country Metric 2 only taking into account combinations of Country-Customer having at least a number of connections higher than 100. There is only one combination which meets this criterion in the example table: Namibia-C. Thus, the expected result is 2000.

Those are just examples but there can be more metrics and other aggregations (sum, min, max, count...) but they should be very similar.

Here is what I've tried:

Issue: if the same country appears in multiple combinations, the same metric is taken into account multiple times.


Issue: for each country, only one combination is taken into account giving lower and random results...

Is there a way to achieve this?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic