SASUSMC
SASUSMC

Reputation: 681

Aliasing using the same Alias twice

I have run across a query where the developer is using the same alias twice in the query.

This was done several times in the Query- so I don't think it was a mistake. Is there a reason to do this that I am not aware of or, is it just sloppy work?

(Select
  AppointmentID
  ,Division
  ,Hospital
  ,ApptDate
  ,KeptDate
From
     (Select
          s1.AppointmentID
          ,s1.Division
          ,s1.Hospital
          ,s1.Apptdate
          ,s1.KeptDate
          From S1
          Group By
          s1.AppontmentID
          ,s1.Division
          ,s1.Hospital
          ,s1.Apptdate
          ,s1.KeptDate) as S2
Group By
  AppointmentID
  ,Division
  ,Hospital
  ,ApptDate
  ,KeptDate) As S2

This is the code simplified down- there are many more fields but no aggregate in them as well as the fact that both Group Bys follow one after the other. There is nothing in between them.

Thanks,

Upvotes: 2

Views: 513

Answers (2)

Yosi Dahari
Yosi Dahari

Reputation: 6999

  1. Don't use aggregation unless you have an aggregation function (MIN/MAX/AVG etc.)
  2. Try not to use the same alias twice (it's confusing)
  3. When the smallest granularity (AppointmentID) is in the aggregation, it makes it useless (is it unique?) if it is unique, this will return the same result as a simple SELECT statment

Upvotes: 1

Lajos Veres
Lajos Veres

Reputation: 13725

If you strip down the external query you will get the same result. So this is sloppy work in this form. But is you removed something important to simplify the example it can be a cause. (for example joined extra tables.)

Upvotes: 0

Related Questions