Search code examples
sqloracleoracle-sqldeveloper

How to find the row and column number of a specific cell in sql?


I have a table in SQL database and I want to find the location of a cell like a coordinate and vice versa. Here is an example:

0 1 2 3                                                                 
1 a b c                                                             
2 g h i                                                              
3 n o j

When I ask for i, I want to get row=2 and column=3. When I ask for a cell of row=2 and column=3, I want to get i.


Solution

  • You need to store your matrix in table specifying the columns and rows like this

    create table matrix (
       row int,
       column int,
       value varchar2(20)
    );
    

    Then you insert your data like this

    insert into matrix values (1, 1, 'a');
    insert into matrix values (1, 2, 'b');
    //and so on. 
    

    And then you can simply find what you need using two queries

    select column, row from matrix where value = 'i';
    select value from matrix where column = 2 and row = 3;