What is the best way for selecting 2 row from table in one row?

Ulphat

I have a table like following

TABLE_A
ID           PERSON_ID    NAME         GRADE
----------   ----------   ----------   ----------
1            1            NAME_1       10
2            1            NAME_1       20
3            2            NAME_2       30
4            2            NAME_2       40
...

in this table, for each name there is exactly two rows (two grades). I want to make a query which results like following

RESULT
PERSON_ID    NAME         GRADE1       GRADE_2
----------   ----------   ----------   ----------
1            NAME_1       10            20
2            NAME_2       30            40

What is the best way for this. I can use self join but I think this is not correct method

Ulphat

I found best answer for PostgreSQL in this link https://www.postgresql.org/docs/9.2/static/tablefunc.html In PostgreSQL there is the crosstab(text) function.

The crosstab function is used to produce "pivot" displays, wherein data is listed across the page rather than down. For example, we might have data like

row1    val11
row1    val12
row1    val13
...
row2    val21
row2    val22
row2    val23
...

which we wish to display like

row1    val11   val12   val13   ...
row2    val21   val22   val23   ...
...

The crosstab function takes a text parameter that is a SQL query producing raw data formatted in the first way, and produces a table formatted in the second way. The sql parameter is a SQL statement that produces the source set of data. This statement must return one row_name column, one category column, and one value column. N is an obsolete parameter, ignored if supplied (formerly this had to match the number of output value columns, but now that is determined by the calling query). For example, the provided query might produce a set something like:

row_name    cat    value
----------+-------+-------
  row1      cat1    val1
  row1      cat2    val2
  row1      cat3    val3
  row1      cat4    val4
  row2      cat1    val5
  row2      cat2    val6
  row2      cat3    val7
  row2      cat4    val8

The crosstab function is declared to return setof record, so the actual names and types of the output columns must be defined in the FROM clause of the calling SELECT statement, for example:

SELECT * FROM crosstab('...') AS ct(row_name text, category_1 text, category_2 text);

This example produces a set something like:

           <== value  columns  ==>
 row_name   category_1   category_2
----------+------------+------------
  row1        val1         val2
  row2        val5         val6

For more read section F.35.1.2. in URL entered top of the answer

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inserting Into a Row and Then Selecting from that Row in One Query

From Dev

Selecting row from table in laravel

From Dev

Selecting two rows from another table using one row

From Dev

How to push one table values to another table by selecting one or more row using angular 2

From Dev

Selecting row from Table - Java Selenium

From Dev

mySQL - Selecting the latest row from a table

From Dev

MySQL - selecting random row from large table

From Dev

Elegant way for selecting "best" row for each category in grouped query

From Dev

Selecting Latest Row From MAX Row of 3rd Table

From Dev

Selecting a row in a html table

From Dev

What is the best way to define a row vector in NumPy?

From Dev

Grab one row from a table

From Dev

GWT CellTable: Selecting one checkbox selects every checkbox/row in the table

From Dev

selecting records from main table and count of each row in another table

From Dev

How to get 2 columns from one table and 2 rows as columns from other table in one row, in MySQL?

From Dev

what is the fastest way to insert row in two table

From Dev

Best way of selecting 8k+ rows from a table

From Dev

Using Java, what is the best way to update a column in every row with values from an array? (SQLite)

From Dev

Only Selecting Specific Row Info From One Query

From Dev

Selecting one row from sqlite database using rawQuery and rowid in Android

From Dev

correct way to handle logout from table row

From Dev

Best way to add row to HTML table without re-rendering

From Dev

Best way verify existence of a row in a table via Protractor?

From Dev

Best way to store data in a table row and retrieve via jQuery

From Dev

Best way to update a table view after deleting a row with animation?

From Dev

Finding one row from pivot table

From Dev

Copy a row from one table to another

From Dev

Jquery - copy a row from one table to another

From Dev

Move table columns from one row into another

Related Related

  1. 1

    Inserting Into a Row and Then Selecting from that Row in One Query

  2. 2

    Selecting row from table in laravel

  3. 3

    Selecting two rows from another table using one row

  4. 4

    How to push one table values to another table by selecting one or more row using angular 2

  5. 5

    Selecting row from Table - Java Selenium

  6. 6

    mySQL - Selecting the latest row from a table

  7. 7

    MySQL - selecting random row from large table

  8. 8

    Elegant way for selecting "best" row for each category in grouped query

  9. 9

    Selecting Latest Row From MAX Row of 3rd Table

  10. 10

    Selecting a row in a html table

  11. 11

    What is the best way to define a row vector in NumPy?

  12. 12

    Grab one row from a table

  13. 13

    GWT CellTable: Selecting one checkbox selects every checkbox/row in the table

  14. 14

    selecting records from main table and count of each row in another table

  15. 15

    How to get 2 columns from one table and 2 rows as columns from other table in one row, in MySQL?

  16. 16

    what is the fastest way to insert row in two table

  17. 17

    Best way of selecting 8k+ rows from a table

  18. 18

    Using Java, what is the best way to update a column in every row with values from an array? (SQLite)

  19. 19

    Only Selecting Specific Row Info From One Query

  20. 20

    Selecting one row from sqlite database using rawQuery and rowid in Android

  21. 21

    correct way to handle logout from table row

  22. 22

    Best way to add row to HTML table without re-rendering

  23. 23

    Best way verify existence of a row in a table via Protractor?

  24. 24

    Best way to store data in a table row and retrieve via jQuery

  25. 25

    Best way to update a table view after deleting a row with animation?

  26. 26

    Finding one row from pivot table

  27. 27

    Copy a row from one table to another

  28. 28

    Jquery - copy a row from one table to another

  29. 29

    Move table columns from one row into another

HotTag

Archive