Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Aliasing Fieldnames and adding a calculation Field

Aliasing Fieldnames and adding a calculation Field

Scheduled Pinned Locked Moved Visual Basic
database
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raabi Anony
    wrote on last edited by
    #1

    Hello everybody! I am facing two simple problems in the following code:

        Dim cmdText As String
        cmdText = "SELECT tblStaffInfo.StaffName, tblCampuses.CampusName, 
                   LessonPlanning, LessonPreprn, Regularity,
    	           ((LessonPlanning + LessonPreprn + Regularity)/3 ) AS AvgScore
                    FROM tblStaffInfo
                    INNER JOIN tblCampuses
                    ON tblStaffInfo.CampusID = tblCampuses.CampusID
                    INNER JOIN tblStaffEvaluation
                    ON tblStaffEvaluation.StaffID = tblStaffInfo.StaffID
                    ORDER BY tblStaffInfo.StaffName"
    

    (1)- The above Sql shows hatred for the division (/) sign (2)- How can use short alias for the long tables' name; like tblStaffInfo and tblCampuses etc. Please suggest!

    C D 2 Replies Last reply
    0
    • R Raabi Anony

      Hello everybody! I am facing two simple problems in the following code:

          Dim cmdText As String
          cmdText = "SELECT tblStaffInfo.StaffName, tblCampuses.CampusName, 
                     LessonPlanning, LessonPreprn, Regularity,
      	           ((LessonPlanning + LessonPreprn + Regularity)/3 ) AS AvgScore
                      FROM tblStaffInfo
                      INNER JOIN tblCampuses
                      ON tblStaffInfo.CampusID = tblCampuses.CampusID
                      INNER JOIN tblStaffEvaluation
                      ON tblStaffEvaluation.StaffID = tblStaffInfo.StaffID
                      ORDER BY tblStaffInfo.StaffName"
      

      (1)- The above Sql shows hatred for the division (/) sign (2)- How can use short alias for the long tables' name; like tblStaffInfo and tblCampuses etc. Please suggest!

      C Offline
      C Offline
      CHill60
      wrote on last edited by
      #2

      To answer your second question first ... You can give each table a short alias by just including that alias after the table name in the query, or by using AS aliasname. For example you can use either

      FROM tblStaffInfo AS A
      INNER JOIN tblCampuses AS B ON A.CampusID = B.CampusID
      INNER JOIN tblStaffEvaluation AS C ON **C.**StaffID = A.StaffID

      or

      FROM tblStaffInfo A
      INNER JOIN tblCampuses B ON A.CampusID = B.CampusID
      INNER JOIN tblStaffEvaluation C ON **C.**StaffID = A.StaffID

      Once you have given a table an alias you can then no longer use the tablename to qualify which fields you want - you must use the alias. For example, this query

      SELECT A.StaffName, tblCampuses.CampusName,
      LessonPlanning, LessonPreprn, Regularity,
      ((LessonPlanning + LessonPreprn + Regularity) / 3) AS AvgScore
      FROM tblStaffInfo A
      INNER JOIN tblCampuses B ON A.CampusID = B.CampusID
      INNER JOIN tblStaffEvaluation C ON C.StaffID = A.StaffID
      ORDER BY A.StaffName

      will generate the error

      Quote:

      Msg 4104, Level 16, State 1, Line 24 The multi-part identifier "tblCampuses.CampusName" could not be bound.

      It should be B.CampusName. For the first part of your question you will have to explain to us what "The above Sql shows hatred for the division (/) sign" means. There is no error generated by your query and if the fields are defined as INT on the table schema then it gives the correct results. You will get strange results if you have stored those scores as char or varchar fields.

      1 Reply Last reply
      0
      • R Raabi Anony

        Hello everybody! I am facing two simple problems in the following code:

            Dim cmdText As String
            cmdText = "SELECT tblStaffInfo.StaffName, tblCampuses.CampusName, 
                       LessonPlanning, LessonPreprn, Regularity,
        	           ((LessonPlanning + LessonPreprn + Regularity)/3 ) AS AvgScore
                        FROM tblStaffInfo
                        INNER JOIN tblCampuses
                        ON tblStaffInfo.CampusID = tblCampuses.CampusID
                        INNER JOIN tblStaffEvaluation
                        ON tblStaffEvaluation.StaffID = tblStaffInfo.StaffID
                        ORDER BY tblStaffInfo.StaffName"
        

        (1)- The above Sql shows hatred for the division (/) sign (2)- How can use short alias for the long tables' name; like tblStaffInfo and tblCampuses etc. Please suggest!

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        (1) For the average problem, are any of these columns nullable? If so, your statement needs to be a bit more complex because NULL is not the same as 0. You might want to try this[^]. (2) As for the alias, it's ridiculously simple:

        SELECT si.StaffName, ci.CampusName, ...
        FROM tblStaffInfo AS si
        INNER JOIN tblCampuses AS ci
        ON si.CampusID = ci.CampusID
        ...
        

        A guide to posting questions on CodeProject

        Click this: Asking questions is a skill. Seriously, do it.
        Dave Kreskowiak

        R 1 Reply Last reply
        0
        • D Dave Kreskowiak

          (1) For the average problem, are any of these columns nullable? If so, your statement needs to be a bit more complex because NULL is not the same as 0. You might want to try this[^]. (2) As for the alias, it's ridiculously simple:

          SELECT si.StaffName, ci.CampusName, ...
          FROM tblStaffInfo AS si
          INNER JOIN tblCampuses AS ci
          ON si.CampusID = ci.CampusID
          ...
          

          A guide to posting questions on CodeProject

          Click this: Asking questions is a skill. Seriously, do it.
          Dave Kreskowiak

          R Offline
          R Offline
          Raabi Anony
          wrote on last edited by
          #4

          Thank you very much CHill60 and Dave for your help, especially very beautiful explanation by CHill60. Now, my problem got resolved. Wish you very good time and regards.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups