<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>custom sorting. &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/custom-sorting/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Mon, 20 Nov 2017 05:56:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>PostgreSQL Dynamic SQL</title>
		<link>https://www.dbrnd.com/2015/05/postgresql-dynamic-sql/</link>
					<comments>https://www.dbrnd.com/2015/05/postgresql-dynamic-sql/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 27 May 2015 17:14:33 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[custom sorting.]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[dynamic sql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[query. custom filter]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=203</guid>

					<description><![CDATA[In this post, I am sharing a demonstration on the Dynamic SQL Query of PostgreSQL. Many times custom filters and sorting requires for selecting customized data. Generally, what we are doing is, we call a stored procedure using master filters and display results on grid or report, but user requires a more custom filter on [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fpostgresql-dynamic-sql%2F&amp;linkname=PostgreSQL%20Dynamic%20SQL" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fpostgresql-dynamic-sql%2F&amp;linkname=PostgreSQL%20Dynamic%20SQL" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fpostgresql-dynamic-sql%2F&amp;linkname=PostgreSQL%20Dynamic%20SQL" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fpostgresql-dynamic-sql%2F&amp;linkname=PostgreSQL%20Dynamic%20SQL" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fpostgresql-dynamic-sql%2F&amp;linkname=PostgreSQL%20Dynamic%20SQL" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fpostgresql-dynamic-sql%2F&amp;linkname=PostgreSQL%20Dynamic%20SQL" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F05%2Fpostgresql-dynamic-sql%2F&#038;title=PostgreSQL%20Dynamic%20SQL" data-a2a-url="https://www.dbrnd.com/2015/05/postgresql-dynamic-sql/" data-a2a-title="PostgreSQL Dynamic SQL"></a></p><p>In this post, I am sharing a demonstration on the Dynamic SQL Query of PostgreSQL.</p>
<p>Many times custom filters and sorting requires for selecting customized data.</p>
<blockquote><p>Generally, what we are doing is, we call a stored procedure using master filters and display results on grid or report, but user requires a more custom filter on returned data.</p>
<p>You can also say like the user needs particular column filtering. Now, this column filter called as custom filtering and which we can achieve by dynamic SQL.<br />
The user can also demand custom sorting, for this, we have to construct SQL using custom sorting column.</p></blockquote>
<p>Dynamic SQL means, you can prepare or build your SQL statement basis on different parameters or results.</p>
<p>Below is a demonstration of PostgreSQL dynamic query using a custom filter.</p>
<p><b>First create one sample Employee table using 10 test records. </b></p>
<pre class="crayon-plain-tag">CREATE TABLE Employee
(
 EmpID SERIAL
 ,EmpName CHARACTER VARYING(50)
 ,Gender CHAR(1)
 ,AGE SMALLINT
);

INSERT INTO Employee 
(
 EmpName 
 ,Gender 
 ,AGE 
)
VALUES 
 ('Anvesh','M',27)
 ,('Mohan','M',30)
 ,('Roy','M',31)
 ,('Meera','F',27)
 ,('Richa','F',26)
 ,('Martin','M',35)
 ,('Mahesh','M',38)
 ,('Paresh','M',22)
 ,('Alina','F',21)
 ,('Alex','M',24);</pre>
<p><b>Below is a sample stored procedure: </b></p>
<pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION fn_GetEmployeeDataByCustomFilter
(
 Paging_PageSize INTEGER = NULL
 ,Paging_PageNumber INTEGER = NULL   
 ,Paging_ColumnFilterExpression TEXT = NULL
 ,Paging_SortExpression TEXT = NULL
)

RETURNS TABLE 
( 
 EmpID INTEGER
 ,EmpName CHARACTER VARYING
 ,Gender CHAR(1)
 ,Age SMALLINT
) AS
$BODY$
DECLARE 

         PageNumber BIGINT;
         SQL TEXT; 
         Paging_PageFilterExpression VARCHAR;  
BEGIN 
 /****************************************************************
 Construct Where expression for Custom Paging 
 **************************************************************** */
 IF Paging_ColumnFilterExpression IS NULL THEN
  Paging_ColumnFilterExpression := ' ';
 ELSE
  Paging_ColumnFilterExpression := ' AND ' || Paging_ColumnFilterExpression;
 END IF; 
 /****************************************************************
 Construct Sort expression for Custom Paging 
 **************************************************************** */
 IF paging_sortexpression IS NULL THEN
  paging_sortexpression := 'EmpName'; 
 END IF;  
 /****************************************************************
 Construct Custom paging parameter...
 **************************************************************** */
 IF (paging_pagesize IS NOT NULL AND paging_pagenumber IS NOT NULL) THEN
  PageNumber := (Paging_PageSize * (Paging_PageNumber-1)); 
 END IF;    
 /* ***************************************************************
 Custome paging SQL Query construction.......
 **************************************************************** */
 SQL :=
 '
 WITH cteCustomFilter AS
 (
  SELECT
    EmpID
    ,EmpName
    ,Gender
    ,Age
  FROM public.Employee
  WHERE 1=1 '||Paging_ColumnFilterExpression||'
  ORDER BY '||paging_sortexpression||' 
 )
 SELECT 
   EmpID
   ,EmpName
   ,Gender
   ,Age
 FROM cteCustomFilter ';

 

 IF (paging_pagesize IS NOT NULL AND paging_pagenumber IS NOT NULL) THEN  
  SQL:= SQL || 'LIMIT '||Paging_PageSize||' OFFSET '||PageNumber;
 END IF; 

 RETURN QUERY EXECUTE SQL;
  
 EXCEPTION WHEN OTHERS THEN 
 RAISE;
END;
$BODY$
LANGUAGE 'plpgsql';</pre>
<p><b>Show some results:</b></p>
<pre class="crayon-plain-tag">SELECT *FROM public.fn_GetEmployeeDataByCustomFilter(3,1,'empname=''Mahesh''',NULL)</pre>
<a href="https://www.dbrnd.com/wp-content/uploads/2015/05/FilterResult1.png"><img decoding="async" class="aligncenter wp-image-197 size-full" title="PostgreSQL Custom Filter" src="https://www.dbrnd.com/wp-content/uploads/2015/05/FilterResult1.png" alt="FilterResult1" width="314" height="125" srcset="https://www.dbrnd.com/wp-content/uploads/2015/05/FilterResult1.png 314w, https://www.dbrnd.com/wp-content/uploads/2015/05/FilterResult1-300x119.png 300w, https://www.dbrnd.com/wp-content/uploads/2015/05/FilterResult1-150x60.png 150w" sizes="(max-width: 314px) 100vw, 314px" /></a></p>
<p>In the above call, I filtered only  &#8220;Mahesh&#8221; so it returned only one record.</p>
<p>The application is responsible to create this custom parameter for a custom filter. You just need to append filter or sorting string with main SQL Query.</p>
<p>This stored procedure has four parameters first two for custom paging and last two for custom filter and sorting.</p>
<p>Please visit this URL: to understand custom paging.</p>
<p><b><a href="https://www.dbrnd.com/2015/05/postgresql-paging-query-limit-offset/">PostgreSQL Custom Paging using LIMIT and OFFSET</a></b></p>
<p>Now, about the dynamic query in which I am passing filter column name through input parameter.</p>
<p>This filter and sorting parameter are required to concat with the main SQL query for customized results.</p>
<p>input parameter: Paging_ColumnFilterExpression is for custom filter and Paging_SortExpression is for custom sorting.</p>
<p>You should also set some default value for this kind of variables or parameters.</p>
<p><b>Now examine second sample:</b></p>
<pre class="crayon-plain-tag">SELECT *FROM public.fn_GetEmployeeDataByCustomFilter(5,1,'empname like ''%M%''','age desc')</pre>
<a href="https://www.dbrnd.com/wp-content/uploads/2015/05/FilterResult2.png"><img fetchpriority="high" decoding="async" class="aligncenter wp-image-198 size-full" title="PostgreSQL Custom Filter" src="https://www.dbrnd.com/wp-content/uploads/2015/05/FilterResult2.png" alt="FilterResult2" width="313" height="167" srcset="https://www.dbrnd.com/wp-content/uploads/2015/05/FilterResult2.png 313w, https://www.dbrnd.com/wp-content/uploads/2015/05/FilterResult2-300x160.png 300w" sizes="(max-width: 313px) 100vw, 313px" /></a></p>
<p>You can see both filters and sort in this result.</p>
<p>We have sort age column by descending and select employee starting with &#8216;M&#8217;.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/05/postgresql-dynamic-sql/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
