Return the value based on the most recent matching the criaterion
Hi all, please help to resolve the following issue.
I Have a very huge dataset showing the following and I need to fill the column "Currency 2/ USD" with values:
# Time; Cur 1; Cur 2; Pair; Price; Currency 2/USD
1 2:00 pm EUR ; USD; EUR/USD 1,165 1
2 5:00 pm EUR ; USD; EUR/USD 1,167 1
3 5:01 pm UAH ; EUR; UAH/EUR 0,032 1,167
4 5:02 pm UAH ; EUR; UAH/EUR 0,031 1,167
5 5:03 pm EUR ; USD; EUR/USD 1,169 1
6 5:06 pm UAH ; EUR; UAH/EUR 0,031 1,169
So the logic of filling the column "Currency 2 / USD" is the following: If Currency 2 is USD then return 1, if it is not USD (let's say EUR like in line 3), look for the most recent this currency / USD price and state it (1,167 for lines 3 and 4 as the most recent EUR/USD is line 2 with price 1,167).
How should I do this? P.S. I have 67 unique pairs
Best Answer
-
That was a fun one. Here's the query I used:
SELECT a.* ,
CASE WHEN a.`Currency 2` = 'USD' THEN 1
ELSE (SELECT b.`Price`
FROM `currency_data` AS b
WHERE a.`Currency 2` = LEFT(b.`Pair`,3)
AND RIGHT(b.`Pair`,3) = 'USD'
AND b.`Time` <= a.`Time`
ORDER BY b.`Time` DESC
LIMIT 1)
END AS 'Currency 2/USD'
FROM `currency_data` AS aHere's the screenshot with query and results
Hope that helps get you what you need.
Sincerely,
Valiant_Ronin
**Please mark "Accept as Solution" if this post solves your problem
**Say "Thanks" by clicking the "heart" in the post that helped you.0
Answers
-
That was a fun one. Here's the query I used:
SELECT a.* ,
CASE WHEN a.`Currency 2` = 'USD' THEN 1
ELSE (SELECT b.`Price`
FROM `currency_data` AS b
WHERE a.`Currency 2` = LEFT(b.`Pair`,3)
AND RIGHT(b.`Pair`,3) = 'USD'
AND b.`Time` <= a.`Time`
ORDER BY b.`Time` DESC
LIMIT 1)
END AS 'Currency 2/USD'
FROM `currency_data` AS aHere's the screenshot with query and results
Hope that helps get you what you need.
Sincerely,
Valiant_Ronin
**Please mark "Accept as Solution" if this post solves your problem
**Say "Thanks" by clicking the "heart" in the post that helped you.0 -
Thank you! I'll try. Should I use this in a beastmode?
0 -
No, you'll need to do this with a SQL Data Transform.
You're having to compare the dataset against itself so you can't use only card level beastmodes.
Once you open the SQL transform, select your dataset and you should be able to then tweak my code to match column/dataset names.
Let me know if you need further assistance,
Valiant_Ronin
0 -
Thanks a lot! It works
1 -
Hi again,
I faced the problem that DOMO can't handle this request as I have like 60 million rows of initial data and after a day of processing DOMO just refuses to do that
any thoughts how to optimize? Got the response from DOMO that is huge indeed and SQL is not good for such an exercise
0
Categories
- All Categories
- 2K Product Ideas
- 2K Ideas Exchange
- 1.6K Connect
- 1.3K Connectors
- 311 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 3.8K Transform
- 656 Datasets
- 115 SQL DataFlows
- 2.2K Magic ETL
- 813 Beast Mode
- 3.3K Visualize
- 2.5K Charting
- 81 App Studio
- 45 Variables
- 771 Automate
- 190 Apps
- 481 APIs & Domo Developer
- 77 Workflows
- 23 Code Engine
- 36 AI and Machine Learning
- 19 AI Chat
- AI Playground
- AI Projects and Models
- 17 Jupyter Workspaces
- 410 Distribute
- 120 Domo Everywhere
- 280 Scheduled Reports
- 10 Software Integrations
- 142 Manage
- 138 Governance & Security
- 8 Domo Community Gallery
- 48 Product Releases
- 12 Domo University
- 5.4K Community Forums
- 41 Getting Started
- 31 Community Member Introductions
- 114 Community Announcements
- 4.8K Archive