I had a question where I needed to work out what the percentage of my 'grouped_by' summary values (Sum of price_paid) were of the total of a whole dataset.
I therefore converted my question to an SQL query to accomplish this. However the value I was summing needs to be a currency value (this is set up in the database 'Data Model'). This was fine in the original question but in SQL on the map visualisation, there are no options to prefix the values and if I use 'to_char' to add "£" as a prefix in the query, the whole column isn't recognised/available to select as the metric field.
Is there another way I should be approaching this?
SQL Query
SELECT "Locations"."country_code" AS "Locations__country_code", date_trunc('year', "public"."posts"."post_timestamp") AS "post_timestamp", to_char(sum("public"."posts"."price_paid")::float / '100', 'L999G999G999D99') AS "Sum of Price Paid", (sum("public"."posts"."price_paid") / (SELECT sum("public"."posts"."price_paid")::float FROM "public"."posts") * '100') AS "Ratio %"
FROM "public"."posts"
LEFT JOIN "public"."locations" "Locations" ON "public"."posts"."location_id" = "Locations"."id"
WHERE ("public"."posts"."post_timestamp" >= date_trunc('year', (now() + (INTERVAL '-1 year')))
AND "public"."posts"."post_timestamp" < date_trunc('year', now()))
GROUP BY "Locations"."country_code", date_trunc('year', "public"."posts"."post_timestamp")
ORDER BY "Locations"."country_code" ASC, date_trunc('year', "public"."posts"."post_timestamp") ASC
Diagnostic info
{
"browser-info": {
"language": "en-GB",
"platform": "MacIntel",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
"vendor": "Google Inc."
},
"system-info": {
"file.encoding": "UTF-8",
"java.runtime.name": "OpenJDK Runtime Environment",
"java.runtime.version": "11.0.17+8-LTS",
"java.vendor": "Azul Systems, Inc.",
"java.vendor.url": "http://www.azul.com/",
"java.version": "11.0.17",
"java.vm.name": "OpenJDK 64-Bit Server VM",
"java.vm.version": "11.0.17+8-LTS",
"os.name": "Linux",
"os.version": "4.4.0-1104-aws",
"user.language": "en",
"user.timezone": "Etc/UTC"
},
"metabase-info": {
"databases": [
"postgres",
"h2"
],
"hosting-env": "heroku",
"application-database": "postgres",
"application-database-details": {
"database": {
"name": "PostgreSQL",
"version": "13.9 (Ubuntu 13.9-1.pgdg20.04+1)"
},
"jdbc-driver": {
"name": "PostgreSQL JDBC Driver",
"version": "42.5.0"
}
},
"run-mode": "prod",
"version": {
"date": "2022-12-07",
"tag": "v0.45.1",
"branch": "release-x.45.x",
"hash": "019d31c"
},
"settings": {
"report-timezone": null
}
}
}