How to select data from several rows in a single table in one result row?

dan sawyer

How to select multiple entries in a single query.

The db table structure represents an excel like spreadsheet. Each observation is a workbook.

Work book 1  
1     2  
3     4  

Work book 2  
5    6  
7    8  

with each db row representing a single cell and observations in separate workbooks. The table definition is:

observation integer 
row_number integer
col_number integer
value integer

The database then looks like this:

observation row_number  col_number  value  
    1        1             1        1  
    1        1             2        2  
    1        2             1        3  
    1        2             2        4  
    2        1             1        5  
    2        1             2        6  
    2        2             1        7  
    2        2             2        8  

The question is how to create a single query to:

select observation, value as value1 from database where row_number = 1 and col_number = 2,
select value as value2 from database where row_number = 2 and col_number = 1;

to create:

observation value1  value2  
1             2      3  
2             6      7 

I have tried joins and subqueries.

Jonathan Leffler

The basic answer is with a self-join. The table name 'database' is pretty objectionable: I'm going to call it 'spreadsheet'.

 SELECT r1.observation, r1.value AS value1, r2.value AS value2
   FROM spreadsheet AS r1
   JOIN spreadsheet AS r2
     ON r1.observation = r2.observation
  WHERE r1.row_number = 1
    AND r1.col_number = 2
    AND r2.row_number = 2
    AND r2.col_number = 1

There are plenty of other ways of writing the same query. One of them is:

 SELECT r1.observation, r1.value1, r2.value2
   FROM (SELECT observation, value AS value1
           FROM spreadsheet
          WHERE r1.row_number = 1
            AND r1.col_number = 2
        ) AS r1
   JOIN (SELECT observation, value AS value2
           FROM spreadsheet
          WHERE r2.row_number = 2
            AND r2.col_number = 1
        ) AS r2
     ON r1.observation = r2.observation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select a single row where multiple rows exist from a table

From Dev

How to select a single row where multiple rows exist from a table

From Dev

how to select one row from one table and multiple rows from other table using joins in mysql,

From Dev

MySQL: combine 2 rows from another table into a single result row

From Dev

In rails, how to select one specific column's data from a table? (there is only 1 row so I want only a specific result but not an array)

From Dev

Postgres select from one to many table to single table rows

From Dev

Make one row in result table from two rows in SQLite

From Dev

How to combine multiple rows from 4 tables into one single row in a new table in SQL?

From Dev

How to select multiple rows of mysql table as a single row

From Dev

Group several rows of data into one row by column?

From Dev

How to select rows from one data.table to apply in another data.table?

From Dev

How to select rows where given data appear in more than one row in a table?

From Dev

How to select rows where given data appear in more than one row in a table?

From Dev

MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

From Dev

How to select a specific row from the table with one column as a sum of values of other rows?

From Dev

Select two rows, using data from one row

From Dev

Select data in single row for multi records of an ID from table

From Dev

Convert the multiple rows Select statement result into a single row string result

From Java

Using data.table to select rows by distance from another row

From Dev

How to get multiple rows from single row of table?

From Dev

How to combine data from one table where there are duplicate values into a single row?

From Dev

How to combine data from one table where there are duplicate values into a single row?

From Dev

How to get result from two different table with one single input

From Dev

How to get result from two different table with one single input

From Dev

SQL - How to select discontinuous rows from a table with one select?

From Dev

Select From one table : single-row subquery returns more than one row

From Dev

How can I get a single value from a mysql result, if I have several rows with the same value

From Dev

select single row from multiple rows by id

From Dev

from data table, randomly select one row per group

Related Related

  1. 1

    How to select a single row where multiple rows exist from a table

  2. 2

    How to select a single row where multiple rows exist from a table

  3. 3

    how to select one row from one table and multiple rows from other table using joins in mysql,

  4. 4

    MySQL: combine 2 rows from another table into a single result row

  5. 5

    In rails, how to select one specific column's data from a table? (there is only 1 row so I want only a specific result but not an array)

  6. 6

    Postgres select from one to many table to single table rows

  7. 7

    Make one row in result table from two rows in SQLite

  8. 8

    How to combine multiple rows from 4 tables into one single row in a new table in SQL?

  9. 9

    How to select multiple rows of mysql table as a single row

  10. 10

    Group several rows of data into one row by column?

  11. 11

    How to select rows from one data.table to apply in another data.table?

  12. 12

    How to select rows where given data appear in more than one row in a table?

  13. 13

    How to select rows where given data appear in more than one row in a table?

  14. 14

    MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

  15. 15

    How to select a specific row from the table with one column as a sum of values of other rows?

  16. 16

    Select two rows, using data from one row

  17. 17

    Select data in single row for multi records of an ID from table

  18. 18

    Convert the multiple rows Select statement result into a single row string result

  19. 19

    Using data.table to select rows by distance from another row

  20. 20

    How to get multiple rows from single row of table?

  21. 21

    How to combine data from one table where there are duplicate values into a single row?

  22. 22

    How to combine data from one table where there are duplicate values into a single row?

  23. 23

    How to get result from two different table with one single input

  24. 24

    How to get result from two different table with one single input

  25. 25

    SQL - How to select discontinuous rows from a table with one select?

  26. 26

    Select From one table : single-row subquery returns more than one row

  27. 27

    How can I get a single value from a mysql result, if I have several rows with the same value

  28. 28

    select single row from multiple rows by id

  29. 29

    from data table, randomly select one row per group

HotTag

Archive