﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / Get products ordered by category order / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 13:14:45 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Get products ordered by category order</title><link>http://www.sqlservercentral.com/Forums/Topic771242-338-1.aspx</link><description>How “deep” does your hierarchy go?...you have given just two levels.If you have many more nested levels, such as this for example:Shirts..........Sports....................Tennis..............................Men..............................Women.....................Golf..............................Men..............................Women...........CasualThen I think it gets a little bit more difficult.Take a look at this post and RBarryYoungs’ solution to a similar question here:[url][b]http://www.sqlservercentral.com/Forums/FindPost771406.aspx[/b][/url]regards gah</description><pubDate>Sun, 16 Aug 2009 16:14:15 GMT</pubDate><dc:creator>J Livingston SQL</dc:creator></item><item><title>RE: Get products ordered by category order</title><link>http://www.sqlservercentral.com/Forums/Topic771242-338-1.aspx</link><description>I think that you're trying to make this more complicated than it needs to be.  I was able to match your output with a simple ORDER BY clause.[code="sql"]SELECT Parent_ID, Product_ID, Product_OrderFROM Dept_Product AS pINNER JOIN Dept AS dON p.Dept_ID = d.Dept_IDORDER BY d.Parent_ID, d.Dept_ID, p.Product_Order[/code]Drew</description><pubDate>Sun, 16 Aug 2009 12:41:20 GMT</pubDate><dc:creator>drew.allen</dc:creator></item><item><title>RE: Get products ordered by category order</title><link>http://www.sqlservercentral.com/Forums/Topic771242-338-1.aspx</link><description>[quote][b]Bob Hovious (8/14/2009)[/b][hr]Lucky, could you show us some sample data and then the expected output?   I'm not sure I understand your question from what you put in the initial post.[b]gah (8/14/2009)[/b][hr]Hi..maybe I have misunderstood your question....but you ask that "want products ordered by category-wise "from the table designs that you have provided I cannot see where " category" is defined?Could you please clarify...thanks[/quote]Thanks for the replies and apologize for not giving more information.Table Schemas=============Table : Dept (Category)Column_name --&gt; Typedept_id --&gt; intparent_id --&gt; intdept_order --&gt; intname --&gt; nvarchar(128)Table : Dept_ProductColumn_name --&gt; Typedept_id --&gt; intproduct_id --&gt; intproduct_order --&gt; intINSERT INTO Dept VALUES(1,0,1,'Shirts')INSERT INTO Dept VALUES(2,1,2,'T-Shirts')INSERT INT Dept VALUES(3,1,3,'Sport')INSERT INT Dept VALUES(4,1,4,'Casual')INSERT INT Dept VALUES((5,0,5,'Pants')INSERT INT Dept VALUES((6,5,6,'Plain')INSERT INT Dept VALUES(7,5,7,'Wide Leg')INSERT INT Dept_Product VALUES(2,101,1)INSERT INT Dept_Product VALUES(2,102,2)INSERT INT Dept_Product VALUES(2,103,4)INSERT INT Dept_Product VALUES(2,104,5)INSERT INT Dept_Product VALUES(2,106,17)INSERT INT Dept_Product VALUES(2,108,19)INSERT INT Dept_Product VALUES(2,109,3)INSERT INT Dept_Product VALUES(2,110,6)INSERT INT Dept_Product VALUES(2,111,10)INSERT INT Dept_Product VALUES(2,112,8)INSERT INT Dept_Product VALUES(3,113,7)INSERT INT Dept_Product VALUES(3,114,9)INSERT INT Dept_Product VALUES(3,115,11)INSERT INT Dept_Product VALUES(3,116,31)INSERT INT Dept_Product VALUES(3,117,27)INSERT INT Dept_Product VALUES(3,118,30)INSERT INT Dept_Product VALUES(2,119,21)INSERT INT Dept_Product VALUES(4,120,9)INSERT INT Dept_Product VALUES(4,121,11)INSERT INT Dept_Product VALUES(4,122,3)INSERT INT Dept_Product VALUES(4,123,1)INSERT INT Dept_Product VALUES(4,124,15)INSERT INT Dept_Product VALUES(4,125,5)INSERT INT Dept_Product VALUES(4,126,2)INSERT INT Dept_Product VALUES(6,127,1)INSERT INT Dept_Product VALUES(6,128,22)INSERT INT Dept_Product VALUES(6,130,24)INSERT INT Dept_Product VALUES(6,135,9)INSERT INT Dept_Product VALUES(6,138,12)INSERT INT Dept_Product VALUES(6,139,10)INSERT INT Dept_Product VALUES(6,154,6)INSERT INT Dept_Product VALUES(7,153,4)INSERT INT Dept_Product VALUES(7,129,21)INSERT INT Dept_Product VALUES(7,131,18)INSERT INT Dept_Product VALUES(7,160,12)INSERT INT Dept_Product VALUES(7,162,51)INSERT INT Dept_Product VALUES(7,163,38)INSERT INT Dept_Product VALUES(7,165,1)The output should look likeFirst Column is the Parent Category (Dept)Second Column is the product idThird column is the ranking of the product in the sub-category and is not a requirement in the output.As T-Shirts, Sport and Casual fall under the parent Shirts the order of products at the parent level should be as follows:1 --&gt; 101 --&gt; 11 --&gt; 102 --&gt; 21 --&gt; 109 --&gt; 31 --&gt; 103 --&gt; 41 --&gt; 104 --&gt; 51 --&gt; 110 --&gt; 61 --&gt; 112 --&gt; 81 --&gt; 111 --&gt; 101 --&gt; 106 --&gt; 171 --&gt; 108 --&gt; 191 --&gt; 113 --&gt; 71 --&gt; 114 --&gt; 91 --&gt; 115 --&gt; 111 --&gt; 119 --&gt; 211 --&gt; 117 --&gt; 271 --&gt; 118 --&gt; 301 --&gt; 116 --&gt; 311 --&gt; 123 --&gt; 11 --&gt; 126 --&gt; 21 --&gt; 122 --&gt; 31 --&gt; 125 --&gt; 51 --&gt; 120 --&gt; 91 --&gt; 121 --&gt; 111 --&gt; 124 --&gt; 15As Plain and Wide Leg fall under the parent Pants the order of products at the parent level should be as follows:5 --&gt; 127 --&gt; 15 --&gt; 154 --&gt; 65 --&gt; 135 --&gt; 95 --&gt; 139 --&gt; 105 --&gt; 138 --&gt; 125 --&gt; 128 --&gt; 225 --&gt; 130 --&gt; 245 --&gt; 165 --&gt; 15 --&gt; 153 --&gt; 45 --&gt; 160 --&gt; 125 --&gt; 131 --&gt; 185 --&gt; 129 --&gt; 215 --&gt; 163 --&gt; 385 --&gt; 162 --&gt; 51I hope this time I'm clear of my requirements.Thanks</description><pubDate>Sun, 16 Aug 2009 08:32:35 GMT</pubDate><dc:creator>lucky-80472</dc:creator></item><item><title>RE: Get products ordered by category order</title><link>http://www.sqlservercentral.com/Forums/Topic771242-338-1.aspx</link><description>Hi..maybe I have misunderstood your question....but you ask that "want products ordered by category-wise "from the table designs that you have provided I cannot see where " category" is defined?Could you please clarify...thanks[quote]Table Schemas=============Table : DeptColumn_name	           Typedept_id		           intparent_id		           intdept_order	           intname		           nvarchar(128)Table : Dept_ProductColumn_name	           Typedept_id		           intproduct_id	           intproduct_order	           int[/quote]can you post some more info please[url]http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]</description><pubDate>Fri, 14 Aug 2009 13:32:27 GMT</pubDate><dc:creator>J Livingston SQL</dc:creator></item><item><title>RE: Get products ordered by category order</title><link>http://www.sqlservercentral.com/Forums/Topic771242-338-1.aspx</link><description>Lucky, could you show us some sample data and then the expected output?   I'm not sure I understand your question from what you put in the initial post.</description><pubDate>Fri, 14 Aug 2009 13:28:22 GMT</pubDate><dc:creator>The Dixie Flatline</dc:creator></item><item><title>Get products ordered by category order</title><link>http://www.sqlservercentral.com/Forums/Topic771242-338-1.aspx</link><description>Hello Peers,I'm having a requirement where in trying to get products ordered by category-wise order related records as shown belowCategory	    Products================1	1011	1021	1032	1042	1052	1063	1073	1083	109Table Schemas=============Table : DeptColumn_name	           Typedept_id		           intparent_id		           intdept_order	           intname		           nvarchar(128)Table : Dept_ProductColumn_name	           Typedept_id		           intproduct_id	           intproduct_order	           intI have used different ways by using the rank functions but nowhere i'm coming too close to the solution.I would appreciate if one of you throws some light.Thanks</description><pubDate>Fri, 14 Aug 2009 13:01:32 GMT</pubDate><dc:creator>lucky-80472</dc:creator></item></channel></rss>