﻿<?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 2008 / T-SQL (SS2K8)  / Find Row Number Based on Column Information / 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>Wed, 19 Jun 2013 14:46:22 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Find Row Number Based on Column Information</title><link>http://www.sqlservercentral.com/Forums/Topic1422230-392-1.aspx</link><description>I like Sean's solution and got to wondering whether a subquery would work as well:[code="sql"]SELECT t.*, isnull((    select top 1 ItemCode as ItemCode     from #tTestTB t2     where t2.TreeType = 'S' and t2.VisOrder &amp;lt; t.VisOrder     order by VisOrder desc        ), '') as ItemCodeFROM  #tTestTB t[/code]So I tried a small test harness:[code="sql"]IF OBJECT_ID('TempDB..#tTestTB') IS NOT NULLDROP TABLE #tTestTBCREATE TABLE #tTestTB(	[VisOrder] [Int] IDENTITY (0,1) NOT NULL,		[ItemCode] [varchar](25) NULL,	[TreeType] [varchar] (1) NULL	) SET NOCOUNT ONINSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (   'PA3922U-1ARA','N')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (   'MULTI-DOCK-BUNDLE1+','S')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (   'PA3934U-1PRP','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (   'PA3922U-1ARA','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (   'PL10436401','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (   'PL10436401-10pk+','S')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (   'PL10436401','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (   'MULTI-DOCK-BUNDLE3+','S')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (   'PA3934U-1PRP','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (   'PA3956U-1PRP','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (    'PL10436401','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (    'PA3922U-1ARA','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (    'PA1495U-1TWC','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (    'PA3945U-1EAB','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (    'PL10436401-2PK+','S')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (    'PL10436401','I')INSERT INTO #tTestTB (  ItemCode,TreeType) VALUES (    '920-003594','N');WITH Tally(n) AS (    SELECT TOP 1000 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))    FROM sys.all_columns a CROSS JOIN sys.all_columns b)INSERT INTO #tTestTBSELECT ItemCode + '-' + CAST(n AS VARCHAR(7))    ,TreeTypeFROM #tTestTBCROSS APPLY TallyDECLARE @Other VARCHAR(25)PRINT 'Subquery'SET STATISTICS TIME ONSELECT @Other=isnull((    select top 1 ItemCode as ItemCode     from #tTestTB t2     where t2.TreeType = 'S' and t2.VisOrder &amp;lt; t.VisOrder     order by VisOrder desc        ), '') --as ItemCodeFROM  #tTestTB tSET STATISTICS TIME OFFPRINT 'Outer Apply'SET STATISTICS TIME ONSELECT @Other=isnull(x.ItemCode, '') --as ItemCodeFROM  #tTestTB touter apply(    select top 1 ItemCode as ItemCode     from #tTestTB t2     where t2.TreeType = 'S' and t2.VisOrder &amp;lt; t.VisOrder     order by VisOrder desc) xSET STATISTICS TIME OFF DROP TABLE #tTestTB[/code]And got these results:[code="plain"]Subquery SQL Server Execution Times:   CPU time = 17972 ms,  elapsed time = 18181 ms.Outer Apply SQL Server Execution Times:   CPU time = 18330 ms,  elapsed time = 18699 ms.[/code]Over many runs, I think such close results would probably go back and forth.  There is only a slight difference in the execution plans.Obviously I have too much time on my hands late this Friday afternoon. :-P</description><pubDate>Fri, 22 Feb 2013 03:25:21 GMT</pubDate><dc:creator>dwain.c</dc:creator></item><item><title>RE: Find Row Number Based on Column Information</title><link>http://www.sqlservercentral.com/Forums/Topic1422230-392-1.aspx</link><description>Thanks. I really appreciate your help.</description><pubDate>Thu, 21 Feb 2013 07:13:26 GMT</pubDate><dc:creator>miker 8667</dc:creator></item><item><title>RE: Find Row Number Based on Column Information</title><link>http://www.sqlservercentral.com/Forums/Topic1422230-392-1.aspx</link><description>Thanks for the details. Makes this kind of thing a lot easier!!!Something like this?[code]SELECT t.*, isnull(x.ItemCode, '') as ItemCodeFROM  #tTestTB touter apply(select top 1 ItemCode as ItemCode from #tTestTB t2 where t2.TreeType = 'S' and t2.VisOrder &amp;lt; t.VisOrder order by VisOrder desc) x[/code]</description><pubDate>Wed, 20 Feb 2013 13:28:26 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Find Row Number Based on Column Information</title><link>http://www.sqlservercentral.com/Forums/Topic1422230-392-1.aspx</link><description>[code="sql"] DECLARE @ID INT = 2 SELECT TOP 1 * FROM  #tTestTB  WHERE VisOrder &amp;lt; @ID AND TreeType = 'S'   AND EXISTS(SELECT * FROM #tTestTB WHERE VisOrder = @ID AND TreeType &amp;lt;&amp;gt; 'S')  ORDER BY VisOrder DESC[/code]</description><pubDate>Wed, 20 Feb 2013 13:25:27 GMT</pubDate><dc:creator>Alexander Suprun</dc:creator></item><item><title>RE: Find Row Number Based on Column Information</title><link>http://www.sqlservercentral.com/Forums/Topic1422230-392-1.aspx</link><description>Thanks Sean, Please tell me if this is correct:[code="other"]IF OBJECT_ID('TempDB..#tTestTB') IS NOT NULLDROP TABLE #tTestTBCREATE TABLE #tTestTB(	[VisOrder] [Int] NULL,		[ItemCode] [varchar](25) NULL,	[TreeType] [varchar] (1) NULL	) INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   0,'PA3922U-1ARA','N')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   1, 'MULTI-DOCK-BUNDLE1+','S')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   2, 'PA3934U-1PRP','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   3, 'PA3922U-1ARA','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   4, 'PL10436401','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   5, 'PL10436401-10pk+','S')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   6, 'PL10436401','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   7, 'MULTI-DOCK-BUNDLE3+','S')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   8, 'PA3934U-1PRP','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   9, 'PA3956U-1PRP','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   10, 'PL10436401','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   11, 'PA3922U-1ARA','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   12, 'PA1495U-1TWC','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   13, 'PA3945U-1EAB','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   14, 'PL10436401-2PK+','S')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   15, 'PL10436401','I')INSERT INTO #tTestTB (  VisOrder,ItemCode,TreeType) VALUES (   16, '920-003594','N') SELECT * FROM  #tTestTB DROP TABLE #tTestTB[/code]My Desire is to run this on a line by line basis. When run against line 0 the result would be blankSame for line 1Line 2 - 4 would return 'MULTI-DOCK-BUNDLE1+'Line 5 is blankLine 6 Returns 'PL10436401-10pk+' despite line 4 containing the same informationThank you for your help</description><pubDate>Wed, 20 Feb 2013 13:00:46 GMT</pubDate><dc:creator>miker 8667</dc:creator></item><item><title>RE: Find Row Number Based on Column Information</title><link>http://www.sqlservercentral.com/Forums/Topic1422230-392-1.aspx</link><description>I am pretty sure of what you are looking for. Can you take a few minutes to read the article found in my signature about best practices when posting questions? To offer much assistance we need ddl and sample data to work with and that article explains how to gather that information and post it in a readily consumable format.</description><pubDate>Wed, 20 Feb 2013 12:28:35 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>Find Row Number Based on Column Information</title><link>http://www.sqlservercentral.com/Forums/Topic1422230-392-1.aspx</link><description>Hey Guys, I'm trying to find the ItemCode of the TreeType 'S' Above the Line that I'm running the query against.Here is the Table:[code="other"]VisOrder	ItemCode	TreeType0	PA3922U-1ARA	                N1	MULTI-DOCK-BUNDLE1+  S2	PA3934U-1PRP	                I3	PA3922U-1ARA	                I4	PL10436401	                I5	PL10436401-10pk+	        S6	PL10436401	                I7	MULTI-DOCK-BUNDLE3+	S8	PA3934U-1PRP	                I9	PA3956U-1PRP	                I10	PL10436401	                I11	PA3922U-1ARA	                I12	PA1495U-1TWC	        I13	PA3945U-1EAB	                I14	PL10436401-2PK+	        S15	PL10436401	                I16	920-003594	                N[/code]The Resulting Code for PA3934U-1PRP on visorder 2 Should be MULTI-DOCK-BUNDLE1+  while being run against visorder 8 should be MULTI-DOCK-BUNDLE3+.Running PA3922U-1ARA from visOrder 3 Should also return MULTI-DOCK-BUNDLE1+ Etc. And running against a line with treetype 'S' OR 'N' Should not return anything. Thanks for your help.</description><pubDate>Wed, 20 Feb 2013 11:42:12 GMT</pubDate><dc:creator>miker 8667</dc:creator></item></channel></rss>