<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: SQL Puzzle: SQL Advance Query &#8211; Group and Count of Consecutive flags	</title>
	<atom:link href="https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-group-and-count-of-consecutive-flags/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-group-and-count-of-consecutive-flags/</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Sat, 04 May 2019 12:08:28 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>
		By: Jerry		</title>
		<link>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-group-and-count-of-consecutive-flags/#comment-7098</link>

		<dc:creator><![CDATA[Jerry]]></dc:creator>
		<pubDate>Sat, 04 May 2019 12:08:28 +0000</pubDate>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6588#comment-7098</guid>

					<description><![CDATA[;WITH CTE
AS
(
SELECT id,flag,LAG(flag)OVER (ORDER BY id)lg,IIF(flag=(LAG(flag)OVER (ORDER BY id)),0,1)R
FROM tbl_flags
),
CTE2
AS
(
SELECT id,flag,SUM(R)OVER(ORDER BY id)S
FROM CTE
)
SELECT flag,COUNT(S)
FROM CTE2
Group By flag,S]]></description>
			<content:encoded><![CDATA[<p>;WITH CTE<br />
AS<br />
(<br />
SELECT id,flag,LAG(flag)OVER (ORDER BY id)lg,IIF(flag=(LAG(flag)OVER (ORDER BY id)),0,1)R<br />
FROM tbl_flags<br />
),<br />
CTE2<br />
AS<br />
(<br />
SELECT id,flag,SUM(R)OVER(ORDER BY id)S<br />
FROM CTE<br />
)<br />
SELECT flag,COUNT(S)<br />
FROM CTE2<br />
Group By flag,S</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Anvesh Patel		</title>
		<link>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-group-and-count-of-consecutive-flags/#comment-6224</link>

		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 13 Feb 2018 18:32:13 +0000</pubDate>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6588#comment-6224</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-group-and-count-of-consecutive-flags/#comment-6223&quot;&gt;Sql&lt;/a&gt;.

Yes!]]></description>
			<content:encoded><![CDATA[<p>Yes!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Sql		</title>
		<link>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-group-and-count-of-consecutive-flags/#comment-6223</link>

		<dc:creator><![CDATA[Sql]]></dc:creator>
		<pubDate>Tue, 13 Feb 2018 06:15:25 +0000</pubDate>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6588#comment-6223</guid>

					<description><![CDATA[why did u use id- ROW_NUMBER() OVER (PARTITION BY flag ORDER BY id) rnk ?
Just for grouping? Thanks.]]></description>
			<content:encoded><![CDATA[<p>why did u use id- ROW_NUMBER() OVER (PARTITION BY flag ORDER BY id) rnk ?<br />
Just for grouping? Thanks.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Dinesh.IS		</title>
		<link>https://www.dbrnd.com/2017/10/sql-puzzle-sql-advance-query-group-and-count-of-consecutive-flags/#comment-5943</link>

		<dc:creator><![CDATA[Dinesh.IS]]></dc:creator>
		<pubDate>Mon, 23 Oct 2017 10:41:28 +0000</pubDate>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6588#comment-5943</guid>

					<description><![CDATA[Declare @Result Table(ID Int,SLNo Int)
Declare @ID As Int,@Val Int=&#039;&#039;,@RowCnt Int,@PVal Int=&#039;&#039;;
Select @ID=Min(ID) From TBL_Flags
Set @RowCnt=1;

While @ID Is Not Null
Begin
		Select @Val=Flag From TBL_Flags Where ID=@ID 
		
		If @PVal@Val 
		Begin
			Set @RowCnt=@RowCnt+1
		End

		Insert Into @Result
		(ID,SLNo)
		Values(@Val,@RowCnt)

		Select @ID=Min(ID) From TBL_Flags Where ID&#062;@ID 		

		Set @PVal=@Val

End

Select ID As FLag,Count(SLNo) As NoOfCnt From @Result
Group By ID,SLNo 
Order By ID,SLNo]]></description>
			<content:encoded><![CDATA[<p>Declare @Result Table(ID Int,SLNo Int)<br />
Declare @ID As Int,@Val Int=&#8221;,@RowCnt Int,@PVal Int=&#8221;;<br />
Select @ID=Min(ID) From TBL_Flags<br />
Set @RowCnt=1;</p>
<p>While @ID Is Not Null<br />
Begin<br />
		Select @Val=Flag From TBL_Flags Where ID=@ID </p>
<p>		If @PVal@Val<br />
		Begin<br />
			Set @RowCnt=@RowCnt+1<br />
		End</p>
<p>		Insert Into @Result<br />
		(ID,SLNo)<br />
		Values(@Val,@RowCnt)</p>
<p>		Select @ID=Min(ID) From TBL_Flags Where ID&gt;@ID 		</p>
<p>		Set @PVal=@Val</p>
<p>End</p>
<p>Select ID As FLag,Count(SLNo) As NoOfCnt From @Result<br />
Group By ID,SLNo<br />
Order By ID,SLNo</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
