Skip to content
Advertisement

Is there a more efficient way to append multiple columns from the same table?

I’d like to return multiple values from the same record, with different columns of the same type appended under one another. I could do multiple joins, but that seems very inefficient because of the multiple table scans.

Returns:

VisID Vis Home HomeID
arenn001 Nolan Arenado Colin Moran morac001
badeh001 Harrison Bader Anthony Alford alfoa002
carld002 Dylan Carlson Yoshi Tsutsugo tsuty001
edmat001 Tommy Edman Kevin Newman newmk001
goldp001 Paul Goldschmidt Ke’Bryan Hayes hayek001
kim-k001 Kwang Kim Wil Crowe croww001
moliy001 Yadier Molina Jacob Stallings stalj001
oneit001 Tyler O’Neill Bryan Reynolds reynb001
sosae001 Edmundo Sosa Cole Tucker tuckc001

This is exactly what I’m looking for, but it’s painfully slow. Is there a better way?

Advertisement

Answer

You need to unpivot each row. This means that you only scan the table once, then break it out into separate rows. You could use UNPIVOT, but CROSS APPLY (VALUES is far more flexible.

It goes without saying that your table is seriously denormalized and should be redesigned pronto.

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