23333d5c1593f9a0ad6502771667a8bfddf78ca8
Integration/Tutorials/Views.md
| ... | ... | @@ -0,0 +1,168 @@ |
| 1 | +#**Views** |
|
| 2 | + |
|
| 3 | +[[_TOC_]] |
|
| 4 | + |
|
| 5 | +###Why should I create views? |
|
| 6 | + |
|
| 7 | +Izenda recommends using views as part of our best practices to optimize and simplify the user experience. Views offer the ability to draw data in from many sources, organize data, and perform many calculations on data before the user uses it as a report data source. |
|
| 8 | + |
|
| 9 | +###What are views? |
|
| 10 | + |
|
| 11 | +In SQL, a VIEW is a virtual table based on the result-set of a SELECT statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add the SQL functions WHERE and JOIN to a view and present the data as if the data were coming from a single table. |
|
| 12 | + |
|
| 13 | +Note: The database design and structure will NOT be affected by the WHERE or JOIN functions in a view. |
|
| 14 | + |
|
| 15 | +###How do I create views for my database? |
|
| 16 | + |
|
| 17 | +Izenda Reports offers a direct front end for your database meaning that there is no need to create models, catalogs or secondary schemas. |
|
| 18 | + |
|
| 19 | +Essentially the product does an analysis of the metadata rather than requiring a developer to create a mapping layer. This approach makes integration and maintenance significantly easier in that the product does not generally need to be maintained separately from the database. Most databases have evolved in such a way that the schema may be too complex from direct consumption by the user. Izenda recommends creating a series of views that present the user with a simplified and secure perspective to work with. |
|
| 20 | + |
|
| 21 | +Izenda recommends creating views that use Camel Case (i.e. FirstName) or underscore format (i.e. first_name) to limit how much manual typing a report designer needs to do. |
|
| 22 | + |
|
| 23 | +###View Code Samples |
|
| 24 | + |
|
| 25 | +**A Basic View** |
|
| 26 | + |
|
| 27 | +A view is generally created by adding the create view statement to a standard SQL statement. |
|
| 28 | + |
|
| 29 | +``` sql |
|
| 30 | +CREATE VIEW SampleView AS SELECT FirstName FROM User |
|
| 31 | +``` |
|
| 32 | + |
|
| 33 | +This article will use a fictitious database to provide samples on how to best utilize views. |
|
| 34 | + |
|
| 35 | + |
|
| 36 | +**Table and Column Aliasing** |
|
| 37 | + |
|
| 38 | +Tables and columns are often named in ways that are not intuitive to the user. Izenda recommends aliasing in such situations. |
|
| 39 | + |
|
| 40 | +``` sql |
|
| 41 | +CREATE VIEW Persons AS SELECT ctx_fname AS FirstName, ctx_lname AS LastName FROM ctx_usrs |
|
| 42 | +``` |
|
| 43 | + |
|
| 44 | +**Combining Columns** |
|
| 45 | + |
|
| 46 | +``` sql |
|
| 47 | +CREATE VIEW Events AS |
|
| 48 | +SELECT CASE DATEPART(w,CreateDate) |
|
| 49 | + WHEN ‘1’ THEN ‘Sunday’ |
|
| 50 | + WHEN ‘2’ THEN ‘Monday’ |
|
| 51 | + END AS DayOfWeekAggregating Data |
|
| 52 | +CREATE VIEW OrderSummary |
|
| 53 | +SELECT DATEPART(yyyy,OrderDate) AS OrderMonth |
|
| 54 | + ShipCountry AS Country, |
|
| 55 | + COUNT(OrderID) AS Orders |
|
| 56 | +FROM ORDERS |
|
| 57 | +GROUP BY ShipCountry,DATEPART(yyyy,OrderDate) |
|
| 58 | +``` |
|
| 59 | + |
|
| 60 | +**Pre-Joining Common Data Sources** |
|
| 61 | + |
|
| 62 | +``` sql |
|
| 63 | +CREATE VIEW Persons AS |
|
| 64 | +SELECT ctx_name |
|
| 65 | + AS Name, dep_dept_name |
|
| 66 | + AS Department |
|
| 67 | +FROM ctx_usrs |
|
| 68 | +JOIN ctx_depts |
|
| 69 | + ON ctx_usrs.did = ctx_depts.ctx_depts_id |
|
| 70 | +Combining Multiple Databases |
|
| 71 | + |
|
| 72 | +CREATE VIEW Persons AS |
|
| 73 | +SELECT DB1..ctx_name |
|
| 74 | + AS Name,DB2.. dep_dept_name |
|
| 75 | + AS Department |
|
| 76 | +FROM DB1..ctx_usrs |
|
| 77 | +JOIN DB2..ctx_depts |
|
| 78 | + ON DB1..ctx_usrs.did = DB2..ctx_depts.ctx_depts_id |
|
| 79 | +``` |
|
| 80 | + |
|
| 81 | +**Combining Similar Data from Multiple Data Sources** |
|
| 82 | + |
|
| 83 | +``` sql |
|
| 84 | +CREATE VIEW Persons AS |
|
| 85 | +SELECT ctx_name |
|
| 86 | + AS Name |
|
| 87 | +FROM ctx_usrs |
|
| 88 | +UNION SELECT emp_name |
|
| 89 | + AS Name from ctx_employees |
|
| 90 | +``` |
|
| 91 | + |
|
| 92 | +**Converting Numeric Values into Friendly Names** |
|
| 93 | + |
|
| 94 | +``` sql |
|
| 95 | +CREATE VIEW Events AS |
|
| 96 | +SELECT CASE DATEPART(w,CreateDate) |
|
| 97 | + WHEN ‘1’ THEN ‘Sunday’ |
|
| 98 | + WHEN ‘2’ THEN ‘Monday’ |
|
| 99 | +END AS DayOfWeekAggregating Data |
|
| 100 | + |
|
| 101 | +CREATE VIEW OrderSummary |
|
| 102 | +SELECT DATEPART(yyyy,OrderDate) AS OrderMonth |
|
| 103 | + ShipCountry AS Country, |
|
| 104 | + COUNT(OrderID) AS Orders |
|
| 105 | +FROM ORDERS |
|
| 106 | +GROUP BY ShipCountry,DATEPART(yyyy,OrderDate) |
|
| 107 | +``` |
|
| 108 | + |
|
| 109 | +###Using Case Statements |
|
| 110 | + |
|
| 111 | +**Simple Example** |
|
| 112 | + |
|
| 113 | +``` sql |
|
| 114 | +SELECT title, price, Budget = CASE price |
|
| 115 | + WHEN price > 20.00 THEN 'Expensive' |
|
| 116 | + WHEN price BETWEEN 10.00 AND 19.99 THEN 'Moderate' |
|
| 117 | + WHEN price < 10.00 THEN 'Inexpensive' |
|
| 118 | +ELSE 'Unknown' |
|
| 119 | +END, |
|
| 120 | +FROM titles |
|
| 121 | +``` |
|
| 122 | + |
|
| 123 | +**Range Field Example** |
|
| 124 | + |
|
| 125 | +``` sql |
|
| 126 | +CREATE VIEW [RangeFieldExample] AS |
|
| 127 | +SELECT CASE |
|
| 128 | + WHEN [Freight] > 0 AND [Freight] < 1 THEN '$0 - $1' |
|
| 129 | + WHEN [Freight] > 1 AND [Freight] < 10 THEN '$1 - $10' |
|
| 130 | + WHEN [Freight] > 10 AND [Freight] < 100 THEN '$100 - $1' |
|
| 131 | + WHEN [Freight] > 100 AND [Freight] < 1000 THEN '$100 - $1000' |
|
| 132 | + WHEN [Freight] > 1000 THEN '$1000 'END |
|
| 133 | +AS [FreightRange],* |
|
| 134 | +FROM [Orders] |
|
| 135 | +``` |
|
| 136 | + |
|
| 137 | +###Pivot View Code Samples |
|
| 138 | + |
|
| 139 | +**Pivot With Total Example** |
|
| 140 | + |
|
| 141 | +``` sql |
|
| 142 | +CREATE VIEW [PivotWithTotalExample] AS |
|
| 143 | +SELECT[ShipCountry] |
|
| 144 | + AS [Country],(CASE [Shippers].[CompanyName] |
|
| 145 | + WHEN 'Federal Shipping' |
|
| 146 | + THEN Freight ELSE 0 END) |
|
| 147 | + AS [Federal Shipping], (CASE [Shippers].[CompanyName] |
|
| 148 | + WHEN 'Speedy Express' |
|
| 149 | + THEN Freight ELSE 0 END) |
|
| 150 | + AS [Speedy Express], (CASE [Shippers].[CompanyName] |
|
| 151 | + WHEN 'United Package' |
|
| 152 | + THEN Freight ELSE 0 END) |
|
| 153 | + AS [United Package], Freight As [TotalFreight] |
|
| 154 | +FROM [Orders] |
|
| 155 | +JOIN [Shippers] on [Shippers].[ShipperID] = [Orders].[ShipVia] |
|
| 156 | +``` |
|
| 157 | + |
|
| 158 | +**Year Pivot** |
|
| 159 | + |
|
| 160 | +``` sql |
|
| 161 | +SELECT [ShipCountry],SUM(Freight) AS Total,SUM(CASE DATEPART(yyyy,[OrderDate]) |
|
| 162 | + WHEN '2005' THEN Freight ELSE 0 END) AS [2005], SUM(CASE DATEPART(yyyy,[OrderDate]) |
|
| 163 | + WHEN '2006' THEN Freight ELSE 0 END) AS [2006], SUM(CASE DATEPART(yyyy,[OrderDate]) |
|
| 164 | + WHEN '2007' THEN Freight ELSE 0 END) AS [2007], SUM(CASE DATEPART(yyyy,[OrderDate]) |
|
| 165 | + WHEN '2008' THEN Freight ELSE 0 END) AS [2008] |
|
| 166 | +FROM [Orders] |
|
| 167 | +GROUP BY [ShipCountry] |
|
| 168 | +``` |
|
| ... | ... | \ No newline at end of file |
Integration/Views.md
| ... | ... | @@ -1,168 +0,0 @@ |
| 1 | -#**Views** |
|
| 2 | - |
|
| 3 | -[[_TOC_]] |
|
| 4 | - |
|
| 5 | -###Why should I create views? |
|
| 6 | - |
|
| 7 | -Izenda recommends using views as part of our best practices to optimize and simplify the user experience. Views offer the ability to draw data in from many sources, organize data, and perform many calculations on data before the user uses it as a report data source. |
|
| 8 | - |
|
| 9 | -###What are views? |
|
| 10 | - |
|
| 11 | -In SQL, a VIEW is a virtual table based on the result-set of a SELECT statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add the SQL functions WHERE and JOIN to a view and present the data as if the data were coming from a single table. |
|
| 12 | - |
|
| 13 | -Note: The database design and structure will NOT be affected by the WHERE or JOIN functions in a view. |
|
| 14 | - |
|
| 15 | -###How do I create views for my database? |
|
| 16 | - |
|
| 17 | -Izenda Reports offers a direct front end for your database meaning that there is no need to create models, catalogs or secondary schemas. |
|
| 18 | - |
|
| 19 | -Essentially the product does an analysis of the metadata rather than requiring a developer to create a mapping layer. This approach makes integration and maintenance significantly easier in that the product does not generally need to be maintained separately from the database. Most databases have evolved in such a way that the schema may be too complex from direct consumption by the user. Izenda recommends creating a series of views that present the user with a simplified and secure perspective to work with. |
|
| 20 | - |
|
| 21 | -Izenda recommends creating views that use Camel Case (i.e. FirstName) or underscore format (i.e. first_name) to limit how much manual typing a report designer needs to do. |
|
| 22 | - |
|
| 23 | -###View Code Samples |
|
| 24 | - |
|
| 25 | -**A Basic View** |
|
| 26 | - |
|
| 27 | -A view is generally created by adding the create view statement to a standard SQL statement. |
|
| 28 | - |
|
| 29 | -``` sql |
|
| 30 | -CREATE VIEW SampleView AS SELECT FirstName FROM User |
|
| 31 | -``` |
|
| 32 | - |
|
| 33 | -This article will use a fictitious database to provide samples on how to best utilize views. |
|
| 34 | - |
|
| 35 | - |
|
| 36 | -**Table and Column Aliasing** |
|
| 37 | - |
|
| 38 | -Tables and columns are often named in ways that are not intuitive to the user. Izenda recommends aliasing in such situations. |
|
| 39 | - |
|
| 40 | -``` sql |
|
| 41 | -CREATE VIEW Persons AS SELECT ctx_fname AS FirstName, ctx_lname AS LastName FROM ctx_usrs |
|
| 42 | -``` |
|
| 43 | - |
|
| 44 | -**Combining Columns** |
|
| 45 | - |
|
| 46 | -``` sql |
|
| 47 | -CREATE VIEW Events AS |
|
| 48 | -SELECT CASE DATEPART(w,CreateDate) |
|
| 49 | - WHEN ‘1’ THEN ‘Sunday’ |
|
| 50 | - WHEN ‘2’ THEN ‘Monday’ |
|
| 51 | - END AS DayOfWeekAggregating Data |
|
| 52 | -CREATE VIEW OrderSummary |
|
| 53 | -SELECT DATEPART(yyyy,OrderDate) AS OrderMonth |
|
| 54 | - ShipCountry AS Country, |
|
| 55 | - COUNT(OrderID) AS Orders |
|
| 56 | -FROM ORDERS |
|
| 57 | -GROUP BY ShipCountry,DATEPART(yyyy,OrderDate) |
|
| 58 | -``` |
|
| 59 | - |
|
| 60 | -**Pre-Joining Common Data Sources** |
|
| 61 | - |
|
| 62 | -``` sql |
|
| 63 | -CREATE VIEW Persons AS |
|
| 64 | -SELECT ctx_name |
|
| 65 | - AS Name, dep_dept_name |
|
| 66 | - AS Department |
|
| 67 | -FROM ctx_usrs |
|
| 68 | -JOIN ctx_depts |
|
| 69 | - ON ctx_usrs.did = ctx_depts.ctx_depts_id |
|
| 70 | -Combining Multiple Databases |
|
| 71 | - |
|
| 72 | -CREATE VIEW Persons AS |
|
| 73 | -SELECT DB1..ctx_name |
|
| 74 | - AS Name,DB2.. dep_dept_name |
|
| 75 | - AS Department |
|
| 76 | -FROM DB1..ctx_usrs |
|
| 77 | -JOIN DB2..ctx_depts |
|
| 78 | - ON DB1..ctx_usrs.did = DB2..ctx_depts.ctx_depts_id |
|
| 79 | -``` |
|
| 80 | - |
|
| 81 | -**Combining Similar Data from Multiple Data Sources** |
|
| 82 | - |
|
| 83 | -``` sql |
|
| 84 | -CREATE VIEW Persons AS |
|
| 85 | -SELECT ctx_name |
|
| 86 | - AS Name |
|
| 87 | -FROM ctx_usrs |
|
| 88 | -UNION SELECT emp_name |
|
| 89 | - AS Name from ctx_employees |
|
| 90 | -``` |
|
| 91 | - |
|
| 92 | -**Converting Numeric Values into Friendly Names** |
|
| 93 | - |
|
| 94 | -``` sql |
|
| 95 | -CREATE VIEW Events AS |
|
| 96 | -SELECT CASE DATEPART(w,CreateDate) |
|
| 97 | - WHEN ‘1’ THEN ‘Sunday’ |
|
| 98 | - WHEN ‘2’ THEN ‘Monday’ |
|
| 99 | -END AS DayOfWeekAggregating Data |
|
| 100 | - |
|
| 101 | -CREATE VIEW OrderSummary |
|
| 102 | -SELECT DATEPART(yyyy,OrderDate) AS OrderMonth |
|
| 103 | - ShipCountry AS Country, |
|
| 104 | - COUNT(OrderID) AS Orders |
|
| 105 | -FROM ORDERS |
|
| 106 | -GROUP BY ShipCountry,DATEPART(yyyy,OrderDate) |
|
| 107 | -``` |
|
| 108 | - |
|
| 109 | -###Using Case Statements |
|
| 110 | - |
|
| 111 | -**Simple Example** |
|
| 112 | - |
|
| 113 | -``` sql |
|
| 114 | -SELECT title, price, Budget = CASE price |
|
| 115 | - WHEN price > 20.00 THEN 'Expensive' |
|
| 116 | - WHEN price BETWEEN 10.00 AND 19.99 THEN 'Moderate' |
|
| 117 | - WHEN price < 10.00 THEN 'Inexpensive' |
|
| 118 | -ELSE 'Unknown' |
|
| 119 | -END, |
|
| 120 | -FROM titles |
|
| 121 | -``` |
|
| 122 | - |
|
| 123 | -**Range Field Example** |
|
| 124 | - |
|
| 125 | -``` sql |
|
| 126 | -CREATE VIEW [RangeFieldExample] AS |
|
| 127 | -SELECT CASE |
|
| 128 | - WHEN [Freight] > 0 AND [Freight] < 1 THEN '$0 - $1' |
|
| 129 | - WHEN [Freight] > 1 AND [Freight] < 10 THEN '$1 - $10' |
|
| 130 | - WHEN [Freight] > 10 AND [Freight] < 100 THEN '$100 - $1' |
|
| 131 | - WHEN [Freight] > 100 AND [Freight] < 1000 THEN '$100 - $1000' |
|
| 132 | - WHEN [Freight] > 1000 THEN '$1000 'END |
|
| 133 | -AS [FreightRange],* |
|
| 134 | -FROM [Orders] |
|
| 135 | -``` |
|
| 136 | - |
|
| 137 | -###Pivot View Code Samples |
|
| 138 | - |
|
| 139 | -**Pivot With Total Example** |
|
| 140 | - |
|
| 141 | -``` sql |
|
| 142 | -CREATE VIEW [PivotWithTotalExample] AS |
|
| 143 | -SELECT[ShipCountry] |
|
| 144 | - AS [Country],(CASE [Shippers].[CompanyName] |
|
| 145 | - WHEN 'Federal Shipping' |
|
| 146 | - THEN Freight ELSE 0 END) |
|
| 147 | - AS [Federal Shipping], (CASE [Shippers].[CompanyName] |
|
| 148 | - WHEN 'Speedy Express' |
|
| 149 | - THEN Freight ELSE 0 END) |
|
| 150 | - AS [Speedy Express], (CASE [Shippers].[CompanyName] |
|
| 151 | - WHEN 'United Package' |
|
| 152 | - THEN Freight ELSE 0 END) |
|
| 153 | - AS [United Package], Freight As [TotalFreight] |
|
| 154 | -FROM [Orders] |
|
| 155 | -JOIN [Shippers] on [Shippers].[ShipperID] = [Orders].[ShipVia] |
|
| 156 | -``` |
|
| 157 | - |
|
| 158 | -**Year Pivot** |
|
| 159 | - |
|
| 160 | -``` sql |
|
| 161 | -SELECT [ShipCountry],SUM(Freight) AS Total,SUM(CASE DATEPART(yyyy,[OrderDate]) |
|
| 162 | - WHEN '2005' THEN Freight ELSE 0 END) AS [2005], SUM(CASE DATEPART(yyyy,[OrderDate]) |
|
| 163 | - WHEN '2006' THEN Freight ELSE 0 END) AS [2006], SUM(CASE DATEPART(yyyy,[OrderDate]) |
|
| 164 | - WHEN '2007' THEN Freight ELSE 0 END) AS [2007], SUM(CASE DATEPART(yyyy,[OrderDate]) |
|
| 165 | - WHEN '2008' THEN Freight ELSE 0 END) AS [2008] |
|
| 166 | -FROM [Orders] |
|
| 167 | -GROUP BY [ShipCountry] |
|
| 168 | -``` |
|
| ... | ... | \ No newline at end of file |