How to create a SQL Server view to display specific columns from an existing table? Consider a view as a virtual table defined by a SQL query. It’s a set of columns and rows from a table. A user defined view is a view created by a SQL Server developer.
Three different types of views exist:
- Indexed views (it’s a materialized view that needs to be refreshed)
- Partitioned views
- System views
How to create a SQL Server view?
Let’s consider the Sales table created in this article, to create a view displaying a part of the table content, use the script below and adjust it.
CREATE VIEW [dbo].[SalesByMonth] AS SELECT [MonthName], [NumberMonth], SUM([NumberOfClients]) AS [NumberOfClients], SUM([Amount_ET]) AS [Amount_ET], SUM([Amount_IT]) AS [Amount_IT] FROM [dbo].[Sales] GROUP BY [MonthName], [NumberMonth] ORDER BY [NumberMonth] ASC GO
How to call the view?
Simply use a select command like this one, for example to select the second part of the year:
SELECT * FROM [dbo].[SalesByMonth] WHERE [NumberMonth] > 6;