<?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>Complex SQL Query &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/complex-sql-query/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>Wed, 21 Jun 2017 20:20:33 +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 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>SQL Server Interview: Advance SQL Query &#8211; Find a count of repeated character in a String</title>
		<link>https://www.dbrnd.com/2017/06/sql-server-interview-advance-sql-query-find-a-count-of-repeated-character-in-a-string/</link>
					<comments>https://www.dbrnd.com/2017/06/sql-server-interview-advance-sql-query-find-a-count-of-repeated-character-in-a-string/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 01 Jun 2017 16:16:31 +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[Repeated String]]></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[string count]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5354</guid>

					<description><![CDATA[In this post, I am sharing one complex SQL Query interview question and solution for T-SQL Developers. Last week, I asked this question to one of the T-SQL Developer. If you are going for database developer interview, you must find and practice complex or advance SQL Queries. Question: Find a count of given repeated character [&#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-find-a-count-of-repeated-character-in-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Find%20a%20count%20of%20repeated%20character%20in%20a%20String" 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-find-a-count-of-repeated-character-in-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Find%20a%20count%20of%20repeated%20character%20in%20a%20String" 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-find-a-count-of-repeated-character-in-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Find%20a%20count%20of%20repeated%20character%20in%20a%20String" 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-find-a-count-of-repeated-character-in-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Find%20a%20count%20of%20repeated%20character%20in%20a%20String" 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-find-a-count-of-repeated-character-in-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Find%20a%20count%20of%20repeated%20character%20in%20a%20String" 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-find-a-count-of-repeated-character-in-a-string%2F&amp;linkname=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Find%20a%20count%20of%20repeated%20character%20in%20a%20String" 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-find-a-count-of-repeated-character-in-a-string%2F&#038;title=SQL%20Server%20Interview%3A%20Advance%20SQL%20Query%20%E2%80%93%20Find%20a%20count%20of%20repeated%20character%20in%20a%20String" data-a2a-url="https://www.dbrnd.com/2017/06/sql-server-interview-advance-sql-query-find-a-count-of-repeated-character-in-a-string/" data-a2a-title="SQL Server Interview: Advance SQL Query – Find a count of repeated character in a String"></a></p><p>In this post, I am sharing one complex SQL Query interview question and solution for T-SQL Developers. </p>
<p>Last week, I asked this question to one of the T-SQL Developer.<br />
If you are going for database developer interview, you must find and practice complex or advance SQL Queries.</p>
<p><strong>Question:</strong> Find a count of given repeated character from a string</p>
<p><strong>Create a table with sample data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Strings (name VARCHAR(30))
GO

INSERT INTO tbl_Strings
VALUES
('India'),('Southern Europe')
,('Japan'),('Indonesia')
,('Swaziland'),('South Africa')
,('United Arab Emirates'),('United States')
GO</pre> </p>
<p><strong>Solution:</strong></p>
<pre class="crayon-plain-tag">;WITH CTE AS
(
		SELECT name, CONVERT(VARCHAR(1),LEFT(name,1)) AS Letter, RIGHT(name, LEN(name)-1) AS Remain
		FROM tbl_Strings
		WHERE LEN(name)&gt;1
	UNION ALL
		SELECT name, CONVERT(VARCHAR(1),LEFT(Remain,1)) AS Letter, 
			RIGHT(Remain, LEN(Remain)-1) AS Remain
		FROM CTE
		WHERE LEN(Remain)&gt;0
)
SELECT 
	name
	,Letter
	,ASCII(Letter) AS CharAsciiCode
	,COUNT(Letter) AS CountOfLetter
FROM CTE
WHERE Letter IN('a','A')
GROUP BY name, Letter, ASCII(Letter)</pre> </p>
<p><strong>The Result:</strong></p>
<pre class="crayon-plain-tag">name                           Letter CharAsciiCode CountOfLetter
------------------------------ ------ ------------- -------------
South Africa                   A      65            1
United Arab Emirates           A      65            1
India                          a      97            1
Indonesia                      a      97            1
Japan                          a      97            2
South Africa                   a      97            1
Swaziland                      a      97            2
United Arab Emirates           a      97            2
United States                  a      97            1</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/06/sql-server-interview-advance-sql-query-find-a-count-of-repeated-character-in-a-string/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
