Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Refactoring SQL statment

Refactoring SQL statment

Refactoring SQL statment

Can any one help me refactor this SQL statment so I can read it?


    SELECT Tblevents.eventid, Tblevents.eventname, Tblevents.details, Tblevents.date, qry_home_cs_all.CountOfcontactid AS Totalatt, qry_CS_status_1.CountOfContactid AS Uni, qry_CS_status_2.CountOfContactid AS Biz, qry_CS_status_3.CountOfContactid AS Other, Tblevents.event_type FROM (((qry_CS_status_1 RIGHT JOIN Tblevents ON qry_CS_status_1.eventid=Tblevents.eventid) LEFT JOIN qry_CS_status_3 ON Tblevents.eventid=qry_CS_status_3.eventid) LEFT JOIN qry_CS_status_2 ON Tblevents.eventid=qry_CS_status_2.eventid) LEFT JOIN qry_home_cs_all ON Tblevents.eventid=qry_home_cs_all.eventid WHERE (((Tblevents.event_type)=2)) ORDER BY Tblevents.eventid DESC;  

RE: Refactoring SQL statment


To improve readability I suggest to :
  • remove pointless parenthesis ( () )
  • use aliases
  • do not mix left and right joins
  • join with using clause when possible
So this is the look I prefer :


h.CountOfcontactid as Totalatt,
s1.CountOfContactid as Uni,s2.CountOfContactid as Biz,s3.CountOfContactid as Other

from Tblevents e
left join qry_CS_status_1 s1 using (eventid)
left join qry_CS_status_3 s3 using (eventid)
left join qry_CS_status_2 s2 using (eventid)
left join qry_home_cs_all h using (eventid)

where e.event_type=2

order by e.eventid desc
Note : is possible that I accidentally messed up the statement during editing.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close