<?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>PeterKellner.net &#187; TSQL</title>
	<atom:link href="http://peterkellner.net/category/tsql/feed/" rel="self" type="application/rss+xml" />
	<link>http://peterkellner.net</link>
	<description>Microsoft Focused, JavaScript,HTML5 (ExtJS, SenchaTouch &#38; Windows 8 Metro)</description>
	<lastBuildDate>Fri, 11 May 2012 16:43:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Tip for Using SQLServer To Count By Alpha Names in a list</title>
		<link>http://peterkellner.net/2011/10/04/tip-for-using-sqlserver-to-count-by-alpha-names-in-a-list/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tip-for-using-sqlserver-to-count-by-alpha-names-in-a-list</link>
		<comments>http://peterkellner.net/2011/10/04/tip-for-using-sqlserver-to-count-by-alpha-names-in-a-list/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 05:11:33 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2008]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/10/04/tip-for-using-sqlserver-to-count-by-alpha-names-in-a-list/</guid>
		<description><![CDATA[&#160;
So, this is not a big tip, but worth at least 10 minutes to figure it out on your own.&#160; Here is to saving you 10 minutes:
&#160;

SELECT LEFT (userlastname, 1) as alpha,       count(id)FROM attendeesWHERE id IN (             [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>So, this is not a big tip, but worth at least 10 minutes to figure it out on your own.&#160; Here is to saving you 10 minutes:</p>
<p>&#160;</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">SELECT</span> <span style="color: #0000ff">LEFT</span> (userlastname, 1) <span style="color: #0000ff">as</span> alpha,       <span style="color: #0000ff">count</span>(id)<span style="color: #0000ff">FROM</span> attendees<span style="color: #0000ff">WHERE</span> id <span style="color: #0000ff">IN</span> (              <span style="color: #0000ff">SELECT</span> attendeesid              <span style="color: #0000ff">FROM</span> AttendeesCodeCampYear              <span style="color: #0000ff">WHERE</span> codecampyearid = 6      )<span style="color: #0000ff">GROUP</span> <span style="color: #0000ff">BY</span>  <span style="color: #0000ff">LEFT</span> (userlastname, 1)O<span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> <span style="color: #0000ff">LEFT</span> (userlastname, 1)</pre>
<p></div>
<p>It’s pretty self explanatory.&#160; Our case is we have two tables that we track code camp attendees.&#160; One is the master list, and the other is a detail by year.&#160; (6 is this year)</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/10/image3.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/10/image_thumb.png" width="344" height="198" /></a></p>
<p><span id="more-1600"></span></p>
<p>And, the results:</p>
<p>&gt; NULL&#160;&#160;&#160; 1</p>
<p>&gt;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 6</p>
<p>&gt; .&#160;&#160;&#160;&#160;&#160;&#160; 1</p>
<p>&gt; @&#160;&#160;&#160;&#160;&#160;&#160; 1</p>
<p>&gt; _&#160;&#160;&#160;&#160;&#160;&#160; 18</p>
<p>&gt; A&#160;&#160;&#160;&#160;&#160;&#160; 319</p>
<p>&gt; B&#160;&#160;&#160;&#160;&#160;&#160; 488</p>
<p>&gt; C&#160;&#160;&#160;&#160;&#160;&#160; 543</p>
<p>&gt; Ç&#160;&#160;&#160;&#160;&#160;&#160; 2</p>
<p>&gt; D&#160;&#160;&#160;&#160;&#160;&#160; 313</p>
<p>&gt; E&#160;&#160;&#160;&#160;&#160;&#160; 84</p>
<p>&gt; F&#160;&#160;&#160;&#160;&#160;&#160; 169</p>
<p>&gt; g&#160;&#160;&#160;&#160;&#160;&#160; 368</p>
<p>&gt; H&#160;&#160;&#160;&#160;&#160;&#160; 340</p>
<p>&gt; I&#160;&#160;&#160;&#160;&#160;&#160; 49</p>
<p>&gt; J&#160;&#160;&#160;&#160;&#160;&#160; 190</p>
<p>&gt; K&#160;&#160;&#160;&#160;&#160;&#160; 474</p>
<p>&gt; L&#160;&#160;&#160;&#160;&#160;&#160; 460</p>
<p>&gt; M&#160;&#160;&#160;&#160;&#160;&#160; 592</p>
<p>&gt; N&#160;&#160;&#160;&#160;&#160;&#160; 255</p>
<p>&gt; O&#160;&#160;&#160;&#160;&#160;&#160; 79</p>
<p>&gt; P&#160;&#160;&#160;&#160;&#160;&#160; 427</p>
<p>&gt; Q&#160;&#160;&#160;&#160;&#160;&#160; 14</p>
<p>&gt; R&#160;&#160;&#160;&#160;&#160;&#160; 336</p>
<p>&gt; S&#160;&#160;&#160;&#160;&#160;&#160; 776</p>
<p>&gt; T&#160;&#160;&#160;&#160;&#160;&#160; 302</p>
<p>&gt; u&#160;&#160;&#160;&#160;&#160;&#160; 27</p>
<p>&gt; V&#160;&#160;&#160;&#160;&#160;&#160; 191</p>
<p>&gt; w&#160;&#160;&#160;&#160;&#160;&#160; 296</p>
<p>&gt; X&#160;&#160;&#160;&#160;&#160;&#160; 21</p>
<p>&gt; y&#160;&#160;&#160;&#160;&#160;&#160; 176</p>
<p>&gt; Z&#160;&#160;&#160;&#160;&#160;&#160; 100</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/10/04/tip-for-using-sqlserver-to-count-by-alpha-names-in-a-list/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Writing A Transact SQL (TSQL) Procedure For SQL Server 2008 To Delete Rows From Table Safely</title>
		<link>http://peterkellner.net/2010/04/29/sqlserver2008-tsql-storedproc-delete-rows-in-groups-safely/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlserver2008-tsql-storedproc-delete-rows-in-groups-safely</link>
		<comments>http://peterkellner.net/2010/04/29/sqlserver2008-tsql-storedproc-delete-rows-in-groups-safely/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 23:54:21 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2008]]></category>
		<category><![CDATA[Transaction]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/04/29/sqlserver2008-tsql-storedproc-delete-rows-in-groups-safely/</guid>
		<description><![CDATA[In this post, we will show and explain a small TSQL Sql Server 2008 procedure that deletes all rows in a table that are older than some specified date.&#160; That is, say the table has 10,000,000 rows in it the accumulated over the past 2 years.&#160; 
Say you want to delete all but the last [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, we will show and explain a small <a href="http://msdn.microsoft.com/en-us/library/ms189826(SQL.90).aspx">TSQL</a> <a href="http://www.microsoft.com/sqlserver/2008/en/us/">Sql Server 2008</a> procedure that deletes all rows in a table that are older than some specified date.&#160; That is, say the table has 10,000,000 rows in it the accumulated over the past 2 years.&#160; </p>
<p>Say you want to delete all but the last 30 days of activity.&#160; If you just simply say <a href="http://msdn.microsoft.com/en-us/library/ms189835.aspx">DELETE</a> FROM table WHERE id&gt;10000, you will cause this to happen in one transaction and likely, you will get an error.&#160; That’s the best case.&#160; The worst case is your system tries to do this, eventually consumes all the resources in your computer and crashes your server.</p>
<p> <span id="more-1285"></span>
<p>&#160;</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">PROCEDURE</span> dbo.DeleteRowsInGroupsBeforeNdaysLog4NetAll@NoOfRowsToDelete <span style="color: #0000ff">int</span>,@DaysBeforeTodayToDelete <span style="color: #0000ff">int</span><span style="color: #0000ff">AS</span><span style="color: #0000ff">BEGIN</span>

  <span style="color: #0000ff">DECLARE</span> @DateToDeleteBefore <span style="color: #0000ff">date</span>    <span style="color: #008000">-- Number Of Days To Delete Records From</span>  <span style="color: #0000ff">DECLARE</span> @DateToday datetime         <span style="color: #008000">-- To Hold Todays Date</span>  <span style="color: #0000ff">DECLARE</span> @PrintMessage <span style="color: #0000ff">varchar</span>(256)  <span style="color: #008000">-- To Hold Print Message        </span>  <span style="color: #0000ff">DECLARE</span> @CurrentRowCount <span style="color: #0000ff">int</span>        <span style="color: #008000">-- Scratch Var For Counting Rows</span>

  <span style="color: #0000ff">SET</span> @DateToday = GetDate()

  <span style="color: #0000ff">SELECT</span> @CurrentRowCount =         (           <span style="color: #0000ff">SELECT</span> <span style="color: #0000ff">COUNT</span>(*)           <span style="color: #0000ff">FROM</span> dbo.Log4NetAll           <span style="color: #0000ff">WHERE</span> <span style="color: #0000ff">Date</span> &lt;(@DateToday - @DaysBeforeTodayToDelete)         ) 

  <span style="color: #0000ff">WHILE</span> (@CurrentRowCount &gt; 0) <span style="color: #0000ff">BEGIN</span>      <span style="color: #0000ff">DELETE</span> <span style="color: #0000ff">TOP</span> (@NoOfRowsToDelete)      <span style="color: #0000ff">FROM</span> dbo.Log4NetAll        <span style="color: #0000ff">WHERE</span> Id <span style="color: #0000ff">IN</span> (        <span style="color: #0000ff">SELECT</span> <span style="color: #0000ff">TOP</span> (@NoOfRowsToDelete) Id         <span style="color: #0000ff">FROM</span> Log4NetAll        <span style="color: #0000ff">WHERE</span> <span style="color: #0000ff">Date</span> &lt; (@DateToday - @DaysBeforeTodayToDelete)        <span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> Id <span style="color: #0000ff">ASC</span>)

       <span style="color: #008000">-- Count the records again</span>       <span style="color: #0000ff">SELECT</span> @CurrentRowCount =         (           <span style="color: #0000ff">SELECT</span> <span style="color: #0000ff">COUNT</span>(*)           <span style="color: #0000ff">FROM</span> dbo.Log4NetAll           <span style="color: #0000ff">WHERE</span> <span style="color: #0000ff">Date</span> &lt;(@DateToday - @DaysBeforeTodayToDelete)         )        <span style="color: #0000ff">SET</span> @PrintMessage = N<span style="color: #006080">'Deleting This Many Rows: '</span>                            + (<span style="color: #0000ff">CAST</span>(@CurrentRowCount <span style="color: #0000ff">AS</span> nvarchar(10)));       <span style="color: #0000ff">PRINT</span> @PrintMessage;  <span style="color: #0000ff">END</span>  END</pre>
<p></div>
<div>* Inspired by a <a href="http://www.sqlservercurry.com/2008/04/how-to-delete-records-from-large-table.html">blog post by Suprotim Agarwal</a> </div>
<div>&#160;</div>
<div id="codeSnippetWrapper">Basically, what is happening in the above code is first, we are testing to see if there are any records that meet our criteria to delete.&#160; That is, records older than a certain date.&#160; Next, we have a WHILE loop that continue until there are no more records that meet the criteria.&#160; The <a href="http://msdn.microsoft.com/en-us/library/ms189835.aspx">DELETE SQL command</a> uses TOP, which is very handy because it will delete up to that many.&#160; That way, you don’t have to worry about deleting the last few records with any special case code.</div>
<div>&#160;</div>
<div>The only thing a little tricky that I did was to actually delete the records with <a href="http://msdn.microsoft.com/en-us/library/ms189575.aspx">Subquery</a> that forces a sort of the records before deleting.&#160; That is, by asking for Id IN (Records sorted ascending), I’m forcing my TOP command to delete the oldest records first.&#160; This may not be necessary if your method runs to completion, but if you interrupt it, it might be convenient for the oldest records to be deleted first.</div>
<div>
  </div>
<p>Then, do Execute the Procedure, you would call it as follows assuming you wanted to delete the code in batches.</p>
<p>&#160;</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">DECLARE</span> @NoOfRowsToDelete <span style="color: #0000ff">int</span>;<span style="color: #0000ff">DECLARE</span> @DaysBeforeTodayToDelete <span style="color: #0000ff">int</span>;

<span style="color: #0000ff">SET</span> @NoOfRowsToDelete = 100;<span style="color: #0000ff">SET</span> @DaysBeforeTodayToDelete = 30;

<span style="color: #0000ff">EXEC</span> [dbo].[DeleteRowsInGroupsBeforeNdaysLog4NetAll]   @NoOfRowsToDelete, @DaysBeforeTodayToDelete ;</pre>
<p></div>
<p>The output of calling this might be:</p>
<p>Query execution was canceled by user request.<br />
  <br />Deleting This Many Rows: 19199 </p>
<p>Deleting This Many Rows: 19099 </p>
<p>Deleting This Many Rows: 18999 </p>
<p>Deleting This Many Rows: 18899 </p>
<p>Deleting This Many Rows: 18799 </p>
<p>Deleting This Many Rows: 18699 </p>
<p>Deleting This Many Rows: 18599… (All the way until 0)</p>
<p>&#160;</p>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/04/29/sqlserver2008-tsql-storedproc-delete-rows-in-groups-safely/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using SqlServer 2008 and TSQL Subtract 1 Hour From All Values In a DateTime Column</title>
		<link>http://peterkellner.net/2010/04/28/sqlserver2008-dateadd-function-subract-hour/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=sqlserver2008-dateadd-function-subract-hour</link>
		<comments>http://peterkellner.net/2010/04/28/sqlserver2008-dateadd-function-subract-hour/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 23:36:00 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[Sql Server 2008]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/04/28/sqlserver2008-dateadd-function-subract-hour/</guid>
		<description><![CDATA[In this post, we’ll go briefly the process of how you would update all rows in a SQL Server 2008 table such that a particular date column will be moved back 1 hour in time.&#160; This is actually pretty simple, but being that I typically do my work in the ORM layer (that is LINQ2SQL [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, we’ll go briefly the process of how you would update all rows in a SQL Server 2008 table such that a particular date column will be moved back 1 hour in time.&#160; This is actually pretty simple, but being that I typically do my work in the ORM layer (that is <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx">LINQ2SQL</a> or <a href="http://msdn.microsoft.com/en-us/library/aa697427(VS.80).aspx">Entity Framework</a>), I just don’t do much of this.&#160; The process I’m going to follow is first to use <a href="http://msdn.microsoft.com/en-us/library/ms174173.aspx">Microsoft SQL Server Management Studio</a> to make sure I know what I’m doing with the Sql Server 2008 functions, then add it to an UPDATE statement.</p>
<p> <span id="more-1284"></span>
<p>So, first thing I do is fire up <a href="http://msdn.microsoft.com/en-us/library/ms174173.aspx">Microsoft SQL Server Management Studio</a>, create a new query (it does not matter what database because I’m going to simply execute some <a href="http://en.wikipedia.org/wiki/Transact-SQL">TSQL</a>.</p>
<p>Basically, I’m going to write some simple TSQL to create a variable with today&#8217;s date (and time) in it, then use the function <a href="http://msdn.microsoft.com/en-us/library/ms186819.aspx">DATEADD</a> and create a new date with 1 hour less.&#160; Here is the code:</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">DECLARE</span> @MyDate datetime<span style="color: #0000ff">DECLARE</span> @NewDate datetime

<span style="color: #0000ff">SET</span> @MyDate = GetDate();<span style="color: #0000ff">SET</span> @NewDate = DATEADD(<span style="color: #0000ff">HOUR</span>,-1,@MyDate);

<span style="color: #0000ff">PRINT</span> @MyDate<span style="color: #0000ff">PRINT</span> @NewDate</pre>
<p></div>
<p>And, after pasting it in Microsoft SQL Server Management Studio, you can see the output when pressing execute.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/UsingSqlServer2008andTSQLSubtract1HourFr_EA7E/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/UsingSqlServer2008andTSQLSubtract1HourFr_EA7E/image_thumb.png" width="401" height="328" /></a> </p>
<p>Now, we’ve proven to ourselves we no how to subtract an hour, let’s apply it to a simple SQL Statement and presto, problem solved.&#160; Here is the final simple SQL that does the job.</p>
<div>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">UPDATE</span> <span style="color: #0000ff">Load</span> <span style="color: #0000ff">SET</span> ProcessedDate=DATEADD(<span style="color: #0000ff">HOUR</span>,-1,ProcessedDate) <span style="color: #0000ff">WHERE</span> Id=954249</pre>
</div>
<div>&#160;</div>
<div>That’s it! Hope this simple little post helps someone.</div>
<div>
  </div>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/04/28/sqlserver2008-dateadd-function-subract-hour/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LINQPad Help For Creating Nasty Group by Query in Native SQL</title>
		<link>http://peterkellner.net/2009/09/28/linq2sql-groupby-date-linqpad/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=linq2sql-groupby-date-linqpad</link>
		<comments>http://peterkellner.net/2009/09/28/linq2sql-groupby-date-linqpad/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 16:51:21 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2009/09/28/linq2sql-groupby-date-linqpad/</guid>
		<description><![CDATA[ I often speak very highly of LINQ and also LINQPad.&#160; This morning, I was struggling with some sql that would let me do a count by DateTime while stripping out the time portion.&#160; That is, I just want to know how many entries are in the table for each Date (regardless of what time).&#160; [...]]]></description>
			<content:encoded><![CDATA[<p> I often speak very highly of <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx">LINQ</a> and also <a href="http://www.linqpad.net/">LINQPad</a>.&#160; This morning, I was struggling with some sql that would let me do a count by DateTime while stripping out the time portion.&#160; That is, I just want to know how many entries are in the table for each Date (regardless of what time).&#160; I tried lots of solutions I got from search, and they all gave not correct results, usually involving Casting and other non fun sql programming constructs.</p>
<p> <span id="more-361"></span>
<p>It occurred to me I could do it in LINQ, then grab out the SQL it generated.&#160; The tool?&#160; LINQPad of course.&#160; So, here is what I typed into LINQPad:</p>
<pre class="csharpcode">(from data <span class="kwrd">in</span> Attendees
let dateNoTime =
    <span class="kwrd">new</span> DateTime
     (data.CreationDate.Value.Year,
      data.CreationDate.Value.Month,
      data.CreationDate.Value.Day)
orderby dateNoTime
group data by dateNoTime into g
<span class="kwrd">where</span> g.Count() &gt; 0
select <span class="kwrd">new</span>
    {
    g.Key,
    Number_Registered = g.Count()
    }).OrderByDescending(f=&gt;f.Key)</pre>
<p>
  </p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<pre class="csharpcode">Then, After pasting that into LINQPad, choosing the SQL tab on the bottom, it gives me this:</pre>
<pre class="csharpcode"><span class="kwrd">SELECT</span> [t2].[value3] <span class="kwrd">AS</span> [<span class="kwrd">Key</span>], [t2].[value2] <span class="kwrd">AS</span> [Number_Registered]
<span class="kwrd">FROM</span> (
<span class="kwrd">SELECT</span> <span class="kwrd">COUNT</span>(*) <span class="kwrd">AS</span> [<span class="kwrd">value</span>], <span class="kwrd">COUNT</span>(*) <span class="kwrd">AS</span> [value2], [t1].[<span class="kwrd">value</span>]
          <span class="kwrd">AS</span> [value3]
<span class="kwrd">FROM</span> (
    <span class="kwrd">SELECT</span> <span class="kwrd">CONVERT</span>
         (DATETIME, <span class="kwrd">CONVERT</span>(<span class="kwrd">NCHAR</span>(2), DATEPART(<span class="kwrd">Month</span>, [t0].[CreationDate]))
           + (<span class="str">'/'</span> + (<span class="kwrd">CONVERT</span>(<span class="kwrd">NCHAR</span>(2), DATEPART(<span class="kwrd">Day</span>, [t0].[CreationDate])) +
              (<span class="str">'/'</span> + <span class="kwrd">CONVERT</span>(<span class="kwrd">NCHAR</span>(4), DATEPART(<span class="kwrd">Year</span>, [t0].[CreationDate]))))), 101)
                    <span class="kwrd">AS</span> [<span class="kwrd">value</span>]
        <span class="kwrd">FROM</span> [Attendees] <span class="kwrd">AS</span> [t0]
        ) <span class="kwrd">AS</span> [t1]
    <span class="kwrd">GROUP</span> <span class="kwrd">BY</span> [t1].[<span class="kwrd">value</span>]
    ) <span class="kwrd">AS</span> [t2]
<span class="kwrd">WHERE</span> [t2].[<span class="kwrd">value</span>] &gt; @p0
<span class="kwrd">ORDER</span> <span class="kwrd">BY</span> [t2].[value3] DESC</pre>
<p>
  </p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>And, the nice screen shot of LINQPad</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/LINQPadHelpForCreatingNastyGroupbyQueryi_875D/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/LINQPadHelpForCreatingNastyGroupbyQueryi_875D/image_thumb.png" width="397" height="421" /></a></p>
<p>And, it works perfect!&#160; Even the TSql generated looks pretty nice.&#160; <a href="http://msdn.microsoft.com/en-us/library/ms177673.aspx">Group By</a>, <a href="http://msdn.microsoft.com/en-us/library/ms187928.aspx">Convert</a>, <a href="http://msdn.microsoft.com/en-us/library/ms174420.aspx">DatePart</a> and everything. Somehow, I feel like I’ve cheated, but I’m good with that in this case.</p>
<p>Hope this helps!</p>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2009/09/28/linq2sql-groupby-date-linqpad/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 31/36 queries in 0.032 seconds using disk: basic
Content Delivery Network via Amazon Web Services: S3: PetersBlogCDN.s3.amazonaws.com

Served from: peterkellner.net @ 2012-05-23 00:39:01 -->
