Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Need an example of a nested view

Author  Topic 

itmaster
Starting Member

28 Posts

Posted - 2013-08-13 : 08:28:14
Hi,
They want me to write a nested view and I canot find an example anywhere. Can someomne show me one please.

Thnk you

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-08-13 : 08:31:15
create view dbo.View1 AS select 1 as ColumnName
Go
create view dbo.View2 as select ColumnName, 2 * columnname AS TwiceColumnName from dbo.view1
go

select * from dbo.view2
go



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

itmaster
Starting Member

28 Posts

Posted - 2013-08-13 : 09:45:11
Thanks for the help and this ias my fault, but this is nto what I mean. They call it view but it isn't. It is is something like this:
select *
from t1
Inner join
(
Select Max(val)...
Inner join
(
SQL statement
Union
SQL statement
) vi1
....
This is not exact code, because that is what I am looking for I do not know how to do the nested vi1s?
the code I
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-08-13 : 09:48:45
Do you mean something like this?
CREATE TABLE #A (id INT, val FLOAT);
CREATE TABLE #B (id INT, val FLOAT);

SELECT
*
FROM
(
SELECT id, MAX(val) AS MaxValA FROM #A
) a
INNER JOIN
(
SELECT id, MAX(val) AS MaxValB FROM #B
UNION ALL
SELECT id, MAX(val) FROM #A
) b ON a.id = b.id;


DROP TABLE #A;
DROP TABLE #b;
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2013-08-13 : 09:49:55
You mean a derived table?


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-08-13 : 10:11:44
That is, as Peso said, a derived table. Sometimes is it also called an inline-view.
Go to Top of Page
   

- Advertisement -