0

I have two tables like this:

| MOVIE_ID | BUDGET      |
|----------+-------------|
|  1904269 | $850,000    |
|  1809508 | NLG 800,000 |
|  1988471 | $40,000     |
|  2119404 | $3,266      |
|  2117105 | $125,000    |
|  2167227 | CAD 280,000 |

and

| MOVIE_ID | GROSS                  |
|----------+------------------------|
|  2463072 | ESP 20,040,964 (Spain) |
|  2044720 | ESP 7,494,043 (Spain)  |
|  2083304 | ESP 53,463,024 (Spain) |
|  2461323 | ESP 15,670,733 (Spain) |
|  2318432 | ESP 16,530,040 (Spain) |
|  1874413 | SEK 512,112 (Sweden)   |

I want to create a budget_table which contains pairs of movie_id's and movie budgets. The movie budgets are stored as strings in movie_info. We only need to consider budgets which are listed in dollars, i.e. "$ XXXXXX". I also want to create a gross_table which contains pairs of movie_id's and movie gross revenues. Some movies have multiple gross revenues (i.e. the revenue as of 2008, 2010, 2012, etc.), and we just want the largest one. Then, I need to find the most profitable one by gross - budget. The two tables above I got by using the following:

CREATE OR REPLACE VIEW budget_table AS 
SELECT movie_id, info AS budget 
FROM movie_info WHERE info_type_id = 105;

so the budget info has id = 105. And the gross info has id = 107 and the code is:

CREATE OR REPLACE VIEW gross_table AS 
SELECT movie_id, info AS gross 
FROM movie_info WHERE info_type_id = 107;

Below is my code to get the budgets only listed in dollars:

CREATE OR REPLACE VIEW budget_table AS
SELECT movie_id, TO_NUMBER(REPLACE(REGEXP_SUBSTR(info, '\$[0-9]+'), '\$', ''))
                       AS budget
                      FROM movie_info
                      WHERE info_type_id = 105
                      AND REGEXP_LIKE(info, '\$[0-9]+');

I have no idea why it didn't print out anything when I do SELECT * FROM budget_table LIMIT 10;. If I get rid of the last row REGEXP_LIKE, I get the movie_id but all nulls for the budget. Can someone point out the issue with my SQL query?

4
  • Don't cram multiple values into strings; use separate columns for currency_type, budget, gross and country. If your input data are these multiple-value strings, then parse it to split it up when inserting into tables. Commented Feb 3, 2024 at 2:48
  • I'm given the schema, not allowed to edit it so I can't use separate columns for currency_type, budget, gross or country. Commented Feb 3, 2024 at 3:08
  • Which dbms are you using? (Your query involves several product specific functions.) Commented Feb 3, 2024 at 18:39
  • @jarlh I'm using snowflake Commented Feb 4, 2024 at 4:18

1 Answer 1

0

I would look for few things below to debug the issue -

  1. make sure if there a value with dollar sign followed by one or more digits. by just applying "regexp_substr(info,'$[0-9]+')" as budget_Col1, REPLACE(REGEXP_SUBSTR(info, '$[0-9]+'), '$', '') as budget_Col2 To debug the issue with expression or if there no values in column with matching expression

  2. And try the filter as select info,"regexp_substr(info,'$[0-9]+')" from your_table where "regexp_substr(info,'$[0-9]+')" is not null

  3. And if there null in string column, try using "to_number(replace(replace(nvl(regexp_substr(info,'$[0-9]+'),'0'),'$',' '),',','')"

Hope this helps:)

Sign up to request clarification or add additional context in comments.

10 Comments

I tried all three. Here's the issue: A subset of the data is shown above so there are definitely values with matching expression. Also tried 2 and 3 but it didn't work. :( I see so issues in my code though. The budget is in string, like the example above "$850,000". I just want to convert it to a number which I believe, my code is perfectly fine.
"to_number(replace(replace(nvl(regexp_substr(info,'\$[0-9]+'),'0'),'$',' '),',','')"
Thanks for fixing your code but it still outputted nothing, not even NULLs.
Can you please share sample value from info column, or can i assume the value as $850,000 for example?
I did share above in my question. The column: | MOVIE_ID | BUDGET | |----------+-------------| | 1904269 | $850,000 | | 1809508 | NLG 800,000 | | 1988471 | $40,000 | | 2119404 | $3,266 | | 2117105 | $125,000 | | 2167227 | CAD 280,000 |
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.