<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://beyondrelational.com/live/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tag 'PostgreSQL'</title><link>http://beyondrelational.com/live/search/SearchResults.aspx?a=1&amp;o=DateDescending&amp;tag=PostgreSQL&amp;orTags=0</link><description>Search results matching tag 'PostgreSQL'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Interfacing C# application (basic CRUD using functions) with PostgreSQL(9.1.0-1) and NpgSql(2.0.11.91) as .Net data provider - Part 2</title><link>http://beyondrelational.com/live/blogs/niladribiswas/archive/2011/12/31/interfacing-c-application-basic-crud-using-functions-with-postgresql-9-1-0-1-and-npgsql-2-0-11-91-as-net-data-provider-part-2.aspx</link><pubDate>Sat, 31 Dec 2011 11:39:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:13753</guid><dc:creator>niladribiswas</dc:creator><description>&lt;h2&gt;Interfacing C# application (basic CRUD using functions) with PostgreSQL(9.1.0-1) and NpgSql(2.0.11.91) as .Net data provider - Part 2&lt;/h2&gt;
&lt;p&gt;PostgreSQL does not support the traditional way of creating stored procedure found in other databases like Sql Server, Oracle etc. It lacks the famous &lt;b&gt;Create Procedure&lt;/b&gt; syntax.&lt;/p&gt;
&lt;p&gt;However,we can do so by the help of functions.We know that stored procedures can return multiple result sets which a functions can not. In PostgreSQL we can do so by returing a set of refcursors. In this article we will look into how to do so by extending our&lt;a href="http://beyondrelational.com/blogs/niladribiswas/archive/2011/12/01/interfacing-c-application-basic-crud-using-inline-queries-with-postgresql-9-1-0-1-and-npgsql-2-0-11-91-as-net-data-provider-part-1.aspx"&gt; previous example&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We will first create the environment.Let us first create the Player table(tblPlayers) found in the earlier example&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
CREATE TABLE tblPlayers (
        PlayerName Varchar(50)
		,Salary Int
		,BelongsTo Varchar(50)
		,Age Int
);
&lt;/pre&gt;
&lt;p&gt;Next let us create the functions as under&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Create Select All Record Function&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
CREATE OR REPLACE FUNCTION fnFetchPlayerRecord() RETURNS SETOF refcursor AS

&amp;#39;DECLARE 
  recordSet  refcursor;  
BEGIN

OPEN recordSet FOR 
 Select * from tblPlayers;
RETURN NEXT recordSet ;

RETURN;
END;&amp;#39;
LANGUAGE plpgsql;
&lt;/pre&gt;
&lt;p&gt;We can find out that the return type is &lt;b&gt;refcursor&lt;/b&gt;. In this way we can send the record sets back to the client.Also the language type is &lt;b&gt;plpgsql&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Create Insert Function&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
CREATE OR REPLACE FUNCTION fnInsertRecord(varchar,int,varchar,int) RETURNS void AS  
&amp;#39;BEGIN
		Insert Into tblPlayers(playername,age,belongsto,salary) values ($1,$2,$3,$4);
END;&amp;#39;
LANGUAGE plpgsql;
&lt;/pre&gt;
&lt;p&gt;This function accepts 4 parameters.The first and third being of Varchar type while the second and fourth is integer type and returns void. Observe that the values being inserted by using the positional parameter (e.g. $1,$2 etc.). It is different from Oracle or SQL Server where we do so by using the parameter name.Also at the time of declaring the &lt;b&gt;CREATE PROCEDURE&lt;/b&gt; statement in those databases, we name the Parameters and their type.But in PostgreSQL, mentioning only the datatype is enough&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Create Update Function&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
CREATE OR REPLACE FUNCTION fnUpdateRecord(varchar,int) RETURNS void AS  
&amp;#39;BEGIN		
		Update tblPlayers Set salary  = salary + $2 Where playername = $1;		
END;&amp;#39;
LANGUAGE plpgsql;
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Create Delete Function&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
CREATE OR REPLACE FUNCTION fnDeleteRecord(varchar) RETURNS void AS  
&amp;#39;BEGIN		
		Delete From tblPlayers Where playername = $1;		
END;&amp;#39;
LANGUAGE plpgsql;
&lt;/pre&gt;
&lt;p&gt;So, we have written all kind of basic scripts for our operation&lt;/p&gt;
&lt;p&gt;Now we will look into the interfacing code in our DAL layer.We first need to add a reference to&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Mono.Security.dll&lt;/li&gt;
	&lt;li&gt;Npgsql.dll&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;We will now look into as how we can invoke these functions from DAL layer&lt;/p&gt;
&lt;p&gt;First let us look into how we are invoking the &lt;b&gt;fnFetchPlayerRecord&lt;/b&gt; function(1st function)&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
public DataTable GetAllRecords()
{

	DataTable dtRecord = new DataTable();           

	try
	{
		using (NpgsqlConnection pgsqlConnection = new NpgsqlConnection(connString))
		{
			// Open the PgSQL Connection.                
			pgsqlConnection.Open();

			string selectCommand = &amp;quot;fnFetchPlayerRecord&amp;quot;;                    

			using (NpgsqlCommand pgsqlcommand = new NpgsqlCommand(selectCommand, pgsqlConnection))
			{
				using (NpgsqlTransaction tran = pgsqlConnection.BeginTransaction())
				{
					pgsqlcommand.CommandType = CommandType.StoredProcedure;

					using (NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(pgsqlcommand))
					{
						Adpt.Fill(dtRecord);
					}
					tran.Commit();
				}
			}
		}
	}
	catch (NpgsqlException ex)
	{
		throw ex;
	}
	catch (Exception ex)
	{
		throw ex;
	}
	return dtRecord;
}
&lt;/pre&gt;
&lt;p&gt;It is preety much the same code as we found while interfacing with other databases.Since we know that, our function returns only one record set we are using Datatable else we can use DataSet.We are using &lt;b&gt;transaction&lt;/b&gt; for preventing cursors returned by refcursor function from closing after the implicity transaction is finished.In the CommandText of the NpgsqlCommand class , we are specifying the function name.&lt;/p&gt;
&lt;p&gt;Next we will see the way to invoke the Insert function (fnInsertRecord)&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
public void InsertRecord(string PlayerName, int Age, string BelongsTo, int Salary)
{

	try
	{
		using (NpgsqlConnection pgsqlConnection = new NpgsqlConnection(connString))
		{
			// Open the PgSQL Connection.                  
			pgsqlConnection.Open();

			string insertCommand = &amp;quot;fnInsertRecord&amp;quot;;

			using (NpgsqlCommand pgsqlcommand = new NpgsqlCommand(insertCommand, pgsqlConnection))
			{
				using (NpgsqlTransaction tran = pgsqlConnection.BeginTransaction())
				{
					pgsqlcommand.CommandType = CommandType.StoredProcedure;

					pgsqlcommand.Parameters.Add(new NpgsqlParameter(&amp;quot;playername&amp;quot;, NpgsqlDbType.Varchar));
					pgsqlcommand.Parameters.Add(new NpgsqlParameter(&amp;quot;age&amp;quot;, NpgsqlDbType.Integer));
					pgsqlcommand.Parameters.Add(new NpgsqlParameter(&amp;quot;belongsto&amp;quot;, NpgsqlDbType.Varchar));
					pgsqlcommand.Parameters.Add(new NpgsqlParameter(&amp;quot;salary&amp;quot;, NpgsqlDbType.Integer));

					pgsqlcommand.Parameters[0].Value = PlayerName;
					pgsqlcommand.Parameters[1].Value = Age;
					pgsqlcommand.Parameters[2].Value = BelongsTo;
					pgsqlcommand.Parameters[3].Value = Salary;

					pgsqlcommand.ExecuteNonQuery();
					
					tran.Commit();
				}
			}                   
		}
	}
	catch (NpgsqlException ex)
	{
		throw ex;
	}
	catch (Exception ex)
	{
		throw ex;
	}
}
&lt;/pre&gt;
&lt;p&gt;This follows the similar concept as the previous one&lt;/p&gt;
&lt;p&gt;The other DML functions follows the same pattern.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;References &lt;/b&gt;&lt;a href="http://npgsql.projects.postgresql.org/docs/manual/UserManual.html"&gt;Npgsql: User&amp;#39;s Manual&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Hope this will help those who want&amp;#39;s start their dotnet client interfacing with PostgreSQL DB.Thanks for reading the article.Attached is the zipped file that contains a sample windows application and the PostgreSQL script&lt;/p&gt;








</description></item><item><title>Interfacing C# application (basic CRUD using inline queries) with PostgreSQL(9.1.0-1) and NpgSql(2.0.11.91) as .Net data provider - Part 1</title><link>http://beyondrelational.com/live/blogs/niladribiswas/archive/2011/12/30/interfacing-c-application-basic-crud-using-inline-queries-with-postgresql-9-1-0-1-and-npgsql-2-0-11-91-as-net-data-provider-part-1.aspx</link><pubDate>Fri, 30 Dec 2011 08:10:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:13752</guid><dc:creator>niladribiswas</dc:creator><description>&lt;p&gt;In this article we will look into how to interface C# application with PostgreSQL(9.1.0-1) and PgAdmin III(1.14.0) which was release on Sept 9, 2011.This powerful RDBMS is there for a long time and it is free also which indicates that we will not have any licensing issue if we use it in our commercial applications.&lt;/p&gt;
&lt;p&gt;First we need to download the PostgreSQL(9.1.0-1) software from &lt;a href="http://www.enterprisedb.com/products-services-training/pgdownload"&gt;here&lt;/a&gt;. and 
NpgSql(2.0.11.91) from &lt;a href="http://pgfoundry.org/frs/?group_id=1000140&amp;amp;release_id=958"&gt;here&lt;/a&gt;.I have download &lt;i&gt;Npgsql2.0.11.91-bin-ms.net4.0.zip&lt;/i&gt; since I will use .Net Framework 4 for this application.&lt;/p&gt;
&lt;p&gt;Once the &lt;i&gt;Npgsql2.0.11.91-bin-ms.net4.0.zip&lt;/i&gt; is downloaded, we need to unzip that inside which we will find&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Mono.Security.dll&lt;/li&gt;
	&lt;li&gt;Npgsql.dll&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These two dlls will participate while interfacing the C# application with PostgreSQL&lt;/p&gt;
&lt;p&gt;Now let us create a database(say TestDB) and have a &lt;b&gt;Player table(tblPlayers)&lt;/b&gt; as under.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
CREATE TABLE tblPlayers (
        PlayerName Varchar(50)
		,Salary Int
		,BelongsTo Varchar(50)
		,Age Int
);
&lt;/pre&gt;
&lt;p&gt;Now, let us create a simple windows form application with 4 lables (for 4 fields) and 3 textboxes (for PlayerName,Salary,Age fields) and a ComboBox control for (BelongsTo that will have values as India, Australia,Singapore and USA), a DatagridView and 4 buttons (Insert, Update , Delete, Display)&lt;/p&gt;
&lt;p&gt;Next add reference to &lt;b&gt;Npgsql.dll and Mono.Security.dll&lt;/b&gt; in our project&lt;/p&gt;
&lt;p&gt;We will now prepare our DAL layer&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
using System;
using System.Data;
using Npgsql;

namespace App1
{
    public class Dal
    {
        static string serverName = &amp;quot;127.0.0.1&amp;quot;; //localhost
        static string port = &amp;quot;5432&amp;quot;; // default port 
        static string userName = &amp;quot;postgres&amp;quot;; //admin name
        static string password = &amp;quot;niladri_1234&amp;quot;; //admin password
        static string databaseName = &amp;quot;TestDB&amp;quot;; //database name

        string connString = String.Format(&amp;quot;Server={0};Port={1};User Id={2};Password={3};Database={4};&amp;quot;,
                                           serverName, port, userName, password, databaseName);

        //Get all records
        public DataTable GetAllRecords()
        { 

            DataTable dtRecord = new DataTable();

            try
            {
                using (NpgsqlConnection pgsqlConnection = new NpgsqlConnection(connString))
                {
                    // Open the PgSQL Connection.                
                    pgsqlConnection.Open();
                    string selectCommand = &amp;quot;Select * from tblPlayers&amp;quot;;

                    using (NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(selectCommand, pgsqlConnection))
                    {
                        Adpt.Fill(dtRecord);
                    }                   
                }
            }
            catch (NpgsqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dtRecord;
        }

        //Insert records
        public void InsertRecord(string PlayerName, int Age, string BelongsTo, int Salary)
        {
           
            try
            {
                using (NpgsqlConnection pgsqlConnection = new NpgsqlConnection(connString))
                {
                    // Open the PgSQL Connection.                  
                    pgsqlConnection.Open();

                    string insertCommand = String.Format(
                                                            &amp;quot;Insert Into tblPlayers(playername,age,belongsto,salary) values(&amp;#39;{0}&amp;#39;,{1},&amp;#39;{2}&amp;#39;,{3})&amp;quot;,
                                                            PlayerName,Age,BelongsTo,Salary
                                                        );

                    using (NpgsqlCommand pgsqlcommand = new NpgsqlCommand(insertCommand, pgsqlConnection))
                    {
                        pgsqlcommand.ExecuteNonQuery();
                    }                    
                }
            }
            catch (NpgsqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Update records
        public void UpdateRecord(string PlayerName, int Salary)
        {
            try
            {
                using (NpgsqlConnection pgsqlConnection = new NpgsqlConnection(connString))
                {
                    // Open the PgSQL Connection.                  
                    pgsqlConnection.Open();

                    string updateCommand = String.Format(
                                                            &amp;quot;Update tblPlayers Set salary  = salary + {0} Where playername = &amp;#39;{1}&amp;#39;&amp;quot;,
                                                            Salary, PlayerName
                                                        );

                    using (NpgsqlCommand pgsqlcommand = new NpgsqlCommand(updateCommand, pgsqlConnection))
                    {
                        pgsqlcommand.ExecuteNonQuery();
                    }
                }
            }
            catch (NpgsqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        //Delete records
        public void DeleteRecord(string PlayerName)
        {
            try
            {
                using (NpgsqlConnection pgsqlConnection = new NpgsqlConnection(connString))
                {
                    // Open the PgSQL Connection.                  
                    pgsqlConnection.Open();

                    string insertCommand = String.Format(
                                                            &amp;quot;Delete From tblPlayers Where playername = &amp;#39;{0}&amp;#39;&amp;quot;,
                                                            PlayerName
                                                        );

                    using (NpgsqlCommand pgsqlcommand = new NpgsqlCommand(insertCommand, pgsqlConnection))
                    {
                        pgsqlcommand.ExecuteNonQuery();
                    }
                }
            }
            catch (NpgsqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}

&lt;/pre&gt;
&lt;p&gt;This program is quite easy to understand. First we are making the connection string which is a keyvalue pair combination. The &lt;b&gt;GetAllRecords&lt;/b&gt; is use for fetching the records.We are first establishing the connection to the PostgreSQL by using the NpgsqlConnection class where we are specifying the connection string.Then by using the NpgsqlDataAdapter class, we are filling the datatable and we are returning it back to our presentation layer for binding to the grid&lt;/p&gt;
&lt;p&gt;The &lt;b&gt;InsertRecord,UpdateRecord and DeleteRecord&lt;/b&gt; also follow the same pattern.By the help of ExecuteNonQuery method of NpgsqlCommand class, we are performing the DML operations&lt;/p&gt;
&lt;p&gt;Hope this will be helpful. The zip file for the sample application is attached.&lt;/p&gt;
&lt;p&gt;Thanks for reading&lt;/p&gt;</description></item><item><title>Day 12: Math functions and operators in PostgreSQL (Part 2)</title><link>http://beyondrelational.com/live/blogs/niladribiswas/archive/2011/12/12/day-12-math-functions-and-operators-in-postgresql-part-2.aspx</link><pubDate>Mon, 12 Dec 2011 17:41:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:14350</guid><dc:creator>niladribiswas</dc:creator><description>&lt;p&gt;Today we will look into the famous problem of &amp;quot;Finding the Second highest salary of Employees for each department&amp;quot;&lt;/p&gt;
&lt;p&gt;Well, this question has already been asked in &lt;a href="http://beyondrelational.com/modules/18/plsql-challenges/299/plsql-challenge-2-find-the-second-highest-salary-for-each-department.aspx?tab=info"&gt;PLSQL Challenge 2&lt;/a&gt; and henceforth we are not going to discuss about the introduction of the same here&lt;/p&gt;
&lt;h4&gt;Sample Data&lt;/h4&gt;
&lt;p&gt;&lt;pre class="brush:sql"&gt;
EmployeeID  EmployeeName    Department      Salary   
----------- --------------- --------------- ---------
1           T Cook          Finance         40000.00
2           D Michael       Finance         25000.00
3           A Smith         Finance         25000.00
4           D Adams         Finance         15000.00
5           M Williams      IT              80000.00
6           D Jones         IT              40000.00
7           J Miller        IT              50000.00
8           L Lewis         IT              50000.00
9           A Anderson      Back-Office     25000.00
10          S Martin        Back-Office     15000.00
11          J Garcia        Back-Office     15000.00
12          T Clerk         Back-Office     10000.00
&lt;/pre&gt;&lt;/p&gt;
&lt;h4&gt;Expected Results&lt;/h4&gt;
&lt;p&gt;&lt;pre class="brush:sql"&gt;
EmployeeID  EmployeeName    Department      Salary   
----------- --------------- --------------- ---------
10          S Martin        Back-Office     15000.00
11          J Garcia        Back-Office     15000.00
2           D Michael       Finance         25000.00
3           A Smith         Finance         25000.00
7           J Miller        IT              50000.00
8           L Lewis         IT              50000.00
&lt;/pre&gt;
&lt;/p&gt;

&lt;h4&gt;Sample Script&lt;/h4&gt;
&lt;pre class="brush:sql"&gt;
CREATE TABLE PLC2_Employees (
	EmployeeID Serial ,
	EmployeeName Character Varying(15),
	Department Character Varying(15),
	Salary NUMERIC(16,2)
);


INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;T Cook&amp;#39;,&amp;#39;Finance&amp;#39;, 40000);
INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;D Michael&amp;#39;,&amp;#39;Finance&amp;#39;, 25000);
INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;A Smith&amp;#39;,&amp;#39;Finance&amp;#39;, 25000);
INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;D Adams&amp;#39;,&amp;#39;Finance&amp;#39;, 15000);

INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;M Williams&amp;#39;,&amp;#39;IT&amp;#39;, 80000);
INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;D Jones&amp;#39;,&amp;#39;IT&amp;#39;, 40000);
INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;J Miller&amp;#39;,&amp;#39;IT&amp;#39;, 50000);
INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;L Lewis&amp;#39;,&amp;#39;IT&amp;#39;, 50000);

INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;A Anderson&amp;#39;,&amp;#39;Back-Office&amp;#39;, 25000);
INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;S Martin&amp;#39;,&amp;#39;Back-Office&amp;#39;, 15000);
INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;J Garcia&amp;#39;,&amp;#39;Back-Office&amp;#39;, 15000);
INSERT INTO PLC2_Employees(EmployeeName, Department, Salary) VALUES(&amp;#39;T Clerk&amp;#39;,&amp;#39;Back-Office&amp;#39;, 10000);
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Solution 1: Using Rank Analytical Function&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
With CTE As(
Select 
	 Rank() Over(Partition By Department Order By Salary Desc) &amp;quot;Rn&amp;quot;
	,* 	
From PLC2_Employees)
Select  
	EmployeeID
	,EmployeeName
	,Department
	,Salary
From CTE
Where &amp;quot;Rn&amp;quot; = 2
Order By 
	Department
	,EmployeeID
&lt;/pre&gt;

&lt;p&gt;&lt;b&gt;Solution 2: Using Dense_Rank Analytical Function&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
With CTE As(
Select 
	 Dense_Rank() Over(Partition By Department Order By Salary Desc) &amp;quot;Rn&amp;quot;
	,* 	
From PLC2_Employees)
Select  
	EmployeeID
	,EmployeeName
	,Department
	,Salary
From CTE
Where &amp;quot;Rn&amp;quot; = 2
Order By 
	Department
	,EmployeeID
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Solution 3: Using Co-related subquery&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select	 
	e1.EmployeeID
	,e1.EmployeeName
	,e1.Department
	,e1.Salary
From  
(
	Select 
		MAX(Salary) Salary
		,Department		
	From PLC2_Employees x
	Where (
		Select 
			MAX(Salary) 
		From PLC2_Employees y
		Where x.department = y.department
	     )&amp;gt; Salary 
		
	Group By Department
) e2
Inner Join PLC2_Employees e1
On e1.Department = e2.Department
And e1.Salary = e2.Salary
Order By 
	e1.Department
	, e1.Salary desc
	,e1.EmployeeID
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Solution 4: Using Co-related subquery&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select Distinct *
From PLC2_Employees e1
Where 2 = ( Select Count(Distinct e2.Salary)
            From PLC2_Employees e2
            Where e2.Department = e1.Department
            And e2.Salary &amp;gt;= e1.Salary 
          )
Order By 
	e1.Department
	, e1.Salary desc
	,e1.EmployeeID
&lt;/pre&gt;

&lt;p&gt;Hope this helps&lt;/p&gt;</description></item><item><title>Day 11: Math functions and operators in PostgreSQL (Part 2)</title><link>http://beyondrelational.com/live/blogs/niladribiswas/archive/2011/12/11/day-10-math-function-and-operator-in-postgresql.aspx</link><pubDate>Sun, 11 Dec 2011 12:14:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:13935</guid><dc:creator>niladribiswas</dc:creator><description>&lt;p&gt;Postgre SQL has a rich set of mathametical function and operators. Here is a list of some of the Mathematical functions with their equivalent counter part in SQL Server&lt;/p&gt;
&lt;p&gt;Yesterday we have looked into the &lt;a href="http://beyondrelational.com/blogs/niladribiswas/archive/2011/12/10/day-10-math-functions-and-operators-in-postgresql-part-1.aspx"&gt;Mathematical operators&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;h2&gt;1.ABS&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns absolute value&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Abs(-95.45)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we also have &lt;b&gt;ABS&lt;/b&gt; function which does the same job.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Abs(-95.45)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;95.45&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;N.B.~&lt;/b&gt;In PostgreSql, we also have the &lt;b&gt;Absolute Number Operator(@)&lt;/b&gt; for accomplishing the same work&lt;/p&gt;
&lt;p&gt;&lt;h2&gt;2.CEIL/CEILING&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the smallest integer &amp;amp;gt= the specified numeric expression&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select CEIL(-105.56),CEILING(-105.56)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we have &lt;b&gt;CEILING&lt;/b&gt; function which does the same job.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select CEILING(-105.56)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;-105&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;3.FLOOR&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the largest integer &amp;amp;lt= the specified numeric expression&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select FLOOR(-34.6789)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we equally have &lt;b&gt;FLOOR&lt;/b&gt; function which does the same job.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select FLOOR(-34.6789)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;-35&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;4.CBRT&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the cube root of a number&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select CBRT(64)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we can achieve the same by using the POWER function where we need to apply the formula as &lt;a href="http://msdn.microsoft.com/en-us/library/ms174276.aspx"&gt;POWER(float_expression,1/y)&lt;/a&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;SELECT POWER(64.0,1.00/3.00)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;4&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;N.B.~&lt;/b&gt;In PostgreSql, we also have the &lt;b&gt;Cube Root Operator(||/)&lt;/b&gt; for accomplishing the same work&lt;/p&gt;
&lt;p&gt;&lt;h2&gt;5.DEGREES&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the degrees from radians&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Degrees(19)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we equally have &lt;b&gt;DEGREES&lt;/b&gt; function which does the same job.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Degrees(19)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;1088.61981074856&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;6.RADIANS&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the radians from degrees&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Radians(1088.61981074856)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we equally have &lt;b&gt;RADIANS&lt;/b&gt; function which does the same job.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Radians(1088.61981074856)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;18.999999999999929000&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;7.EXP&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the exponential&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Exp(10)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we equally have &lt;b&gt;EXP&lt;/b&gt; function which does the same job.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Exp(10)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;22026.4657948067&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;8.LN&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the natural logarithm&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select LN(25)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;3.2188758248682&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;9.LOG(numeric)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the base 10 logarithm&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Log(25)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we equally have &lt;b&gt;LOG(numeric)&lt;/b&gt; function which does the same job.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Log(25)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;3.2188758248682&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;10.LOG(b numeric, N numeric)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the base &amp;#39;b&amp;#39; logarithm&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Log(2,25)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we equally have &lt;b&gt;LOG(N numeric,b numeric)&lt;/b&gt; function which does the same job.The systax is a little different in SQL Server. And it is an enhanced overloaded version in &lt;a href="http://beyondrelational.com/blogs/niladribiswas/archive/2011/07/24/enhanced-log-function-in-denali-ctp-3.aspx"&gt;Denali CTP 3&lt;/a&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Log(25,2)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;4.64385618977472&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;11.MOD(a,b)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the remainder of a/b&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select MOD(100,9)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server have the modulas operator(%) for this.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select 100%9&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;1&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;N.B.~&lt;/b&gt;In PostgreSql, we also have the &lt;b&gt;Modulas Operator(%)&lt;/b&gt; for accomplishing the same work&lt;/p&gt;
&lt;p&gt;&lt;h2&gt;12.PI&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the PI constant value&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select PI()&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server have the same &lt;b&gt;PI&lt;/b&gt; function.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select PI()&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;3.14159265358979&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;13.POW(a,b)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the value of a raised to power of b&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select POW(5,4)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server have the &lt;b&gt;POWER&lt;/b&gt; function.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select POWER(5,4)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;625&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;14.RANDOM()&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Generates random value between 0 to 1&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select RANDOM()&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server have the &lt;b&gt;RAND&lt;/b&gt; function.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select RAND()&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Random values between 0 to 1 e.g. 0.451120470373569. It is non deterministic&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;15.SQRT()&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the square root of a number&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select SQRT(4)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server have the same&lt;b&gt;SQRT&lt;/b&gt; function.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select SQRT(4)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;2&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;N.B.~&lt;/b&gt;In PostgreSql, we also have the &lt;b&gt;Square Root Operator(|/)&lt;/b&gt; for accomplishing the same work&lt;/p&gt;
&lt;p&gt;&lt;h2&gt;16.ROUND(a numeric),ROUND(a numeric, b length)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the nearest integer(for for first case).For second systax it rounds of the specified length.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;SELECT ROUND(50.75),ROUND(50.75666666, 3)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server have the same&lt;b&gt;ROUND&lt;/b&gt; function with the syntax as ROUND ( numeric_expression , length [ ,function ] ).&lt;/p&gt;
&lt;pre class="brush:sql"&gt;SELECT ROUND(50.75666666, 0),ROUND(50.75666666, 3, 6)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;51.00000000,50.75600000&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;17.TRUNC(a numeric),TRUNC(a numeric, b length)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Truncates towards zero(for for first case).For second syntax it truncates to the specified decimal places.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select TRUNC(12345.67892345),TRUNC(12345.67892345,4)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server,there is no direct function. But we can simulate the same by using CharIndex and Left function.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
	Select Declare @val varchar(20) = &amp;#39;12345.67892345&amp;#39;
	Select 
	Left(@val, CharIndex(&amp;#39;.&amp;#39;,@val) -1)
	,Left(@val,LEN(@val)- 4)
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;12345,12345.6789&lt;/pre&gt;


&lt;p&gt;&lt;h2&gt;18.SIGN&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the sign of the argument(-1,0,1).&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Sign(-10),Sign(10),Sign(0)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we also have the same &lt;b&gt;SIGN&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Sign(-10),Sign(10),Sign(0)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;-1,1,0&lt;/pre&gt;

&lt;p&gt;&lt;h2&gt;19.Trigonometric functions&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns the trigonometric values.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
		Select
			 SIN(.567) &amp;quot;Sin&amp;quot;,
			 COS(.567) &amp;quot;Cos&amp;quot;,
			 TAN(.567) &amp;quot;Tan&amp;quot;,
			 COT(.567) &amp;quot;Cot&amp;quot;,
			 ACOS(.567) &amp;quot;ACos&amp;quot;,
			 ASIN(.567) &amp;quot;ASin&amp;quot;,
			 ATAN(.567) &amp;quot;ATan&amp;quot;,
			 ATAN2(.567,.987) &amp;quot;ATan2&amp;quot;
		&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
		Select
			 SIN(.567) &amp;quot;Sin&amp;quot;,
			 COS(.567) &amp;quot;Cos&amp;quot;,
			 TAN(.567) &amp;quot;Tan&amp;quot;,
			 COT(.567) &amp;quot;Cot&amp;quot;,
			 ACOS(.567) &amp;quot;ACos&amp;quot;,
			 ASIN(.567) &amp;quot;ASin&amp;quot;,
			 ATAN(.567) &amp;quot;ATan&amp;quot;,
			{fn atan2(.567,.987)} &amp;quot;ATan2&amp;quot;
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Sin			Cos			Tan			Cot			ACos			ASin			ATan			ATan2
0.537103921254637	0.843516080328581	0.636744140129984	1.57048952157748	0.967937080405776	0.60285924638912	0.51580128425173	0.521434439771744
&lt;/pre&gt;
&lt;p&gt;Hope this was useful. Thanks for reading&lt;/p&gt;</description></item><item><title>Day 10: Math functions and operators in PostgreSQL (Part 1)</title><link>http://beyondrelational.com/live/blogs/niladribiswas/archive/2011/12/10/day-10-math-functions-and-operators-in-postgresql-part-1.aspx</link><pubDate>Sun, 11 Dec 2011 03:31:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:13943</guid><dc:creator>niladribiswas</dc:creator><description>&lt;p&gt;Postgre SQL has a rich set of mathametical function and operators. Here is a list of some of the Mathematical operators with their equivalent counter part in SQL Server&lt;/p&gt;
&lt;p&gt;Tomorrow we will look into the Mathematical functions&lt;/p&gt;
&lt;p&gt;&lt;h2&gt;1.Basic Arithmetic Operators&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Does arithmetic operations.&lt;/p&gt;
&lt;p&gt;
&lt;table border="2"&gt;
	&lt;tr&gt;
		&lt;td&gt;&lt;b&gt;DB Name&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Addition Operator&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Subtraction Operator&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Multiplication Operator&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Division Operator&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;ModOperator&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Exponentiation Operator&lt;/b&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;PostgreSQL&lt;/td&gt;
		&lt;td&gt;+&lt;/td&gt;
		&lt;td&gt;-&lt;/td&gt;
		&lt;td&gt;*&lt;/td&gt;
		&lt;td&gt;/&lt;/td&gt;
		&lt;td&gt;%&lt;/td&gt;
		&lt;td&gt;^&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;Sql Server&lt;/td&gt;
		&lt;td&gt;+&lt;/td&gt;
		&lt;td&gt;-&lt;/td&gt;
		&lt;td&gt;*&lt;/td&gt;
		&lt;td&gt;/&lt;/td&gt;
		&lt;td&gt;%&lt;/td&gt;
		&lt;td&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms174276.aspx"&gt;POWER(float_expression , y)&lt;/a&gt;&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select 
	20+10 &amp;quot;Add&amp;quot;,
	20-10 &amp;quot;Subtract&amp;quot;,
	20*10 &amp;quot;Multiply&amp;quot;,
	20/10 &amp;quot;Divide&amp;quot;,
	20%10 &amp;quot;Mod&amp;quot;,
	5^2 &amp;quot;Exponentiation&amp;quot;
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select 
	20+10 &amp;quot;Add&amp;quot;,
	20-10 &amp;quot;Subtract&amp;quot;,
	20*10 &amp;quot;Multiply&amp;quot;,
	20/10 &amp;quot;Divide&amp;quot;,
	20%10 &amp;quot;Mod&amp;quot;,
	Power(5,2) &amp;quot;Exponentiation&amp;quot;
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt; Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Add	Subtract	Multiply	Divide	Mod	Exponentiation
30	10		200		2	0	25
&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;2.Bitwise Operators(And,Or,Not,XOR,Shift Left,Shift Right)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Does bitwise operations.&lt;/p&gt;
&lt;p&gt;
&lt;table border="2"&gt;
	&lt;tr&gt;
		&lt;td&gt;&lt;b&gt;DB Name&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Bitwise And Operator&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Bitwise OR Operator&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Bitwise NOT Operator&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Bitwise XOR Operator&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Bitwise Shift Left Operator&lt;/b&gt;&lt;/td&gt;
		&lt;td&gt;&lt;b&gt;Bitwise Shift Right Operator&lt;/b&gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;PostgreSQL&lt;/td&gt;
		&lt;td&gt;&amp;amp;&lt;/td&gt;
		&lt;td&gt;|&lt;/td&gt;
		&lt;td&gt;~&lt;/td&gt;
		&lt;td&gt;#&lt;/td&gt;
		&lt;td&gt;&amp;lt;&amp;lt;&lt;/td&gt;
		&lt;td&gt;&amp;gt;&amp;gt;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td&gt;Sql Server&lt;/td&gt;
		&lt;td&gt;&amp;amp;&lt;/td&gt;
		&lt;td&gt;|&lt;/td&gt;
		&lt;td&gt;~&lt;/td&gt;
		&lt;td&gt;^&lt;/td&gt;
		&lt;td&gt;Number * &lt;a href="http://msdn.microsoft.com/en-us/library/ms174276.aspx"&gt;POWER(float_expression , y)&lt;/a&gt;&lt;/td&gt;
		&lt;td&gt;Number / &lt;a href="http://msdn.microsoft.com/en-us/library/ms174276.aspx"&gt;POWER(float_expression , y)&lt;/a&gt;&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select 
	20&amp;amp;10 &amp;quot;Bitwise And&amp;quot;,
	20|10 &amp;quot;Bitwise OR&amp;quot;,
	~20 &amp;quot;Bitwise NOT&amp;quot;,
	20#10 &amp;quot;Bitwise XOR&amp;quot;,
	20&amp;lt;&amp;lt;10 &amp;quot;Bitwise Left&amp;quot;,
	33&amp;gt;&amp;gt;4 &amp;quot;Bitwise Right&amp;quot;
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select 
	20&amp;amp;10 &amp;quot;Bitwise And&amp;quot;,
	20|10 &amp;quot;Bitwise OR&amp;quot;,
	~20 &amp;quot;Bitwise NOT&amp;quot;,
	20^10 &amp;quot;Bitwise XOR&amp;quot;,
	5 * POWER(2, 4) &amp;quot;Bitwise Left&amp;quot;,
	33 / POWER(2, 4) &amp;quot;Bitwise Right&amp;quot;
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt; Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Bitwise And	Bitwise OR	Bitwise NOT	Bitwise XOR	Bitwise Left	Bitwise Right
0		30		-21		30		80		2
&lt;/pre&gt;
&lt;p&gt;&lt;h2&gt;3. Square Root Operator(|/)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns Square Root of a number.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select  |/ 16 &amp;quot;SquareRoot of 16&amp;quot;
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we have &lt;b&gt;SQRT&lt;/b&gt; function for this&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select SQRT(16) &amp;quot;SquareRoot of 16&amp;quot;&lt;/pre&gt;
&lt;p&gt;&lt;b&gt; Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
SquareRoot of 16
----------------
4
&lt;/pre&gt;

&lt;p&gt;&lt;h2&gt;4. Cube Root Operator(||/)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns Cube Root of a number.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select  ||/ 16 &amp;quot;CubeRoot of 16&amp;quot;
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we can achieve the same by using the POWER function where we need to apply the formula as &lt;a href="http://msdn.microsoft.com/en-us/library/ms174276.aspx"&gt;POWER(float_expression,1/y)&lt;/a&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;SELECT POWER(16.0,1.00/3.00) &amp;quot;CubeRoot of 16&amp;quot;&lt;/pre&gt;
&lt;p&gt;&lt;b&gt; Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
CubeRoot of 16
----------------
2.5
&lt;/pre&gt;

&lt;p&gt;&lt;h2&gt;5. Factorial Operator(!)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns factorial of a number.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;Number !&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select 100 !
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;We need to write our own function&lt;/p&gt;
&lt;p&gt;&lt;b&gt; Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
&lt;/pre&gt;

&lt;p&gt;&lt;h2&gt;6.Factorial Prefix Operator(!!)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns factorial of a number.It is the factorial prefix operator.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;!! Number&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select !! 100
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;We need to write our own function&lt;/p&gt;
&lt;p&gt;&lt;b&gt; Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
&lt;/pre&gt;

&lt;p&gt;&lt;h2&gt;7.Absolute Number Operator(@)&lt;/h2&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt;Returns absolute of a number&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;@ Number&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Example in PostgreSQL&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select @ -10
&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Example in Sql Server&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;In Sql Server we have &lt;b&gt;ABS&lt;/b&gt; function for this&lt;/p&gt;
&lt;pre class="brush:sql"&gt;Select Abs(-10)&lt;/pre&gt;
&lt;p&gt;&lt;b&gt;Result&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
10
&lt;/pre&gt;
&lt;p&gt;Hope this was useful. Thanks for reading&lt;/p&gt;</description></item><item><title>Day 9: Array_To_String function in PostgreSQL</title><link>http://beyondrelational.com/live/blogs/niladribiswas/archive/2011/12/09/day-9-array-to-string-function-in-postgresql.aspx</link><pubDate>Sat, 10 Dec 2011 04:54:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:13846</guid><dc:creator>niladribiswas</dc:creator><description>&lt;p&gt;Array_To_String function in PostgreSQL&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt; This functions concatenates  an array element to a string provided the join string is supplied. It returns a string. It is exact opposite of &lt;a href="http://beyondrelational.com/blogs/niladribiswas/archive/2011/12/10/day-8-string-to-array-function-in-postgresql.aspx"&gt;String_To_Array&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;Array_To_String(Array,join string)&lt;/p&gt;
&lt;p&gt;Simple Example&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select Array_To_String(ARRAY[&amp;#39;Hello&amp;#39;,&amp;#39;World&amp;#39;,&amp;#39;Today&amp;#39;,&amp;#39;I&amp;#39;,&amp;#39;am&amp;#39;,&amp;#39;learning&amp;#39;,&amp;#39;Array_To_String&amp;#39;,&amp;#39;It&amp;#39;,&amp;#39;is&amp;#39;,&amp;#39;very&amp;#39;,&amp;#39;cool&amp;#39;], &amp;#39; &amp;#39;)
/* Result */
array_to_string
----------------
&amp;quot;Hello World Today I am learning Array_To_String It is very cool&amp;quot;
&lt;/pre&gt;
&lt;p&gt;The array elements has been concatenated by our supplied join string blank space.&lt;/p&gt;
&lt;p&gt;Example 2: Using Array_To_String on a column&lt;/p&gt;
&lt;p&gt;Let us create the environment&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
CREATE TABLE tblTest(ID Serial NOT NULL, Col1 TEXT,Col2 TEXT,Col3 TEXT NULL, Col4 TEXT  NULL, Col5 TEXT  NULL);
INSERT INTO tblTest(Col1,Col2,Col3,Col4,Col5) VALUES(&amp;#39;Hello&amp;#39;,&amp;#39;World&amp;#39;,Null,Null,Null);
INSERT INTO tblTest(Col1,Col2,Col3,Col4,Col5) VALUES(&amp;#39;Today&amp;#39;,&amp;#39;I&amp;#39;,&amp;#39;am &amp;#39;,&amp;#39;learning&amp;#39;, &amp;#39;String_To_Array function&amp;#39;); 

SELECT * FROM tblTest;

/*Result*/
&amp;quot;id&amp;quot;;&amp;quot;col1&amp;quot;;&amp;quot;col2&amp;quot;;&amp;quot;col3&amp;quot;;&amp;quot;col4&amp;quot;;&amp;quot;col5&amp;quot;
1;&amp;quot;Hello&amp;quot;;&amp;quot;World&amp;quot;;&amp;quot;&amp;quot;;&amp;quot;&amp;quot;;&amp;quot;&amp;quot;
2;&amp;quot;Today&amp;quot;;&amp;quot;I&amp;quot;;&amp;quot;am &amp;quot;;&amp;quot;learning&amp;quot;;&amp;quot;String_To_Array function&amp;quot;
&lt;/pre&gt;
&lt;p&gt;The objective is to combine the Col1,Col2,Col3,Col4,Col5 column data by the string &amp;#39;~~&amp;#39;. Let us see how&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
SELECT ID,
	Array_To_String(
				ARRAY[Col1,Col2,Col3,Col4,Col5],&amp;#39;~~&amp;#39;
			)
FROM tblTest;

/* Result*/
id array_to_string
1  Hello~~World
2  Today~~I~~am ~~learning~~String_To_Array function
&lt;/pre&gt;
&lt;p&gt;Hope this helps.&lt;/p&gt;
&lt;p&gt;Thanks for reading&lt;/p&gt;</description></item><item><title>Day 8: String_To_Array function in PostgreSQL</title><link>http://beyondrelational.com/live/blogs/niladribiswas/archive/2011/12/08/day-8-string-to-array-function-in-postgresql.aspx</link><pubDate>Fri, 09 Dec 2011 04:39:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:13845</guid><dc:creator>niladribiswas</dc:creator><description>&lt;p&gt;String_To_Array function in PostgreSQL&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Purpose:&lt;/b&gt; This functions splits a string to an array provided the delimeter is supplied. It returns an array.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt;String_To_Array(String,delimeter)&lt;/p&gt;
&lt;p&gt;Simple Example&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select String_To_Array(&amp;#39;Hello World Today I am learning String_To_Array It is very cool&amp;#39;,&amp;#39; &amp;#39;)

/* Result */
string_to_array
----------------
{Hello,World,Today,I,am,learning,String_To_Array,It,is,very,cool}
&lt;/pre&gt;
&lt;p&gt;We can figure out that we are splitting the string by blank space which is our delimeter here and the function yielded an array.Every element of the array are demarcated by comma(,).&lt;/p&gt;
&lt;p&gt;We can obtain a table of rows if we apply the &lt;a href="http://beyondrelational.com/blogs/niladribiswas/archive/2011/12/09/day-7-unpivot-using-unnest-function-in-postgresql.aspx"&gt;UnNest &lt;/a&gt; function to the output of String_To_Array function&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select UNNEST(String_To_Array(&amp;#39;Hello World Today I am learning String_To_Array It is very cool&amp;#39;,&amp;#39; &amp;#39;))

/*Result*/
unnest
--------
&amp;quot;Hello&amp;quot;
&amp;quot;World&amp;quot;
&amp;quot;Today&amp;quot;
&amp;quot;I&amp;quot;
&amp;quot;am&amp;quot;
&amp;quot;learning&amp;quot;
&amp;quot;String_To_Array&amp;quot;
&amp;quot;It&amp;quot;
&amp;quot;is&amp;quot;
&amp;quot;very&amp;quot;
&amp;quot;cool&amp;quot;
&lt;/pre&gt;
&lt;p&gt;Example 2: Using String_To_Array on a column&lt;/p&gt;
&lt;p&gt;Let us create the environment&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
CREATE TABLE tblTest( ID Serial NOT NULL, Records TEXT);
INSERT INTO tblTest(Records) VALUES(&amp;#39;Hello,World&amp;#39;);
INSERT INTO tblTest(Records) VALUES(&amp;#39;Today,I,am ,learning, String_To_Array function&amp;#39;); 

SELECT * FROM tblTest;

/*Result*/
&amp;quot;id&amp;quot;;&amp;quot;records&amp;quot;
1;&amp;quot;Hello,World&amp;quot;
2;&amp;quot;Today,I,am ,learning, String_To_Array function&amp;quot;
&lt;/pre&gt;

&lt;p&gt;The objective is to split the Records column data. Let us see how&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select 
	ID
	,UnNest(String_To_Array(Records,&amp;#39;,&amp;#39;)) AS &amp;quot;Splitted Record&amp;quot;
From tblTest;

/* Result*/
id Splitted Record
1  Hello
1  World
2  Today
2  I
2  am 
2  learning
2  String_To_Array function
&lt;/pre&gt;

&lt;p&gt;We can easily write a split function to do this in SQL Server&lt;/p&gt;
&lt;p&gt;Hope this helps.&lt;/p&gt;
&lt;p&gt;Thanks for reading&lt;/p&gt;


</description></item><item><title>Day 7: Unpivot using UNNEST function in PostgreSQL</title><link>http://beyondrelational.com/live/blogs/niladribiswas/archive/2011/12/07/day-7-unpivot-using-unnest-function-in-postgresql.aspx</link><pubDate>Thu, 08 Dec 2011 03:52:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:13844</guid><dc:creator>niladribiswas</dc:creator><description>&lt;p&gt;UnNest function in PostgreSQL&lt;/p&gt;
&lt;p&gt;In PostgreSQL, we can perform UnPivoting using the UnNest function. It accepts an array and expands the array items in set of rows.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Syntax:&lt;/b&gt; UNNEST(Some array) &lt;/p&gt;
&lt;p&gt;Example&lt;/p&gt;
&lt;p&gt;Let us consider the below script&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Create Table tblUnPivotExample(ItemNo INT, TotalAmt INT,Item1 INT,Item2 INT,Item3 INT,Item4 INT);
Insert Into tblUnPivotExample(ItemNo,TotalAmt,Item1,Item2,Item3,Item4) Values(101,100,10,11,12,14);
Insert Into tblUnPivotExample(ItemNo,TotalAmt,Item1,Item2,Item3,Item4) Values(202,200,20,21,22,24);
Insert Into tblUnPivotExample(ItemNo,TotalAmt,Item1,Item2,Item3,Item4) Values(303,300,30,31,32,34);
Insert Into tblUnPivotExample(ItemNo,TotalAmt,Item1,Item2,Item3,Item4) Values(404,400,40,41,42,44);
Insert Into tblUnPivotExample(ItemNo,TotalAmt,Item1,Item2,Item3,Item4) Values(505,500,50,51,52,54);
&lt;/pre&gt;
&lt;p&gt;We have created a table named as &lt;i&gt;tblUnPivotExample&lt;/i&gt;. Projecting the records gives the below output&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select * from tblUnPivotExample;

/*Result*/

ItemNo	TotalAmt	Item1	Item2	Item3	Item4
101	100		10	11	12	14
202	200		20	21	22	24
303	300		30	31	32	34
404	400		40	41	42	44
505	500		50	51	52	54
&lt;/pre&gt;
&lt;p&gt;Now using the UnNest function, we can do the UnPivoting as under&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
SELECT 
	ItemNo,
	UNNEST(ARRAY[&amp;#39;Item1&amp;#39;,&amp;#39;Item2&amp;#39;,&amp;#39;Item3&amp;#39;,&amp;#39;Item4&amp;#39;]) AS ItemName, 
	UNNEST(ARRAY[Item1,Item2,Item3,Item4]) AS Amount 
FROM tblUnPivotExample
ORDER BY ItemNo; 

/*Result*/
ItemNo	ItemName	Amount
101	Item1		10
101	Item2		11
101	Item3		12
101	Item4		14
202	Item1		20
202	Item2		21
202	Item3		22
202	Item4		24
303	Item1		30
303	Item2		31
303	Item3		32
303	Item4		34
404	Item1		40
404	Item2		41
404	Item3		42
404	Item4		44
505	Item1		50
505	Item2		51
505	Item3		52
505	Item4		54
&lt;/pre&gt;
&lt;p&gt;ARRAY[Item1,Item2,Item3,Item4] =&amp;gt; Returns an array object with the values of the elements Item1,Item2,Item3 and Item4&lt;/p&gt;
&lt;p&gt;UNNEST(ARRAY[Item1,Item2,Item3,Item4]) =&amp;gt; Breaks the array to a set of rows&lt;/p&gt;
&lt;p&gt;To keep it simple, if we perform &lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select UnNest(Array[&amp;#39;a&amp;#39;,&amp;#39;b&amp;#39;,&amp;#39;c&amp;#39;,&amp;#39;d&amp;#39;])
&lt;/pre&gt;
&lt;p&gt;We will receive&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
&amp;quot;unnest&amp;quot;
&amp;quot;a&amp;quot;
&amp;quot;b&amp;quot;
&amp;quot;c&amp;quot;
&amp;quot;d&amp;quot;
&lt;/pre&gt;
&lt;p&gt;We can however, write the query as below also&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
SELECT 
	ItemNo
	,&amp;#39;Item1&amp;#39; AS ItemName
	, Item1 AS Amount
FROM tblUnPivotExample

 UNION ALL 

 SELECT 
	ItemNo
	,&amp;#39;Item2&amp;#39; AS ItemName
	, Item2 AS Amount
FROM tblUnPivotExample

UNION ALL 

 SELECT 
	ItemNo
	,&amp;#39;Item3&amp;#39; AS ItemName
	, Item3 AS Amount
FROM tblUnPivotExample

UNION ALL 

 SELECT 
	ItemNo
	,&amp;#39;Item4&amp;#39; AS ItemName
	, Item4 AS Amount
FROM tblUnPivotExample

ORDER BY ItemNo; 
&lt;/pre&gt;
&lt;p&gt;for obtaining the same result.This query will perform 4 runs on different subqueries on the table tblUnPivotExample one for every column we want to unpivot and yields each record from each of the subqueries in a single table. This is very inefficient as it will perform a table scan &amp;#39;N&amp;#39; number of times for every column we want to unpivot.&lt;/p&gt;
&lt;p&gt;Where as, if we use the UnNest function on array, it scans the table only once&lt;/p&gt;
&lt;p&gt;In Sql Server, we do UnPivoting by using UnPivot built in Command which is there since Sql Server 2005.e.g.&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select
		ItemNo
		,ItemName
		,Amount
		From tblUnPivotExample
		UnPivot
		(
			Amount 
			For ItemName IN (Item1,Item2,Item3,Item4)
		)x;
&lt;/pre&gt;
&lt;p&gt;We can however, do the same by using the UNION ALL also&lt;/p&gt;
&lt;p&gt;Hope this helps&lt;/p&gt;
&lt;p&gt;Thanks for reading&lt;/p&gt;






</description></item><item><title>Day 6: Common Table Expressions using WITH clause in PostgreSQL</title><link>http://beyondrelational.com/live/blogs/niladribiswas/archive/2011/12/06/day-6-common-table-expressions-using-with-clause-in-postgresql.aspx</link><pubDate>Wed, 07 Dec 2011 04:26:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:13825</guid><dc:creator>niladribiswas</dc:creator><description>&lt;p&gt;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 &lt;b&gt;WITH&lt;/b&gt; clause for writing CTE based queries.It helps in breaking down complicated and large queries into simpler forms which is easily readable.&lt;/p&gt;
&lt;p&gt;Let us see how we can do so&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Simple CTE&lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
With CTE AS
(	
  Select 
	empid
	, empname
	, salary
	, belongsto
	, deptid
  FROM tblemployee

)
Select * From CTE;
&lt;/pre&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;This is a simple CTE where basically we are projecting the employee table records&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Multiple CTE or Chain CTE &lt;/b&gt;&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
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;
&lt;/pre&gt;
&lt;p&gt;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&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Recursive CTE or Hierarchial queries&lt;/b&gt;&lt;/p&gt;
&lt;p&gt;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 &lt;b&gt;&lt;i&gt;Recursive&lt;/i&gt;&lt;/b&gt; keyword in the CTE which ensures that the query can refer to its own output&lt;/p&gt;

&lt;p&gt;In the next example we will look into how we can generate a number table by the help of Recursive CTE&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
With Recursive CTE(Rn) AS
(	
	Select 1
	Union All
	Select Rn + 1 From CTE
	Where Rn &amp;lt; 10
)
Select * From CTE;
&lt;/pre&gt;

&lt;p&gt;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 &amp;quot;Recursive&amp;quot; keyword. We will write the same program for generating a number table using recursive CTE using SQL Serevr CTE syntax below&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
;With CTE AS
(	
	Select Rn = 1
	Union All
	Select Rn + 1 From CTE
	Where Rn &amp;lt; 10
)
Select * From CTE;
&lt;/pre&gt;
&lt;p&gt;Reference(s)&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;&lt;a href="http://www.postgresql.org/docs/8.4/static/queries-with.html"&gt;WITH Queries (Common Table Expressions)&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://wiki.postgresql.org/wiki/CTEReadme"&gt;CTEReadme&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Hope this helps&lt;/p&gt;
&lt;p&gt;Thanks for reading&lt;/p&gt;</description></item><item><title>Day 5: Generate Identity Column in PostgreSQL by using Serial Datatype</title><link>http://beyondrelational.com/live/blogs/niladribiswas/archive/2011/12/05/day-5-generate-identity-column-in-postgresql.aspx</link><pubDate>Tue, 06 Dec 2011 03:39:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:13824</guid><dc:creator>niladribiswas</dc:creator><description>&lt;p&gt;In PostgreSQL we can use the &lt;b&gt;Serial&lt;/b&gt; datatype for generating the identity column&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
CREATE TABLE tblIndentity
(
  EmpID Serial NOT NULL,
  EmpName Character Varying(20)
);

NOTICE:  CREATE TABLE will create implicit sequence &amp;quot;tblindentity_empid_seq&amp;quot; for serial column &amp;quot;tblindentity.empid&amp;quot;

Query returned successfully with no result in 12 ms.
&lt;/pre&gt;
&lt;p&gt;Polulate some data into the table&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Insert Into tblIndentity(EmpName) Values(&amp;#39;Name1&amp;#39;);
Insert Into tblIndentity(EmpName) Values(&amp;#39;Name2&amp;#39;);
Insert Into tblIndentity(EmpName) Values(&amp;#39;Name3&amp;#39;);
Insert Into tblIndentity(EmpName) Values(&amp;#39;Name4&amp;#39;);
Insert Into tblIndentity(EmpName) Values(&amp;#39;Name5&amp;#39;);
&lt;/pre&gt;

&lt;p&gt;Project the records&lt;/p&gt;
&lt;pre class="brush:sql"&gt;
Select * 
From tblIndentity

--Result
Empid	Empname
-----	--------
1	Name1
2	Name2
3	Name3
4	Name4
5	Name5

&lt;/pre&gt;
&lt;p&gt;In Sql Server we use the &lt;b&gt;Identity &lt;/b&gt; for doing so.&lt;/p&gt;
&lt;p&gt;N.B.~ From Denali CTP1, we can even use &lt;b&gt;Sequence&lt;/b&gt; for generating identity values&lt;/p&gt;
&lt;p&gt;Hope this is useful&lt;/p&gt;
&lt;p&gt;Thanks for reading&lt;/p&gt;</description></item></channel></rss>