Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Examine the following SQL query, and explain clearly and succinctly what it means. Will the query work? Explain why or why not
WITH processed_users
AS (SELECT LEFT(u.phone_country, 2) AS short_phone_country,
u.id FROM users u)
SELECT t.user_id, t.merchant_country,
Sum(t.amount / fx.rate / Power(10, cd.exponent)) AS amount FROM transactions t
JOIN fx_rates fx
ON ( fx.ccy = t.currency
AND fx.base_ccy = 'EUR' ) JOIN currency_details cd
ON cd.currency = t.currency JOIN processed_users pu
ON pu.id = t.user_id WHERE t.source = 'GAIA'
AND pu.short_phone_country = t.merchant_country GROUP BY t.user_id,
t.merchant_country ORDER BY amount DESC;

What I have tried:

Tried to run it and it returns an empty table with free columns user_id, merchant_account, amount. Why this query returns an empty table?
Posted
Updated 12-Feb-21 7:38am
v4

1 solution

First of all, we don't do homework. We can help if you're stuck in a specific question but in such case you would need to show what you've done so far and what is the detail you're struggling with.

Having that said, without having your data it's quite hard to say why the result is empty. What I would suggest is that run the different parts of the query independently and examine the data you receive, if any.

For example what does the following return
SQL
SELECT LEFT(u.phone_country, 2) AS short_phone_country,
       u.id 
FROM users u

what about
SQL
SELECT t.user_id,
       t.merchant_country,
       Sum(t.amount / fx.rate / Power(10, cd.exponent)) AS amount 
FROM transactions t
JOIN fx_rates fx         ON ( fx.ccy = t.currency AND fx.base_ccy = 'EUR' ) 
JOIN currency_details cd ON cd.currency = t.currency 
JOIN processed_users pu  ON pu.id = t.user_id 
WHERE t.source = 'GAIA'

Note that the join to processed_users is removed from the query to be able to run it separately. If the latter returns nothing you can continue breaking it into pieces and removing conditions to understand the reason. If both return data then have a look at the joining columns in the raw data you got. Do the result sets contain common key values and so forth.
 
Share this answer
 
Comments
Alex Greenvith 3-Feb-21 23:16pm    
Thanks a lot! Sorry about that, I am a little inexperienced in this! I ran 2 queries separately and they both work and return data. Does it mean that the problem is with (JOIN processed_users pu ON pu.id = t.user_id) line and that these results have no common values? Or is there a problem with WITH (Common Table Expressions)?
Wendelius 3-Feb-21 23:21pm    
If both return data then have a look at the joining values from both result sets. An inner join would require matching key, please see Join (SQL) - Wikipedia, Inner join[^]
OriginalGriff 12-Feb-21 13:56pm    
He edited it and replaced his homework / interview question with random keystrokes.
I rolled it back ...
Wendelius 12-Feb-21 14:54pm    
Good to know, thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900