WISDOMBYDATA
  • BLOG
    • Blog Guide
    • Blog History
  • EXCEL
    • Functions & Formulas
    • VBA & Macros
    • VLOOKUP
    • Pivot Tables
    • Conditional Formatting
    • Tricks & Shortcuts
  • BI
    • SAP BOBJ/BW
    • Tableau
  • SQL
  • ABOUT
    • About WBD
    • About Me

Using a View to Rename Columns

2/13/2014

0 Comments

 
A view is a virtual table defined by a query. Once created a view would behave like a table. in simple words, a view is a select statement, saved in a database and given a name. The most important benefit of using a view is limiting access to certain columns and rows and eliminating the need for executing large select statements.

In this blog post, I will create a view from one of the tables in the AdventureWorks2012 database. AdventureWorks is a sample Database shipped with SQL Server and can be downloaded from the "Microsoft SQL Server Database Product Samples" website.

The first step is to select all the columns from the Customer table in the AdventureWorks Database. As could be observed in the screenshot below 19,820 rows have been returned after doing so.
Picture
Now we create a view, named CustomerInfo, that selects only a few of the columns above. For the purpose of this example, I have chosen the CustomerID, AccountNumber, and ModifedDate columns for this view. see the screenshot below for the code used to create a view.
Picture
After successfully creating the view, I selected the data from the view. As could be observed below, the same 19,820 rows have been returned, however; only the three columns defined by the view statement have been displayed.
Picture
Now that I've demonstrated how a view is created, I want to use a view in order to change the column names displayed in the column header. Perhaps the person who created the table has used column names that you find confusing or don't like to use. Creating a view is one of the best ways for resolving this issue, as it is more convenient than using column aliases, given that a view only needs to be executed once.

In order to rename the column names using a view, we will first need to drop the view and then recreate it, with the difference that this time we will define the new column names inside parenthesis next to the view name. see the screenshot below.
Picture
After successfully creating the new view, we select data from the view. Note the change in column names.
Picture
0 Comments



Leave a Reply.

    Categories

    All
    BI
    EXCEL
    MISC
    SQL

    Archives

    June 2020
    May 2020
    April 2020
    March 2020
    February 2020
    December 2019
    November 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    July 2018
    June 2018
    May 2018
    April 2018
    March 2018
    September 2017
    August 2017
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    May 2015
    April 2015
    March 2015
    February 2015
    January 2015
    December 2014
    November 2014
    October 2014
    September 2014
    August 2014
    April 2014
    March 2014
    February 2014
    January 2014
    December 2013
    November 2013

Picture
  • BLOG
    • Blog Guide
    • Blog History
  • EXCEL
    • Functions & Formulas
    • VBA & Macros
    • VLOOKUP
    • Pivot Tables
    • Conditional Formatting
    • Tricks & Shortcuts
  • BI
    • SAP BOBJ/BW
    • Tableau
  • SQL
  • ABOUT
    • About WBD
    • About Me