<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-29286778.post8248146502838372450..comments</id><updated>2008-06-17T03:43:36.571-04:00</updated><category term='Feature Creeper'/><category term='FAQs and not so FAQs'/><category term='SQL Puzzles'/><category term='Pass time with SQL'/><title type='text'>Comments on SQL Garbage Collector: A Scenario to Ponder #12</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.sqlpointers.com/feeds/8248146502838372450/comments/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default'/><link rel='alternate' type='text/html' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html'/><author><name>Omnibuzz</name><uri>http://www.blogger.com/profile/04342835880399785393</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-29286778.post-95211662120228168</id><published>2008-06-17T03:43:00.000-04:00</published><updated>2008-06-17T03:43:00.000-04:00</updated><title type='text'>DECLARE @Employees int&lt;br&gt;SELECT @Employees = COUN...</title><content type='html'>DECLARE @Employees int&lt;BR/&gt;SELECT @Employees = COUNT(DISTINCT(employeeid) ) FROM orders&lt;BR/&gt;&lt;BR/&gt;SELECT employee.employeeid, customer.customerid&lt;BR/&gt;FROM (SELECT employeeid, (@Employees - (row_number() OVER (ORDER BY COUNT(Orderid)))) [Rank] FROM orders GROUP BY employeeid) employee&lt;BR/&gt;JOIN (SELECT customerid, (row_number() OVER (ORDER BY COUNT(Orderid)) - 1)/(@Employees+1) [Rank] FROM orders GROUP BY customerid) customer&lt;BR/&gt;  ON customer.[RANK] = Employee.[Rank]&lt;BR/&gt;&lt;BR/&gt;to be unecessarily complete</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default/95211662120228168'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default/95211662120228168'/><link rel='alternate' type='text/html' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html?showComment=1213688580000#c95211662120228168' title=''/><author><name>David</name><uri>http://www.blogger.com/profile/03173499313307254059</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html' ref='tag:blogger.com,1999:blog-29286778.post-8248146502838372450' source='http://www.blogger.com/feeds/29286778/posts/default/8248146502838372450' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1049084755'/></entry><entry><id>tag:blogger.com,1999:blog-29286778.post-5011940964275957266</id><published>2008-06-17T03:16:00.000-04:00</published><updated>2008-06-17T03:16:00.000-04:00</updated><title type='text'>DECLARE @Employees int&lt;br&gt;select @Employees = coun...</title><content type='html'>DECLARE @Employees int&lt;BR/&gt;select @Employees = count(distinct(employeeid)) FROM #EmployeeCustomerOrders&lt;BR/&gt;&lt;BR/&gt;--2005&lt;BR/&gt;&lt;BR/&gt;SELECT employee.employeeid, customer.customerid&lt;BR/&gt;FROM (&lt;BR/&gt;  select employeeid, &lt;BR/&gt;  (@Employees - (row_number() over (order by sum(Ordercount)))) [Rank]&lt;BR/&gt;   from #EmployeeCustomerOrders &lt;BR/&gt;  group by employeeid) employee &lt;BR/&gt;JOIN (&lt;BR/&gt;  select customerid, &lt;BR/&gt;  (row_number() over (order by sum(Ordercount)) - 1)/(@Employees+1) [Rank]&lt;BR/&gt;  from #EmployeeCustomerOrders &lt;BR/&gt;  group by customerid) customer &lt;BR/&gt;ON customer.[RANK] = Employee.[Rank]&lt;BR/&gt;&lt;BR/&gt;-- 2000&lt;BR/&gt;&lt;BR/&gt;select employeeid,IDENTITY (int,0,1) [rank] &lt;BR/&gt;into #Employees&lt;BR/&gt;from #EmployeeCustomerOrders &lt;BR/&gt;group by employeeid &lt;BR/&gt;order by sum(ordercount) asc&lt;BR/&gt;&lt;BR/&gt;select customerid,IDENTITY (int,0,1) [rank] &lt;BR/&gt;into #customers&lt;BR/&gt;from #EmployeeCustomerOrders &lt;BR/&gt;group by customerid &lt;BR/&gt;order by sum(ordercount) asc&lt;BR/&gt;&lt;BR/&gt;SELECT employee.employeeid, customer.customerid&lt;BR/&gt;FROM ( select employeeid, @Employees-[rank]-1 [rank] from #employees) employee &lt;BR/&gt;JOIN ( Select customerid, ([rank])/(@Employees+1) [rank] from #customers) customer &lt;BR/&gt;  ON customer.[RANK] = Employee.[Rank]&lt;BR/&gt;&lt;BR/&gt;drop table #employees&lt;BR/&gt;drop table #customers&lt;BR/&gt;&lt;BR/&gt;Query plan improvement and 2000 equivalent</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default/5011940964275957266'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default/5011940964275957266'/><link rel='alternate' type='text/html' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html?showComment=1213686960000#c5011940964275957266' title=''/><author><name>David</name><uri>http://www.blogger.com/profile/03173499313307254059</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html' ref='tag:blogger.com,1999:blog-29286778.post-8248146502838372450' source='http://www.blogger.com/feeds/29286778/posts/default/8248146502838372450' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1049084755'/></entry><entry><id>tag:blogger.com,1999:blog-29286778.post-7515959505306156856</id><published>2008-06-16T05:05:00.000-04:00</published><updated>2008-06-16T05:05:00.000-04:00</updated><title type='text'>First attempt at using row_number and over in 2005...</title><content type='html'>First attempt at using row_number and over in 2005. (assuming rounding evaluation)&lt;BR/&gt;&lt;BR/&gt;SELECT employee.ID, customer.ID&lt;BR/&gt;FROM (select count(distinct(employeeid)) EmployeBuckets FROM #EmployeeCustomerOrders) Total,&lt;BR/&gt;     (select ID,Orders, row_number() over (order by Orders) [Rank]&lt;BR/&gt;        from (select employeeid ID, sum(ordercount) Orders from #EmployeeCustomerOrders group by employeeid)E ) employee,&lt;BR/&gt;     (select ID,Orders, row_number() over (order by Orders) [Rank] &lt;BR/&gt;        from (select customerid ID, sum(ordercount) Orders from #EmployeeCustomerOrders group by customerid) C ) customer&lt;BR/&gt;WHERE ((customer.[RANK]-1)/(Total.EmployeBuckets+1)) = Total.EmployeBuckets - Employee.[Rank]</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default/7515959505306156856'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default/7515959505306156856'/><link rel='alternate' type='text/html' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html?showComment=1213607100000#c7515959505306156856' title=''/><author><name>David</name><uri>http://www.blogger.com/profile/03173499313307254059</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html' ref='tag:blogger.com,1999:blog-29286778.post-8248146502838372450' source='http://www.blogger.com/feeds/29286778/posts/default/8248146502838372450' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-1049084755'/></entry><entry><id>tag:blogger.com,1999:blog-29286778.post-1510794129226434111</id><published>2007-11-21T00:23:00.000-05:00</published><updated>2007-11-21T00:23:00.000-05:00</updated><title type='text'>select identity(int) as sl,1 as EmployeeId,custome...</title><content type='html'>select identity(int) as sl,1 as EmployeeId,customerid,ordercount into #custorders from #EmployeeCustomerOrders order by OrderCount asc&lt;BR/&gt;select identity(int) as empsl, avg(employeeid) as Employeeid,count(*) as RecievedCount into #Emporders1 from #EmployeeCustomerOrders group by Employeeid order by RecievedCount desc&lt;BR/&gt;select identity(int) as empsl,  Employeeid, RecievedCount into #Emporders from #Emporders1&lt;BR/&gt;&lt;BR/&gt;&lt;BR/&gt;&lt;BR/&gt;declare @Empid int&lt;BR/&gt;declare @avg int&lt;BR/&gt;declare @TotalCount int&lt;BR/&gt;declare @Loop int&lt;BR/&gt;declare @avgloop int&lt;BR/&gt;declare @Empcount int&lt;BR/&gt;set @avgloop =  1&lt;BR/&gt;set @Loop = 1&lt;BR/&gt;set @Empcount = 1&lt;BR/&gt;set @avg = ((select count(*) from #EmployeeCustomerOrders)/(select count(*) from #Emporders) + 1)&lt;BR/&gt;&lt;BR/&gt;select *  into #tempcustorders from #custorders&lt;BR/&gt;set @TotalCount = (select count(*) from #EmployeeCustomerOrders)&lt;BR/&gt;while(@Loop&lt;=@TotalCount)&lt;BR/&gt;begin&lt;BR/&gt;&lt;BR/&gt; if(@avgloop &gt; @avg)  &lt;BR/&gt; begin&lt;BR/&gt;  &lt;BR/&gt;  set @Empcount = @Empcount + 1&lt;BR/&gt;  set @avgloop = 1&lt;BR/&gt;  update #custorders set EmployeeId = (select EmployeeId from #Emporders where empsl=@Empcount)  where sl=@Loop&lt;BR/&gt; end&lt;BR/&gt; else&lt;BR/&gt; begin&lt;BR/&gt;  set @Empid = (select EmployeeId from #Emporders where empsl=@Empcount)  &lt;BR/&gt;  update #custorders set EmployeeId = (select EmployeeId from #Emporders where empsl=@Empcount)  where sl=@Loop&lt;BR/&gt;  set @avgloop = @avgloop + 1&lt;BR/&gt; end&lt;BR/&gt; set @Loop = @Loop + 1&lt;BR/&gt;end&lt;BR/&gt;&lt;BR/&gt;select * from #custorders</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default/1510794129226434111'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default/1510794129226434111'/><link rel='alternate' type='text/html' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html?showComment=1195622580000#c1510794129226434111' title=''/><author><name>Ramu</name><uri>http://www.blogger.com/profile/13680777721992875418</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html' ref='tag:blogger.com,1999:blog-29286778.post-8248146502838372450' source='http://www.blogger.com/feeds/29286778/posts/default/8248146502838372450' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-301236465'/></entry><entry><id>tag:blogger.com,1999:blog-29286778.post-354210304314851499</id><published>2007-08-03T10:56:00.000-04:00</published><updated>2007-08-03T10:56:00.000-04:00</updated><title type='text'>WITH    EmployeeCustomerOrders&lt;br&gt;          AS ( S...</title><content type='html'>WITH    EmployeeCustomerOrders&lt;BR/&gt;          AS ( SELECT   EmployeeID,&lt;BR/&gt;                        CustomerID,&lt;BR/&gt;                        OrderCount = COUNT(orderid)&lt;BR/&gt;               FROM     Orders&lt;BR/&gt;               GROUP BY EmployeeID,&lt;BR/&gt;                        CustomerID&lt;BR/&gt;             ) ,&lt;BR/&gt;        CustomerList&lt;BR/&gt;          AS ( SELECT   CustomerId,&lt;BR/&gt;                        OrderCount = COUNT(OrderCount),&lt;BR/&gt;                        CustomerRank = DENSE_RANK() OVER ( ORDER BY COUNT(OrderCount) ASC )&lt;BR/&gt;               FROM     EmployeeCustomerOrders&lt;BR/&gt;               GROUP BY CustomerId&lt;BR/&gt;             ) ,&lt;BR/&gt;        EmployeeList&lt;BR/&gt;          AS ( SELECT   EmployeeID,&lt;BR/&gt;                        OrderCount = COUNT(OrderCount),&lt;BR/&gt;                        EmployeeRank = DENSE_RANK() OVER ( ORDER BY COUNT(OrderCount) DESC )&lt;BR/&gt;               FROM     EmployeeCustomerOrders&lt;BR/&gt;               GROUP BY EmployeeID&lt;BR/&gt;             )&lt;BR/&gt;    SELECT  cl.CustomerID,&lt;BR/&gt;            EmployeeId = ( SELECT   MAX(EmployeeRank)&lt;BR/&gt;                           FROM     EmployeeList&lt;BR/&gt;                           WHERE    EmployeeRank &lt;= cl.CustomerRank&lt;BR/&gt;                         )&lt;BR/&gt;    FROM    CustomerList cl&lt;BR/&gt;            LEFT JOIN EmployeeList el ON cl.CustomerRank = el.EmployeeRank&lt;BR/&gt;    GROUP BY cl.CustomerID,&lt;BR/&gt;            cl.CustomerRank,&lt;BR/&gt;            el.EmployeeRank&lt;BR/&gt;    ORDER BY EmployeeId</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default/354210304314851499'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/29286778/8248146502838372450/comments/default/354210304314851499'/><link rel='alternate' type='text/html' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html?showComment=1186152960000#c354210304314851499' title=''/><author><name>Erhan Hosca</name><uri>http://www.hosca.com</uri><email>noreply@blogger.com</email><gd:image xmlns:gd='http://schemas.google.com/g/2005' rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img1.blogblog.com/img/blank.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://www.sqlpointers.com/2007/02/scenario-to-ponder-12.html' ref='tag:blogger.com,1999:blog-29286778.post-8248146502838372450' source='http://www.blogger.com/feeds/29286778/posts/default/8248146502838372450' type='text/html'/><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='blogger.itemClass' value='pid-964130431'/></entry></feed>
