SQL: aggregate & transpose rows to columns

Hoax

I am trying to transpose the data from the first table to the second.

original data (number of cars and states are limited):

+----+----------+-------+--------+
| id | car      | state | tstamp |
+----+----------+-------+--------+
| 01 | toyota   |   new | 1900   |
| 02 | toyota   |   old | 1950   |
| 03 | toyota   | scrap | 1980   |
| 04 | mercedes |   new | 1990   |
| 05 | mercedes |   old | 2010   |
| 06 | tesla    |   new | 2013   |
+-----+---------------+----------+

query result:

+----------+------+------+-------+
| car      | new  | old  | scrap |
+----------+------+------+-------+
| toyota   | 1900 | 1950 | 1980  |
| mercedes | 1990 | 2010 | null  |
| tesla    | 2013 | null | null  |
+----------+------+------+-------+

My SQL Skills are somewhat rusty therefore I would appreciate any help!

Richard Hansell

Something like this would work, depending on how your data is organised:

SELECT
    car,
    MAX(CASE WHEN state = 'new' THEN tstamp END) AS new,
    MAX(CASE WHEN state = 'old' THEN tstamp END) AS old,
    MAX(CASE WHEN state = 'scrap' THEN tstamp END) AS scrap
FROM
    table
GROUP BY
    car;

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL Server : expand rows and transpose columns

分類Dev

Transpose Rows to Columns with Pandas

分類Dev

Oracle SQL Developer: How to transpose rows to columns using PIVOT function

分類Dev

Transpose specific rows into columns in pandas

分類Dev

awk search then transpose rows to columns

分類Dev

Pivot(transpose) by some columns SQL

分類Dev

Oracle SQL - Generate aggregate rows for certain rows using select

分類Dev

Pivot my sql rows into columns

分類Dev

SQL Pivot with dynamic generated columns, aggregate function and columns without aggregation

分類Dev

SQL aggregate rows with same id , specific value in secondary column

分類Dev

sql dw count(*) with selected columns doesn't aggregate

分類Dev

Combine Multiple Rows And Columns Into A Single Row In SQL

分類Dev

How can I convert rows to columns in SQL

分類Dev

SQL turning rows into columns and populating with values

分類Dev

SQL Server Rows to Multi-Columns

分類Dev

Transpose of certain related columns in R

分類Dev

Faster aggregate multiple columns

分類Dev

SQL Server: Combine multiple rows into one with no overlap but new columns

分類Dev

Trying to find duplicate values in TWO rows and TWO columns - SQL Server

分類Dev

(Beginner SQL) Select rows based on the sum of other columns

分類Dev

SQL - How can i change rows to columns? (PIVOT)

分類Dev

Pivoting rows to columns with custom column names in SQL Server

分類Dev

Split rows and columns in SQL server and insert into temporary table?

分類Dev

pivot\transpose some of the columns in python and saved it to dataframe

分類Dev

transpose columns to row using cross apply

分類Dev

Transpose DF columns based on column values - Pandas

分類Dev

VBA Transpose Table after n columns

分類Dev

Aggregate rows with string values in R

分類Dev

R aggregate by large number of columns

Related 関連記事

  1. 1

    SQL Server : expand rows and transpose columns

  2. 2

    Transpose Rows to Columns with Pandas

  3. 3

    Oracle SQL Developer: How to transpose rows to columns using PIVOT function

  4. 4

    Transpose specific rows into columns in pandas

  5. 5

    awk search then transpose rows to columns

  6. 6

    Pivot(transpose) by some columns SQL

  7. 7

    Oracle SQL - Generate aggregate rows for certain rows using select

  8. 8

    Pivot my sql rows into columns

  9. 9

    SQL Pivot with dynamic generated columns, aggregate function and columns without aggregation

  10. 10

    SQL aggregate rows with same id , specific value in secondary column

  11. 11

    sql dw count(*) with selected columns doesn't aggregate

  12. 12

    Combine Multiple Rows And Columns Into A Single Row In SQL

  13. 13

    How can I convert rows to columns in SQL

  14. 14

    SQL turning rows into columns and populating with values

  15. 15

    SQL Server Rows to Multi-Columns

  16. 16

    Transpose of certain related columns in R

  17. 17

    Faster aggregate multiple columns

  18. 18

    SQL Server: Combine multiple rows into one with no overlap but new columns

  19. 19

    Trying to find duplicate values in TWO rows and TWO columns - SQL Server

  20. 20

    (Beginner SQL) Select rows based on the sum of other columns

  21. 21

    SQL - How can i change rows to columns? (PIVOT)

  22. 22

    Pivoting rows to columns with custom column names in SQL Server

  23. 23

    Split rows and columns in SQL server and insert into temporary table?

  24. 24

    pivot\transpose some of the columns in python and saved it to dataframe

  25. 25

    transpose columns to row using cross apply

  26. 26

    Transpose DF columns based on column values - Pandas

  27. 27

    VBA Transpose Table after n columns

  28. 28

    Aggregate rows with string values in R

  29. 29

    R aggregate by large number of columns

ホットタグ

アーカイブ