<?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>8bit Blog &#187; Databases</title>
	<atom:link href="http://www.8bit.rs/blog/category/databases/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.8bit.rs/blog</link>
	<description></description>
	<lastBuildDate>Fri, 13 Jan 2012 19:13:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Howto: SQL / LINQ JOIN on TOP 1 row</title>
		<link>http://www.8bit.rs/blog/2010/02/howto-sql-linq-join-on-top-1-row/</link>
		<comments>http://www.8bit.rs/blog/2010/02/howto-sql-linq-join-on-top-1-row/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 12:06:21 +0000</pubDate>
		<dc:creator>bdrajer</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Microsoft .Net]]></category>

		<guid isPermaLink="false">http://www.8bit.rs/blog/index.php/2010/02/howto-sql-linq-join-on-top-1-row/</guid>
		<description><![CDATA[Problem: you want to do a LEFT or INNER JOIN between two tables but include only one record from the other table: that is, you don’t want the join to create duplicate records. Interestingly enough, I found the solution to this through LINQ. In LINQ, you can do this without really thinking about it: Surprisingly [...]]]></description>
			<content:encoded><![CDATA[<p>Problem: you want to do a LEFT or INNER JOIN between two tables but include only one record from the other table: that is, you don’t want the join to create duplicate records. Interestingly enough, I found the solution to this through LINQ. In LINQ, you can do this without really thinking about it:</p>
<pre>
<pre class="brush: sql; title: ; notranslate">
from cmp in ctx.Companies
join pers in ctx.Persons on cmp.Persons.First().ID equals pers.ID
from cmp in ctx.Companies
join pers in ctx.Persons on cmp.Persons.First().ID equals pers.ID
</pre>
</pre>
<p>Surprisingly for me, this query gets translated into working SQL, which looks something like this (note that I cleaned it up quite a bit for readability):</p>
<pre>
<pre class="brush: sql; title: ; notranslate">
view plaincopy to clipboardprint
FROM Company
INNER JOIN Person ON
(
    SELECT TOP (1) top1Person.ID
    FROM Person AS top1Person
    WHERE top1Person.CompanyID = Company.ID
) = Person.ID
FROM Company
INNER JOIN Person ON
(
    SELECT TOP (1) top1Person.ID
    FROM Person AS top1Person
    WHERE top1Person.CompanyID = Company.ID
) = Person.ID</pre>
</pre>
<p>Once you think about it, the solution is quite simple. All you need to remember is that a JOIN can contain subselects (even subselects with their own JOINs).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.8bit.rs/blog/2010/02/howto-sql-linq-join-on-top-1-row/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Common Table Expression For Sorting Hierarchical Records By Depth</title>
		<link>http://www.8bit.rs/blog/2009/02/common-table-expression-for-sorting-hierarchical-records-by-depth/</link>
		<comments>http://www.8bit.rs/blog/2009/02/common-table-expression-for-sorting-hierarchical-records-by-depth/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 14:54:11 +0000</pubDate>
		<dc:creator>bdrajer</dc:creator>
				<category><![CDATA[Databases]]></category>

		<guid isPermaLink="false">http://www.8bit.rs/blog/?p=12</guid>
		<description><![CDATA[I tried to find this on the net, and was unable to find a satisfying and simple solution. How do you retrieve hierarchical records (say, from a self-referencing table) sorted by their depth in the hierarchy? Microsoft SQL Server 2005/2008 has a new SQL construct,&#160; Here’s one example: a Category table with an ID (primary [...]]]></description>
			<content:encoded><![CDATA[<p>I tried to find this on the net, and was unable to find a satisfying and simple solution. How do you retrieve hierarchical records (say, from a self-referencing table) sorted by their depth in the hierarchy? Microsoft SQL Server 2005/2008 has a new SQL construct,&#160; Here’s one example: a Category table with an ID (primary key) and ParentID (pointing to the hierarchical parent).</p>
<pre>WITH CategoryCTE(ID, ParentID, Depth)
AS
(
  SELECT ID, ParentID, 0
  FROM Category
  WHERE ParentID IS NULL – root records

  UNION ALL 

  SELECT cRecursive.ID, cRecursive.ParentID, cCte.Depth+1
  FROM Category AS cRecursive JOIN CategoryCTE AS cCte
      ON cRecursive.ParentID = cCte.ID
)
SELECT *
FROM CategoryCTE
ORDER BY Depth</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.8bit.rs/blog/2009/02/common-table-expression-for-sorting-hierarchical-records-by-depth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

