vendredi 22 avril 2016

Syntax: Using case or if-then-else for t-SQL alias setting?

I'm using t-SQL for reporting and had a question regarding syntax. I just started learning some of the more intermediate SQL stuff, like joins, aliases, case statements, order by, etc.

I have a table that I need to pull person data from. When pulling data I need to pull every person associated and sort through the list of people based on attributes. Then I want to store the person data in two variables (aliases). One person has attribute 'Applicant' and their information should be aliased and stored separately from everyone else's. Everyone else should be stored in a single list. Is there a good way to do this in SQL? Here's what I was thinking in pseudocode

select * from persontable
if persontable.TYPE = 'Applicant' then persontable.name as applName, persontable.otherAtt as applOtherAtt --etc
else persontable.name --add to a list and alias as otherNames

or something like this

case when persontable.TYPE = 'Applicant'then persontable.name as applName, persontable.otherAtt as applOtherAtt --etc
else persontable.name --add to a list and alias as otherNames

Aucun commentaire:

Enregistrer un commentaire