Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Find Row Number Based on Column Information Expand / Collapse
Author
Message
Posted Wednesday, February 20, 2013 11:42 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, July 30, 2013 9:58 AM
Points: 10, Visits: 35
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:

VisOrder	ItemCode	TreeType
0 PA3922U-1ARA N
1 MULTI-DOCK-BUNDLE1+ S
2 PA3934U-1PRP I
3 PA3922U-1ARA I
4 PL10436401 I
5 PL10436401-10pk+ S
6 PL10436401 I
7 MULTI-DOCK-BUNDLE3+ S
8 PA3934U-1PRP I
9 PA3956U-1PRP I
10 PL10436401 I
11 PA3922U-1ARA I
12 PA1495U-1TWC I
13 PA3945U-1EAB I
14 PL10436401-2PK+ S
15 PL10436401 I
16 920-003594 N

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.
Post #1422230
Posted Wednesday, February 20, 2013 12:28 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:24 PM
Points: 11,990, Visits: 11,007
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.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1422246
Posted Wednesday, February 20, 2013 1:00 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, July 30, 2013 9:58 AM
Points: 10, Visits: 35
Thanks Sean,

Please tell me if this is correct:

IF OBJECT_ID('TempDB..#tTestTB') IS NOT NULL
DROP TABLE #tTestTB
CREATE 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


My Desire is to run this on a line by line basis.

When run against line 0 the result would be blank
Same for line 1
Line 2 - 4 would return 'MULTI-DOCK-BUNDLE1+'
Line 5 is blank
Line 6 Returns 'PL10436401-10pk+' despite line 4 containing the same information

Thank you for your help
Post #1422263
Posted Wednesday, February 20, 2013 1:25 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 5:29 PM
Points: 185, Visits: 902
 DECLARE @ID INT = 2
SELECT TOP 1 * FROM #tTestTB
WHERE VisOrder < @ID AND TreeType = 'S'
AND EXISTS(SELECT * FROM #tTestTB WHERE VisOrder = @ID AND TreeType <> 'S')
ORDER BY VisOrder DESC




Alex Suprun
Post #1422281
Posted Wednesday, February 20, 2013 1:28 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:24 PM
Points: 11,990, Visits: 11,007
Thanks for the details. Makes this kind of thing a lot easier!!!

Something like this?

SELECT t.*, isnull(x.ItemCode, '') as ItemCode
FROM #tTestTB t
outer apply(select top 1 ItemCode as ItemCode from #tTestTB t2 where t2.TreeType = 'S' and t2.VisOrder < t.VisOrder order by VisOrder desc) x



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1422283
Posted Thursday, February 21, 2013 7:13 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, July 30, 2013 9:58 AM
Points: 10, Visits: 35
Thanks. I really appreciate your help.
Post #1422571
Posted Friday, February 22, 2013 3:25 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: 2 days ago @ 6:03 PM
Points: 3,590, Visits: 5,098
I like Sean's solution and got to wondering whether a subquery would work as well:

SELECT t.*, isnull((
select top 1 ItemCode as ItemCode
from #tTestTB t2
where t2.TreeType = 'S' and t2.VisOrder < t.VisOrder
order by VisOrder desc
), '') as ItemCode
FROM #tTestTB t


So I tried a small test harness:

IF OBJECT_ID('TempDB..#tTestTB') IS NOT NULL
DROP TABLE #tTestTB
CREATE TABLE #tTestTB(
[VisOrder] [Int] IDENTITY (0,1) NOT NULL,
[ItemCode] [varchar](25) NULL,
[TreeType] [varchar] (1) NULL
)
SET NOCOUNT ON
INSERT 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 #tTestTB
SELECT ItemCode + '-' + CAST(n AS VARCHAR(7))
,TreeType
FROM #tTestTB
CROSS APPLY Tally

DECLARE @Other VARCHAR(25)

PRINT 'Subquery'
SET STATISTICS TIME ON
SELECT @Other=isnull((
select top 1 ItemCode as ItemCode
from #tTestTB t2
where t2.TreeType = 'S' and t2.VisOrder < t.VisOrder
order by VisOrder desc
), '') --as ItemCode
FROM #tTestTB t
SET STATISTICS TIME OFF

PRINT 'Outer Apply'
SET STATISTICS TIME ON
SELECT @Other=isnull(x.ItemCode, '') --as ItemCode
FROM #tTestTB t
outer apply(
select top 1 ItemCode as ItemCode
from #tTestTB t2
where t2.TreeType = 'S' and t2.VisOrder < t.VisOrder
order by VisOrder desc) x
SET STATISTICS TIME OFF

DROP TABLE #tTestTB


And got these results:

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.



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.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1422953
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse