0

Having trouble with cast syntax. I get an error message with this and I am having trouble figuring out what the problem is. I'm using pandasql.

SELECT CAST((((male_year_df.total_male_suicides) / female_year_df.total_female_suicides)) AS DECIMAL(1,5)) AS male_female_ratio), male_year_df.year

EDIT: this is the error message: OperationalError: near ")": syntax error

1
  • "I get an error message" why are you making us guess what the error message is? Include it in your question. Also, what is a DECIMAL(1,5), 1 digit long, 5 of which on the right side of the decimal point? Makes little sense Commented Sep 11, 2022 at 18:02

1 Answer 1

1

your parethesis are too much of, also DECIMAL(1,5) is not allowed as the first number has to be equal or bigger than the second number

SELECT CAST(
            
              (
                (male_year_df.total_male_suicides) 
                / (female_year_df.total_female_suicides)
            ) AS DECIMAL(5,5)) AS male_female_ratio
              , male_year_df.year

You can first define the firsst number with the precision you need and then divide the second number, so that the engine will not use integer division

 SELECT CAST(male_year_df.total_male_suicides AS DECIMAL(5,5)) 
     / (female_year_df.total_female_suicides) AS male_female_ratio
              , male_year_df.year
Sign up to request clarification or add additional context in comments.

4 Comments

it works, but it's giving me whole numbers and rounding them, how do I get it to have 5 places after the decimal point?
so I use AS FLOAT instead of decimal and it's giving me values like 2.0 and 3.0, it's not giving me the precise numbers after the decimal.
then use DECIMAL(5,5) on the first numnber so that Integer dision will not be used
ok that worked. I used float instead of decimal, but yeah I used it on the first number. thanks!

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.