16 May 2023 · filed under Coding challenge

DataLemur: Pharmacy Analytics

sqldatalemurctesaggregation

Solution #1

Part 1

SELECT drug, total_sales - cogs AS total_profit
FROM pharmacy_sales
ORDER BY total_profit DESC
LIMIT 3

Part 2

WITH total_sales AS (
  SELECT manufacturer, drug, total_sales - cogs AS total_loss
  FROM pharmacy_sales
)
SELECT manufacturer, COUNT(drug) AS drug_count, ABS(SUM(total_loss)) AS total_loss
FROM total_sales
WHERE total_loss <= 0
GROUP BY manufacturer
ORDER BY total_loss DESC

Part 3

WITH total_sales AS (
  SELECT manufacturer, ROUND(SUM(total_sales) / 1000000, 0) AS sale
  FROM pharmacy_sales
  GROUP BY manufacturer
  ORDER BY sale DESC
)
SELECT manufacturer, FORMAT('$%s million', sale) AS sale
FROM total_sales