Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Get selected cells rows

Author  Topic 

ramzansadiq
Starting Member

6 Posts

Posted - 2009-06-10 : 21:58:06
Hi there,

I have a table which has all excel cell values(a1,a2,b1,b2 and so on) , I want to pass single cell value which will return all relevent cell rows.
1. If I pass a1 output rows will be ( a1,a2,b1,b2)
2. If I pass d4 output rows will be ( c3,c4,c5,d3,d4,d5, e3,e4,e5)

I didn't create this Db and have limited info. This table doesn't depend on any other table.

Columns are

ID(auto numbers), Cell Values(a1,a2,..-varchar(2)), Plan_Fee(doesn't match for this criteria-number), Prop_Fee(doesn't match for this criteria-number), Geometry (describing all the coorindates ).

This is GIS(Georaphic Information System) Table database.

Help is greatly appreciated.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-10 : 22:48:54
it will be much easier if you have the row / col info in there


-- sample TABLE
DECLARE @cell TABLE
(
ID int identity(1,1),
cell varchar(10)
)

-- Generate some sample data
INSERT INTO @cell (cell)
SELECT cell = CHAR(ASCII('A') + c.NUMBER) + CONVERT(varchar(10), n.NUMBER)
FROM F_TABLE_NUMBER_RANGE(0, 6) c
CROSS JOIN F_TABLE_NUMBER_RANGE(1, 6) n
ORDER BY c.NUMBER, n.NUMBER

DECLARE @ref_cell varchar(10)

SELECT @ref_cell = 'D4'

-- Query
;WITH cell (cell, row, col)
AS
(
SELECT cell,
row = row_number() OVER (PARTITION BY dbo.fnFilterString(cell, '[A-Z]') ORDER BY cell),
col = row_number() OVER (PARTITION BY dbo.fnFilterString(cell, '[0-9]') ORDER BY cell)
FROM @cell c
)
SELECT c.cell
FROM cell c
INNER JOIN cell r ON c.row >= r.row - 1
AND c.row <= r.row + 1
AND c.col >= r.col - 1
AND c.col <= r.col + 1
WHERE r.cell = @ref_cell
ORDER BY c.cell



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -