Skip to content
Advertisement

Datatype conversion and calculated column

I have a table with the following structure:

table

Unfortunately, the sale_date is stored as INT and I had to find a way to convert it in quarter (202001 will be Q1). The conversion worked well, but I need to include also some calculated columns based on this conversion. Basically, I need to calculate for Q1 the total price for each product, and the percentage from total for “product x” and “product y” recorded by each seller, in the current year. I know that I can obtain these easily using group by, but the way I converted the date (sale_date) from INT to quarter it’s affecting the results.

Advertisement

Answer

Note: You should always use the right datatype for a column. It will avoid lots of issues. Always store Date values in date datatype.

I would suggest you first convert the INT to date datatype and then use DATE functions to calculate quarter name. It will be accurate.

Below I am adding 01 as the date to the yyyymm and then making it as yyyymmdd, to make it as ISO 8601 dateformat (which is agnostic of dateformats) and then calculating the quarter value.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement