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 object managed by a SQL Server developer.
To clarify, let talk about the three different types of views exist:
- Indexed views (it’s a materialized view that needs to be refreshed like a table)
- Partitioned views
- System views
How to create a SQL Server view?
Firstly, 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?
Secondly, 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;
To conclude, check the view definition in Wikipedia dedicated page, this time about the SQL Standard view, applicable to any vendor respecting the ISO standard.