<?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>Pivot &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/pivot/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>Tue, 21 Nov 2017 06:17:19 +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>SQL Puzzle: SQL Advance Query &#8211; Do PIVOT &#8211; UNPIVOT and Sort the result</title>
		<link>https://www.dbrnd.com/2017/11/sql-puzzle-sql-advance-query-do-pivot-unpivot-and-sort-the-result/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 15 Nov 2017 16:16:19 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Pivot]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[unpivot]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5922</guid>

					<description><![CDATA[Check the below input data and expected output to sort data vertically and horizontally using PIVOT and UNPIVOT Input Data: [crayon-6ab3d7f39e8e1527986897/] Expected Output: [crayon-6ab3d7f39e8e5533331300/] Create a table with sample data: [crayon-6ab3d7f39e8e7112118220/] Solution: [crayon-6ab3d7f39e8e8862669070/] Please try the different solution for this puzzle and share it via comment...]]></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%2F2017%2F11%2Fsql-puzzle-sql-advance-query-do-pivot-unpivot-and-sort-the-result%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20PIVOT%20%E2%80%93%20UNPIVOT%20and%20Sort%20the%20result" 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%2F2017%2F11%2Fsql-puzzle-sql-advance-query-do-pivot-unpivot-and-sort-the-result%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20PIVOT%20%E2%80%93%20UNPIVOT%20and%20Sort%20the%20result" 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%2F2017%2F11%2Fsql-puzzle-sql-advance-query-do-pivot-unpivot-and-sort-the-result%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20PIVOT%20%E2%80%93%20UNPIVOT%20and%20Sort%20the%20result" 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%2F2017%2F11%2Fsql-puzzle-sql-advance-query-do-pivot-unpivot-and-sort-the-result%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20PIVOT%20%E2%80%93%20UNPIVOT%20and%20Sort%20the%20result" 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%2F2017%2F11%2Fsql-puzzle-sql-advance-query-do-pivot-unpivot-and-sort-the-result%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20PIVOT%20%E2%80%93%20UNPIVOT%20and%20Sort%20the%20result" 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%2F2017%2F11%2Fsql-puzzle-sql-advance-query-do-pivot-unpivot-and-sort-the-result%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20PIVOT%20%E2%80%93%20UNPIVOT%20and%20Sort%20the%20result" 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%2F2017%2F11%2Fsql-puzzle-sql-advance-query-do-pivot-unpivot-and-sort-the-result%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Do%20PIVOT%20%E2%80%93%20UNPIVOT%20and%20Sort%20the%20result" data-a2a-url="https://www.dbrnd.com/2017/11/sql-puzzle-sql-advance-query-do-pivot-unpivot-and-sort-the-result/" data-a2a-title="SQL Puzzle: SQL Advance Query – Do PIVOT – UNPIVOT and Sort the result"></a></p><p>Check the below input data and expected output to sort data vertically and horizontally using PIVOT and UNPIVOT</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">col1 col2 col3
---- ---- ----
2    1    3
3    2    1
Z    X    Y
B    C    D
Y    Z    X
B    C    A</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">col1 col2 col3
---- ---- ----
1    2    3
A    B    C
B    C    D
X    Y    Z</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE PivotSort
(
	col1 CHAR(1)
	,col2 CHAR(1)
	,col3 CHAR(1)
)

INSERT INTO PivotSort (col1, col2, col3) 
VALUES 
('2','1','3'),('3','2','1'),('Z','X','Y')
,('B','C','D'),('Y','Z','X'),('B','C','A')
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH
unpivotted AS 
(
	SELECT *
	FROM (
		SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS rownum, * 
		FROM PivotSort
	) t UNPIVOT (vals FOR col IN (col1,col2,col3)) p
)
, 
ordered_cols AS 
(
	SELECT     
		rownum, vals, 
		ROW_NUMBER() OVER (PARTITION BY rownum ORDER BY vals) AS colnum
	FROM unpivotted
)
SELECT DISTINCT [1] [col1], [2] [col2], [3] [col3]
FROM ordered_cols
PIVOT
	(MAX(vals) FOR colnum IN ([1],[2],[3])) p
ORDER BY [1], [2], [3]</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Puzzle: SQL Advance Query &#8211; Display Year and Month data With or Without PIVOT</title>
		<link>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot/</link>
					<comments>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 30 Oct 2017 16:16:16 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Pivot]]></category>
		<category><![CDATA[SQL Advance Query]]></category>
		<category><![CDATA[SQL Interview]]></category>
		<category><![CDATA[SQL Problem]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5893</guid>

					<description><![CDATA[Check the below input data and expected output to display data basis on year and month. This is a puzzle of PIVOT like form the rows into columns. Input Data: [crayon-6ab3d7f39ebcc377604469/] Expected Output: [crayon-6ab3d7f39ebce316744382/] Create a table with sample data: [crayon-6ab3d7f39ebcf110720208/] Solution 1: Without using PIVOT [crayon-6ab3d7f39ebd0878380733/] Solution 2: Using PIVOT [crayon-6ab3d7f39ebd1836763084/] Please try the [&#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%2F2017%2F10%2Fsql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Display%20Year%20and%20Month%20data%20With%20or%20Without%20PIVOT" 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%2F2017%2F10%2Fsql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Display%20Year%20and%20Month%20data%20With%20or%20Without%20PIVOT" 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%2F2017%2F10%2Fsql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Display%20Year%20and%20Month%20data%20With%20or%20Without%20PIVOT" 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%2F2017%2F10%2Fsql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Display%20Year%20and%20Month%20data%20With%20or%20Without%20PIVOT" 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%2F2017%2F10%2Fsql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Display%20Year%20and%20Month%20data%20With%20or%20Without%20PIVOT" 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%2F2017%2F10%2Fsql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot%2F&amp;linkname=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Display%20Year%20and%20Month%20data%20With%20or%20Without%20PIVOT" 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%2F2017%2F10%2Fsql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot%2F&#038;title=SQL%20Puzzle%3A%20SQL%20Advance%20Query%20%E2%80%93%20Display%20Year%20and%20Month%20data%20With%20or%20Without%20PIVOT" data-a2a-url="https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot/" data-a2a-title="SQL Puzzle: SQL Advance Query – Display Year and Month data With or Without PIVOT"></a></p><p>Check the below input data and expected output to display data basis on year and month. This is a puzzle of PIVOT like form the rows into columns.</p>
<p><strong>Input Data:</strong></p>
<pre class="crayon-plain-tag">YearData    MonthData Amount
----------- --------- -----------
2016        Jan       10000
2016        Feb       15000
2016        Mar       13000
2017        Jan       17000</pre> </p>
<p><strong>Expected Output:</strong></p>
<pre class="crayon-plain-tag">YearData    Jan         Feb         Mar
----------- ----------- ----------- -----------
2016        10000       15000       13000
2017        17000       NULL        NULL</pre> </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE YearMonth
(
	 YearData INT
	,MonthData CHAR(3)
	,Amount INT
)
GO
 
INSERT INTO YearMonth Values 
(2016,'Jan',10000)
,(2016,'Feb',15000)
,(2016,'Mar',13000)
,(2017,'Jan',17000)
GO</pre> </p>
<p><strong>Solution 1: Without using PIVOT</strong></p>
<pre class="crayon-plain-tag">select YearData, sum(case when MonthData = 'Jan' then amount else null end) as Jan,
sum(case when MonthData = 'Feb' then amount else null end) as Feb,
sum(case when MonthData = 'Mar' then amount else null end) as Mar
from YearMonth
group by YearData</pre> </p>
<p><strong>Solution 2: Using PIVOT</strong></p>
<pre class="crayon-plain-tag">;WITH CTE AS
(
       SELECT 1 rnk , 'Jan' Mm
       UNION ALL
       SELECT 2 , 'Feb'
       UNION ALL
       SELECT 3 , 'Mar'
)
,CTE3 AS
(
       SELECT T.* , rnk 
       FROM YearMonth T INNER JOIN CTE c ON C.Mm = T.MonthData
)
,CTE4 AS
(
       SELECT YearData,[1] Jan ,[2] Feb ,[3] Mar FROM CTE3
       PIVOT 
       (SUM(Amount) FOR rnk IN ([1],[2],[3])) p
) 
SELECT YearData , SUM(Jan) Jan , SUM(Feb) Feb , SUM(Mar) Mar 
FROM CTE4 
GROUP BY YearData</pre> </p>
<p><strong><code>Please try the different solution for this puzzle and share it via comment...</code></strong></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-display-year-and-month-data-with-or-without-pivot/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server Interview: Advance SQL Query &#8211; Don&#8217;t use pivot and Do Row aggregation into Column</title>
		<link>https://www.dbrnd.com/2017/06/sql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column/</link>
					<comments>https://www.dbrnd.com/2017/06/sql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 03 Jun 2017 16:16:49 +0000</pubDate>
				<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Complex SQL Query]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Pivot]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[T-SQL Developer]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5359</guid>

					<description><![CDATA[In this post, I am sharing an example of SQL Query which is doing Row aggregation into Column without using PIVOT in SQL Server. If you are preparing for the interview of database developer, you should practice this kind of query which may ask by an interviewer. Just execute the below demonstration, and try it [&#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%2F2017%2F06%2Fsql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Don%E2%80%99t%20use%20pivot%20and%20Do%20Row%20aggregation%20into%20Column" 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%2F2017%2F06%2Fsql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Don%E2%80%99t%20use%20pivot%20and%20Do%20Row%20aggregation%20into%20Column" 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%2F2017%2F06%2Fsql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Don%E2%80%99t%20use%20pivot%20and%20Do%20Row%20aggregation%20into%20Column" 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%2F2017%2F06%2Fsql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Don%E2%80%99t%20use%20pivot%20and%20Do%20Row%20aggregation%20into%20Column" 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%2F2017%2F06%2Fsql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Don%E2%80%99t%20use%20pivot%20and%20Do%20Row%20aggregation%20into%20Column" 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%2F2017%2F06%2Fsql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Don%E2%80%99t%20use%20pivot%20and%20Do%20Row%20aggregation%20into%20Column" 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%2F2017%2F06%2Fsql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column%2F&#038;title=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Don%E2%80%99t%20use%20pivot%20and%20Do%20Row%20aggregation%20into%20Column" data-a2a-url="https://www.dbrnd.com/2017/06/sql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column/" data-a2a-title="SQL Server Interview: Advance SQL Query – Don’t use pivot and Do Row aggregation into Column"></a></p><p>In this post, I am sharing an example of SQL Query which is doing Row aggregation into Column without using PIVOT in SQL Server. </p>
<p>If you are preparing for the interview of database developer, you should practice this kind of query which may ask by an interviewer. </p>
<p>Just execute the below demonstration, and try it yourself. </p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_NoPivot
(
	Itemcode char(1)
	,ItemStatus varchar(10)	
	,ItemQty int 
)
GO

INSERT INTO tbl_NoPivot VALUES
('A','New',100),('A','Old',200),('A','Current',300)
,('A','Old',250),('A','Hold',300),('A','Old',50)
,('B','Hold',320),('B','Current',450),('B','Old',150)
,('B','Current',360),('B','New',320),('B','New',220)
GO</pre> </p>
<p><strong>Solution without using a PIVOT:</strong></p>
<pre class="crayon-plain-tag">SELECT 	
	Itemcode
	,sum(case when ItemStatus='Current' then TotalQty end) as CurrentItemStatus
	,sum(case when ItemStatus='Old' then TotalQty end) as OldItemStatus
	,sum(case when ItemStatus='New' then TotalQty end) as NewItemStatus
	,sum(case when ItemStatus='Hold' then TotalQty end) as HoldItemStatus
FROM 
(
	SELECT Itemcode, ItemStatus,sum(ItemQty) as TotalQty 
	FROM tbl_NoPivot	
	GROUP BY Itemcode, ItemStatus
)AS T
GROUP BY Itemcode</pre> </p>
<p><strong>The solution with using a PIVOT:</strong></p>
<pre class="crayon-plain-tag">SELECT 
	Itemcode
	,[Current] AS CurrentItemStatus
	,Old AS OldItemStatus
	,New AS NewItemStatus
	,Hold AS HoldItemStatus
FROM
(
  SELECT Itemcode,ItemQty,ItemStatus
  FROM tbl_NoPivot   
) T
PIVOT
(
  SUM(ItemQty)
  for ItemStatus in (New,Old,[Current],Hold)
) PIV;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/06/sql-server-interview-advance-sql-query-dont-use-pivot-and-do-row-aggregation-into-column/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL 9.6: Introduced CROSSTABVIEW (pivot) in PSQL</title>
		<link>https://www.dbrnd.com/2017/04/postgresql-9-6-introduced-crosstabview-pivot-in-psql/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 12 Apr 2017 16:16:04 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[crosstab]]></category>
		<category><![CDATA[CROSSTABVIEW]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Pivot]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL 9.6]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[psql]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5020</guid>

					<description><![CDATA[In this post, I am sharing a new feature CROSSTABVIEW in PSQL of PostgreSQL 9.6. Many times we require data arrangement horizontally, instead of vertically. Something like we are doing in a spreadsheet where one element has a multiple child elements and require calculation horizontally for each group of items. Now, you can use CROSSTABVIEW [&#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%2F2017%2F04%2Fpostgresql-9-6-introduced-crosstabview-pivot-in-psql%2F&amp;linkname=PostgreSQL%209.6%3A%20Introduced%20CROSSTABVIEW%20%28pivot%29%20in%20PSQL" 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%2F2017%2F04%2Fpostgresql-9-6-introduced-crosstabview-pivot-in-psql%2F&amp;linkname=PostgreSQL%209.6%3A%20Introduced%20CROSSTABVIEW%20%28pivot%29%20in%20PSQL" 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%2F2017%2F04%2Fpostgresql-9-6-introduced-crosstabview-pivot-in-psql%2F&amp;linkname=PostgreSQL%209.6%3A%20Introduced%20CROSSTABVIEW%20%28pivot%29%20in%20PSQL" 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%2F2017%2F04%2Fpostgresql-9-6-introduced-crosstabview-pivot-in-psql%2F&amp;linkname=PostgreSQL%209.6%3A%20Introduced%20CROSSTABVIEW%20%28pivot%29%20in%20PSQL" 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%2F2017%2F04%2Fpostgresql-9-6-introduced-crosstabview-pivot-in-psql%2F&amp;linkname=PostgreSQL%209.6%3A%20Introduced%20CROSSTABVIEW%20%28pivot%29%20in%20PSQL" 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%2F2017%2F04%2Fpostgresql-9-6-introduced-crosstabview-pivot-in-psql%2F&amp;linkname=PostgreSQL%209.6%3A%20Introduced%20CROSSTABVIEW%20%28pivot%29%20in%20PSQL" 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%2F2017%2F04%2Fpostgresql-9-6-introduced-crosstabview-pivot-in-psql%2F&#038;title=PostgreSQL%209.6%3A%20Introduced%20CROSSTABVIEW%20%28pivot%29%20in%20PSQL" data-a2a-url="https://www.dbrnd.com/2017/04/postgresql-9-6-introduced-crosstabview-pivot-in-psql/" data-a2a-title="PostgreSQL 9.6: Introduced CROSSTABVIEW (pivot) in PSQL"></a></p><p>In this post, I am sharing a new feature CROSSTABVIEW in PSQL of PostgreSQL 9.6. Many times we require data arrangement horizontally, instead of vertically. </p>
<p>Something like we are doing in a spreadsheet where one element has a multiple child elements and require calculation horizontally for each group of items.</p>
<p>Now, you can use CROSSTABVIEW in new PSQL for this kind of requirement.</p>
<p><strong>Below is a small demonstration of this:</strong></p>
<p><strong>Open a psql command line and create a below sample table:</strong></p>
<pre class="crayon-plain-tag">create table tbl_range as
select
    ('{a,b,c,d,e,f}'::text[])[1 + floor(random() * 6)] as data1
    ,('{g,h,i,j,k,l}'::text[])[1 + floor(random() * 6)] as data2
    ,random() * 1000 as randomnumber
from generate_series(1,10000);</pre> </p>
<p><strong>Execute below few SELECTs:</strong></p>
<pre class="crayon-plain-tag">select * from tbl_range;
select data1, data2, count(*) from tbl_range group by data1, data2;</pre> </p>
<p><strong>Now check sample SELECT with CROSSTABVIEW:</strong></p>
<pre class="crayon-plain-tag">select data1, data2, count(*) from tbl_range group by data1, data2 \crosstabview data1 data2 count;</pre> </p>
<p><strong>The result:</strong></p>
<pre class="crayon-plain-tag">dbrnd=# select data1, data2, count(*) from tbl_range group by data1, data2 \crosstabview data1 data2 count;
 data1 |  k  |  j  |  i  |  h  |  g  |  l
-------+-----+-----+-----+-----+-----+-----
 f     | 281 | 273 | 261 | 265 | 268 | 258
 a     | 288 | 293 | 278 | 293 | 248 | 279
 d     | 293 | 264 | 278 | 258 | 289 | 291
 c     | 272 | 276 | 292 | 287 | 263 | 281
 e     | 269 | 279 | 281 | 257 | 274 | 318
 b     | 289 | 270 | 301 | 295 | 247 | 291
(6 rows)</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-11)</title>
		<link>https://www.dbrnd.com/2016/06/sql-server-database-developer-t-sql-interview-questions-and-answers-memory-hekaton-offset-fetch-pivot-unpivot-with-schemabinding-day-11/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 22 Jun 2016 19:18:03 +0000</pubDate>
				<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[answers]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Database Developer]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[fetch]]></category>
		<category><![CDATA[Hekaton]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[memory database]]></category>
		<category><![CDATA[memory table]]></category>
		<category><![CDATA[offset]]></category>
		<category><![CDATA[Pivot]]></category>
		<category><![CDATA[questions]]></category>
		<category><![CDATA[schemabinding]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[TSQL]]></category>
		<category><![CDATA[unpivot]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3066</guid>

					<description><![CDATA[What is In-memory Database? Database Theory: What is In-memory Database? What is Hekaton in SQL Server 2014? SQL Server 2014: What is Hekaton? How to create memory optimized table in SQL Server 2014? SQL Server 2014: Create Memory Optimized File Group and Table I want to turn the unique record from multiple rows to multiple [&#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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-memory-hekaton-offset-fetch-pivot-unpivot-with-schemabinding-day-11%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-11%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-memory-hekaton-offset-fetch-pivot-unpivot-with-schemabinding-day-11%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-11%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-memory-hekaton-offset-fetch-pivot-unpivot-with-schemabinding-day-11%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-11%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-memory-hekaton-offset-fetch-pivot-unpivot-with-schemabinding-day-11%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-11%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-memory-hekaton-offset-fetch-pivot-unpivot-with-schemabinding-day-11%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-11%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-memory-hekaton-offset-fetch-pivot-unpivot-with-schemabinding-day-11%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-11%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-memory-hekaton-offset-fetch-pivot-unpivot-with-schemabinding-day-11%2F&#038;title=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-11%29" data-a2a-url="https://www.dbrnd.com/2016/06/sql-server-database-developer-t-sql-interview-questions-and-answers-memory-hekaton-offset-fetch-pivot-unpivot-with-schemabinding-day-11/" data-a2a-title="SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-11)"></a></p><p><strong>What is In-memory Database?</strong></p>
<blockquote class="wp-embedded-content" data-secret="20p6VsNGKF"><p><a href="https://www.dbrnd.com/2015/12/database-theory-what-is-in-memory-database/">Database Theory: What is In-memory Database?</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/12/database-theory-what-is-in-memory-database/embed/#?secret=20p6VsNGKF" data-secret="20p6VsNGKF" width="600" height="338" title="&#8220;Database Theory: What is In-memory Database?&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>What is Hekaton in SQL Server 2014?</strong></p>
<blockquote class="wp-embedded-content" data-secret="PGyhLLADrO"><p><a href="https://www.dbrnd.com/2016/01/sql-server-2014-what-is-hekaton/">SQL Server 2014: What is Hekaton?</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/01/sql-server-2014-what-is-hekaton/embed/#?secret=PGyhLLADrO" data-secret="PGyhLLADrO" width="600" height="338" title="&#8220;SQL Server 2014: What is Hekaton?&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>How to create memory optimized table in SQL Server 2014?</strong></p>
<blockquote class="wp-embedded-content" data-secret="pMRG55kUs9"><p><a href="https://www.dbrnd.com/2016/01/sql-server-2014-create-memory-optimized-file-group-and-table/">SQL Server 2014: Create Memory Optimized File Group and Table</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/01/sql-server-2014-create-memory-optimized-file-group-and-table/embed/#?secret=pMRG55kUs9" data-secret="pMRG55kUs9" width="600" height="338" title="&#8220;SQL Server 2014: Create Memory Optimized File Group and Table&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>I want to turn the unique record from multiple rows to multiple column values. Is it possible in SQL Server?</strong></p>
<blockquote><p>Yes, We can do this task using PIVOT.</p>
<p>SQL Server has a PIVOT relational operator to turn the unique values of a specified column from multiple rows into multiple column values in the output (cross-tab), effectively rotating a table. It also allows performing aggregations, wherever required, for column values that are expected in the final output. The basic syntax for a PIVOT relational operator looks like this:</p></blockquote>
<blockquote><p>
</p><pre class="crayon-plain-tag">SELECT [ColumnNames] FROM [TableName]
PIVOT
(
AggregateFunction([ColumnToBeAggregated])
FOR PivotColumn IN ([PivotColumnValues])
) AS Alias</pre><p>
</p></blockquote>
<p><strong>Do you know about the UNPIVOT?</strong></p>
<blockquote><p>UNPIVOT is another relational operator in SQL Server that performs almost the reverse operation of PIVOT, by rotating column values into rows values.</p>
<p>Using UNPIVOT, Do you think that you can get all correct value which are pivoted using PIVOT?</p>
<p>If you refer back, I said UNPIVOT is almost the reverse of the PIVOT operator; this means it might or might not be exactly the same. The reason is, Pivot performs aggregation while rotating row values into column values and might merge possible multiple row values into single column value in the output.</p></blockquote>
<p><strong>What is the WITH CHECK OPTION clause for a view?</strong></p>
<blockquote><p>The WITH CHECK OPTION for a view prevents data modifications (to the data) that do not confirm to the WHERE clause of the view definition. This allows data to be updated via the view, but only if it belongs in the view.</p></blockquote>
<p><strong>How you are checking that record is exists in database or not?</strong></p>
<p><strong>Using EXISTS clause:</strong></p><pre class="crayon-plain-tag">IF EXISTS(SELECT 1 FROM dbo.Customer WITH(NOLOCK) WHERE CustId = @CustId)
    BEGIN
        PRINT 'Record Exists'
    END
ELSE
    BEGIN
        PRINT 'Record doesn''t Exists'
    END
END IF;</pre><p>
<strong>What is the meaning of the WITH SCHEMABINDING clause?</strong></p>
<ul>
<li>SCHEMABINDING binds the view to the schema of the underlying table or tables. When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the view definition.</li>
<li>The view definition itself must first be modified or dropped to remove dependencies on the table that is to be modified.</li>
<li>All referenced objects must be in the same database. Views or tables that participate in a view created with the SCHEMABINDING clause cannot be dropped unless that view is dropped or changed so that it no longer has schema binding. Otherwise, the Database Engine raises an error.</li>
<li>Also, executing ALTER TABLE statements on tables that participate in views that have schema binding fail when these statements affect the view definition.</li>
<li>The implementation of SCHEMABINDING would increase stability and performance of the system, but also increase the amount of developers’ work.</li>
</ul>
<p><strong>Can we use WITH SCHEMABINDING in Stored Procedures?</strong></p>
<blockquote><p>No: We cannot use WITH SCHEMABINDING in stored procedures.</p></blockquote>
<p><strong>What are the main disadvantages of the WITH SCHEMABINDING clause?</strong></p>
<blockquote><p>It would take more time to make changes in tables’ structure. If I just want to change the size of one column, I will need to modify before and after several views.</p>
<p>The automated test of the application can catch all broken views and functions. This will not increase the amount of developers’ work.</p>
<p>You can not change the collation of a database with schemabound objects.</p></blockquote>
<p><strong>What do you know about the OFFSET FETCH clause of the SQL Server 2012?</strong></p>
<blockquote><p>The OFFSET-FETCH clause provides you with an option to fetch only a window or page of results from the result set. OFFSET-FETCH can be used only with the ORDER BY clause.</p>
<p><strong>OFFSET:</strong> Specifies the number of rows to skip, before starting to return rows from the query expression. The argument for the OFFSET clause can be an integer or expression that is greater than or equal to zero. You can use ROW and ROWS interchangeably.</p>
<p><strong>FETCH:</strong> Specifies the number of rows to return, after processing the OFFSET clause. The argument for the FETCH clause can be an integer or expression that is greater than or equal to one. You can use ROW and ROWS interchangeably. Similarly, FIRST and NEXT can be used interchangeably.</p></blockquote>
<p><strong>Please write a script or logic of custom paging using OFFSET and FETCH NEXT.</strong></p>
<blockquote class="wp-embedded-content" data-secret="ogXSF1WZTo"><p><a href="https://www.dbrnd.com/2016/01/sql-server-2012-custom-paging-using-offset-and-fetch-next/">SQL Server 2012: Custom paging using OFFSET and FETCH NEXT</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/01/sql-server-2012-custom-paging-using-offset-and-fetch-next/embed/#?secret=ogXSF1WZTo" data-secret="ogXSF1WZTo" width="600" height="338" title="&#8220;SQL Server 2012: Custom paging using OFFSET and FETCH NEXT&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>What are the limitations of OFFSET and FETCH clause of the SQL Server 2012?</strong></p>
<ul>
<li>ORDER BY is mandatory to use OFFSET and FETCH clause.</li>
<li>OFFSET clause is mandatory with FETCH. You can never use, ORDER BY … FETCH.</li>
<li>TOP cannot be combined with OFFSET and FETCH in the same query expression.</li>
<li>The OFFSET/FETCH rowcount expression can be any arithmetic, constant, or parameter expression that will return an integer value. The rowcount expression does not support scalar sub-queries.</li>
</ul>
<p><strong>What is the maximum size of a VARCHAR(MAX) variable?</strong></p>
<blockquote><p>Maximum size of a VARCHAR(MAX) is 2GB.</p></blockquote>
<p><strong>What is BCP? When is it used?</strong></p>
<blockquote><p>It is a tool used to duplicate enormous quantity of information from tables and views. It does not facsimile the structures same as foundation to target. BULK INSERT command helps to bring in a data folder into a record, table or view in a user-specific arrangement.</p></blockquote>
<p><strong>When is the Explicit Cursor Used ?</strong></p>
<blockquote><p>If the developer needs to perform the row by row operations for the result set containing more than one row, then he unambiguously declares a pointer with a name. They are managed by OPEN, FETCH and CLOSE.</p>
<p>%FOUND, %NOFOUND, %ROWCOUNT and %ISOPEN characteristics are used in all types of pointers.</p></blockquote>
<p><strong>How can a SQL Developer prevent T-SQL code from running on a production SQL Server?</strong></p>
<blockquote><p>Use IF logic with the @@SERVERNAME function compared against a string with a RETURN command before any other logic.</p></blockquote>
<p><strong>Can you create foreign key constraints on temporary tables?</strong></p>
<blockquote><p>No: You cannot create a foreign key constraint on temporary tables.</p></blockquote>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Arrange Rows to Columns Pivot Table in MySQL</title>
		<link>https://www.dbrnd.com/2015/08/arrange-rows-to-columns-pivot-table-in-mysql/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 29 Aug 2015 18:07:45 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<category><![CDATA[Pivot]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=456</guid>

					<description><![CDATA[In this post, I am sharing a demonstration on the arrangement of the row data into the columns which is called a MySQL Pivot table. What is a Pivot Table? Pivot table is one kind of summary and representation of data, like Microsoft Spreadsheets. Pivot table arranges some of the row categories into column and [&#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%2F08%2Farrange-rows-to-columns-pivot-table-in-mysql%2F&amp;linkname=Arrange%20Rows%20to%20Columns%20Pivot%20Table%20in%20MySQL" 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%2F08%2Farrange-rows-to-columns-pivot-table-in-mysql%2F&amp;linkname=Arrange%20Rows%20to%20Columns%20Pivot%20Table%20in%20MySQL" 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%2F08%2Farrange-rows-to-columns-pivot-table-in-mysql%2F&amp;linkname=Arrange%20Rows%20to%20Columns%20Pivot%20Table%20in%20MySQL" 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%2F08%2Farrange-rows-to-columns-pivot-table-in-mysql%2F&amp;linkname=Arrange%20Rows%20to%20Columns%20Pivot%20Table%20in%20MySQL" 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%2F08%2Farrange-rows-to-columns-pivot-table-in-mysql%2F&amp;linkname=Arrange%20Rows%20to%20Columns%20Pivot%20Table%20in%20MySQL" 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%2F08%2Farrange-rows-to-columns-pivot-table-in-mysql%2F&amp;linkname=Arrange%20Rows%20to%20Columns%20Pivot%20Table%20in%20MySQL" 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%2F08%2Farrange-rows-to-columns-pivot-table-in-mysql%2F&#038;title=Arrange%20Rows%20to%20Columns%20Pivot%20Table%20in%20MySQL" data-a2a-url="https://www.dbrnd.com/2015/08/arrange-rows-to-columns-pivot-table-in-mysql/" data-a2a-title="Arrange Rows to Columns Pivot Table in MySQL"></a></p><p>In this post, I am sharing a demonstration on the arrangement of the row data into the columns which is called a MySQL Pivot table.</p>
<p>What is a Pivot Table?</p>
<blockquote><p>Pivot table is one kind of summary and representation of data, like Microsoft Spreadsheets.<br />
Pivot table arranges some of the row categories into column and also create count and average of that data for better image.</p></blockquote>
<p>Like Microsoft SQL Server, MySQL doesn&#8217;t provide a feature like Pivot Table, but you can apply Pivot from the imported MySQL Table, View or the entire Excel Data Model.<br />
<a href="http://dev.mysql.com/doc/mysql-windows-excerpt/5.1/en/mysql-for-excel-import-pivottables.html" target="_blank">For this reference you can visit this MySQL official link.</a></p>
<p><strong>Create a sample table:</strong></p><pre class="crayon-plain-tag">CREATE DATABASE Employee;

CREATE TABLE Employee.EmployeePivotTest
(
    EmpName VARCHAR(255)
    ,EmpDeptName VARCHAR(255)
    ,EmpAvgWorkingHours INTEGER
);

INSERT INTO Employee.EmployeePivotTest VALUES
('Anvesh','Computer-IT',226)
,('Anvesh','Computer-IT',100)
,('Anvesh','Computer-IT',200)
,('Anvesh','Computer-IT',752)
,('Anvesh','Account',142)
,('Anvesh','Marketing',110)
,('Anvesh','Finance',236)
,('Anvesh','Account',120)
,('Neevan','Computer-IT',120)
,('Neevan','Finance',852)
,('Neevan','Account',326)
,('Neevan','Marketing',50)
,('Neevan','Finance',140);</pre><p>
<p>Examine above data where two employees with working hour for a different department.<br />
In this sample data, employees are working more than one time in some of the department.</p>
<p>Now the requirement is, to populate two rows with four columns.</p>
<pre class="crayon-plain-tag">SELECT 
     EmpName
    ,SUM(Computer_IT) AS TotalHour_Computer_IT
    ,SUM(Account) AS TotalHour_Dept_Account
    ,SUM(Marketing) AS TotalHour_Dept_Marketing
    ,SUM(Finance) AS TotalHour_Dept_Finance
FROM 
(
    SELECT
	EmpName
	,CASE WHEN EmpDeptName = 'Computer-IT' 
		THEN EmpAvgWorkingHours END AS Computer_IT
	,CASE WHEN EmpDeptName = 'Account' 
		THEN EmpAvgWorkingHours END AS Account
	,CASE WHEN EmpDeptName = 'Marketing' 
		THEN EmpAvgWorkingHours END AS Marketing
	,CASE WHEN EmpDeptName = 'Finance' 
		THEN EmpAvgWorkingHours END AS Finance    
    FROM Employee.EmployeePivotTest
) AS T
GROUP BY EmpName;</pre>
<p><strong>Result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLPivotTable.png"><img loading="lazy" decoding="async" class="alignnone wp-image-461 size-full" title="MySQL Pivot Table" src="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLPivotTable.png" alt="MySQL Pivot Table" width="629" height="71" srcset="https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLPivotTable.png 629w, https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLPivotTable-300x34.png 300w, https://www.dbrnd.com/wp-content/uploads/2015/08/MySQLPivotTable-150x17.png 150w" sizes="auto, (max-width: 629px) 100vw, 629px" /></a></p>
<p>You can see the result of two employees with a total working hour for an each department.</p>
<p>The first phase is an inner query, in which we selected employee working hour based on department category.<br />
The second phase is for selecting this data in the outer query and applies group by clause for SUM ().</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
