Skip to content
Advertisement

mySQL: compared values of the same column based on category of another column (no JOIN)

I want to take the difference of the values of 1 column based on version column (only 2 versions) of another column. the id-value pair may exist in one version but not the other and vice versa (in this case, the difference defaults to 0). It’s essentially converting tables from long to wide format with the addition of calculating the difference of the value column.

I know I can use JOIN to achieve this result. But i am wondering if there are methods without JOIN

version_1 and version_2 is defined by users. and in current example, 2021 is version_1 and 2022 is version_2.

table:

desired result:

Advertisement

Answer

Hmmm . . . you seem to want conditional aggregation:

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