Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Vivek's SQL Notes

Vivek johari is currently a Analyst and have more that 5.5 yeras of experience in database. He has Master degree in Computer and also he is Microsoft certified Sql DBA (MCTS)& Microsoft certified SQl BI professional(MCTS). He is also Oracle certified profession(OCP)DBA in ORACLE 10g and ORACLE 9i.He has the experience of working in PL/SQL, T-SQL and SSIS/SSRS. His work basically involved designing and optimization of the Database.He has also published many database articles on his blog Technologies with Vivek Johari.

Derived Table In Sql Server

Derived tables:- Derived tables are the tables which are created on the fly with the help of the Select statement. It is different from the temporary table in the way that  in case of temporary table, first we have to create a temporary table,  insert the data into the table, select the data from the temporary table and then we have to drop the temporary table. But in case of derived table, SQL Server itself create and populate the table in the memory and we can directly use it. Also we don,t need to drop it. But it can only be referenced  by the outer Select query who created it. Also since it is reside in the memory itself, it is faster then Temporary tables which are created in the temp database.


Suppose, there is a table say tbl_studentcoursereg to store the id of the students registered for various courses, registration month and date. Table structure is given below:-



create table tbl_studentcoursereg (id int identity(1,1),  studentid int, coursename nvarchar(100), Regmonth nvarchar(50), regdate datetime)


The insert command to populate this table with the data is given below:-



insert into tbl_studentcoursereg
values (1, 'C#', 'JAN','01/01/2012')


insert into tbl_studentcoursereg
values (2, 'SQL', 'JAN','01/02/2012')


insert into tbl_studentcoursereg
values (3, 'C++', 'JAN','01/03/2012')


insert into tbl_studentcoursereg
values (4, 'C#', 'FEB','02/02/2012')


insert into tbl_studentcoursereg
values (5, 'C#', 'MAR','03/03/2012')


insert into tbl_studentcoursereg
values (6, 'JAVA', 'APR','04/04/2012')


insert into tbl_studentcoursereg
values (7, 'JAVA', 'APR','04/04/2012')


insert into tbl_studentcoursereg
values (8, 'ORACLE', 'APR','04/04/2012')


insert into tbl_studentcoursereg
values (9, 'C#', 'APR','04/23/2012')


insert into tbl_studentcoursereg
values (10, 'C#', 'MAY','05/05/2012')


Now if we want to know in which month number of students registered for a particular months is greater than 2, the we can use the following query which uses the derived table.



select regmonth, totalstud, totalcourse from 
(select  regmonth ,count(studentid) as totalstud ,
count(coursename)as totalcourse from tbl_studentcoursereg group by regmonth )as tbl1  where tbl1.totalstud>2



In the above query, the following is the derived table:-



(select  regmonth ,count(studentid) as totalstud ,
count(coursename)as totalcourse from tbl_studentcoursereg group by regmonth ) as tbl1

In case of derived table, we have to give the alias name to the derived table as tbl1 is the alias for the above mention derived table.







Comments

Leave a comment on the original post [feedproxy.google.com, opens in a new window]

Loading comments...