Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
SQL Server 119
#SQLServer 88
Oracle 70
#SQL SERVER 35
BRH 31
SQL Server 2012 29
denali 23
#TSQL 19
TSQL 19
C# 15

Archive · View All
October 2011 31
November 2011 30
September 2011 30
August 2011 18
December 2011 15
July 2011 13
June 2011 8
May 2012 4
April 2012 3
January 2010 3

Day 6: Common Table Expressions using WITH clause in PostgreSQL

Dec 6 2011 10:26PM by Niladri Biswas   

Common Table Expression(CTE) is particularly useful when subquery is executed multiple times. It is equally helpful in place of temporary tables.It computes the aggregation once, and allows us to reference it by its name (may be multiple times) in the queries.It materialize subqueries thereby helping oracle not to recompute them multiple times.In PostgreSQL, we use the WITH clause for writing CTE based queries.It helps in breaking down complicated and large queries into simpler forms which is easily readable.

Let us see how we can do so

Simple CTE

With CTE AS
(	
  Select 
	empid
	, empname
	, salary
	, belongsto
	, deptid
  FROM tblemployee

)
Select * From CTE;

One thing to notice here is that in Sql Server we start with a semicolon(;) before writing a CTE inorder to differentiate it from other statements previously written.

This is a simple CTE where basically we are projecting the employee table records

Multiple CTE or Chain CTE

With CTEEmp AS
(	
  Select 
	empid
	, empname
	, salary
	, belongsto
	, deptid
  FROM tblemployee
)
,CTEDept As
(
  Select e.*,d.deptname
  From CTEEmp e
  Join tbldept d
  On e.deptid = d.deptid
)
Select * From CTEDept;

It is call as CTE chains/multiple CTEs.We can find that we are using the resultset of the first CTE (CTEEmp) into the second one(CTEDept) to obtain the result

Recursive CTE or Hierarchial queries

It is the third form of CTE where a CTE can reference to itself. So we call it as recursive CTE. In PostgreSQL, we can achieve so by using the Recursive keyword in the CTE which ensures that the query can refer to its own output

In the next example we will look into how we can generate a number table by the help of Recursive CTE

With Recursive CTE(Rn) AS
(	
	Select 1
	Union All
	Select Rn + 1 From CTE
	Where Rn < 10
)
Select * From CTE;

In Sql Server , CTE was introduce since version 2005. It is available in all the three flavours described here. But for using a recursive CTE, we donot use "Recursive" keyword. We will write the same program for generating a number table using recursive CTE using SQL Serevr CTE syntax below

;With CTE AS
(	
	Select Rn = 1
	Union All
	Select Rn + 1 From CTE
	Where Rn < 10
)
Select * From CTE;

Reference(s)

  1. WITH Queries (Common Table Expressions)
  2. CTEReadme

Hope this helps

Thanks for reading

Tags: PostgreSQL,


Niladri Biswas
7 · 21% · 6710
1
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Day 6: Common Table Expressions using WITH clause in PostgreSQL" rated 5 out of 5 by 1 readers
Day 6: Common Table Expressions using WITH clause in PostgreSQL , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]