How to combine multiple rows into one row and multiple column in SQL Server?

umer

I have different tables through I made temp table and here is the result set of temp table:

     car_id  | car_type | status  | count   
     --------+----------+---------+------
     100421  |  1       |   1     |   9   
     100421  |  1       |   2     |   8   
     100421  |  1       |   3     |   3 
     100421  |  2       |   1     |   6   
     100421  |  2       |   2     |   8   
     100421  |  2       |   3     |   3 
     100422  |  1       |   1     |   5
     100422  |  1       |   2     |   8   
     100422  |  1       |   3     |   7

Here is the meaning of status column:

  • 1 as sale
  • 2 as purchase
  • 3 as return

Now I want to show this result set as below

   car_id  | car_type | sale | purchase | return
   --------+----------+------+----------+----------
    100421 |  1       |  9   |   8      |  3
    100421 |  2       |  6   |   8      |  3
    100422 |  1       |  5   |   8      |  7

I tried but unable to generate this result set. Can anyone help?

Eid Morsy

Try this

select car_id  ,car_type, [1]  as Sale,[2] as Purchase,[3] as [return]
from (select car_id  , car_type ,  [status] ,[count] from tempTable)d
pivot(sum([count]) for [status] in([1],[2],[3]) ) as pvt

also you can remove the subquery if you don't have any condition like

select car_id  ,car_type, [1]  as Sale,[2] as Purchase,[3] as [return]
from tempTable d
pivot(sum([count]) for [status] in([1],[2],[3]) ) as pvt

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

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

編集
0

コメントを追加

0

関連記事

分類Dev

Combine Multiple Rows into One Row (One Column) when Using SQL

分類Dev

How to combine multiple rows into one rows in SQL

分類Dev

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

分類Dev

Combine Multiple Rows And Columns Into A Single Row In SQL

分類Dev

Merge multiple rows into one row using SQL Server?

分類Dev

How to combine multiple rows in a pandas dataframe which have only 1 non-null entry per column into one row?

分類Dev

Combine multiple rows in a single row by descending order SQL

分類Dev

Multiple rows to one column

分類Dev

SQL - Multiple Rows Into One

分類Dev

Convert one column to multiple rows

分類Dev

Merge multiple columns into one column with multiple rows

分類Dev

SQL Server: Select rows with multiple occurrences of regex match in a column

分類Dev

How to reformat an R data frame with multiple rows into one row

分類Dev

How to add multiple rows with the same value and ID in one row

分類Dev

SQL Combining multiple rows into one

分類Dev

collating multiple rows of a column in a panda to one row while maintaining the data type of the column

分類Dev

How to create multiple column in an one row UITableViewCell in swift?

分類Dev

Condense multiple rows into one sum row

分類Dev

How to insert multiple rows into SQL Server Parallel Data Warehouse table

分類Dev

remove duplicate entries in one column and linearize the values in multiple rows to a single row

分類Dev

Combine Multiple Regex into One

分類Dev

Combine multiple JPEGs into one

分類Dev

View comma separated values of one column in multiple rows or in multiple values

分類Dev

Combine multiple columns of pandas data frame to one column

分類Dev

how to select multiple same column in one column

分類Dev

Retrieve multiple status into one row based on multiple conditions SQL 2008

分類Dev

Android Room: How to combine data from multiple SQL queries into one ViewModel

分類Dev

sql - how to select multiple columns with only one distinct column from joining multiple tables

分類Dev

SQL - How to combine rows

Related 関連記事

  1. 1

    Combine Multiple Rows into One Row (One Column) when Using SQL

  2. 2

    How to combine multiple rows into one rows in SQL

  3. 3

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

  4. 4

    Combine Multiple Rows And Columns Into A Single Row In SQL

  5. 5

    Merge multiple rows into one row using SQL Server?

  6. 6

    How to combine multiple rows in a pandas dataframe which have only 1 non-null entry per column into one row?

  7. 7

    Combine multiple rows in a single row by descending order SQL

  8. 8

    Multiple rows to one column

  9. 9

    SQL - Multiple Rows Into One

  10. 10

    Convert one column to multiple rows

  11. 11

    Merge multiple columns into one column with multiple rows

  12. 12

    SQL Server: Select rows with multiple occurrences of regex match in a column

  13. 13

    How to reformat an R data frame with multiple rows into one row

  14. 14

    How to add multiple rows with the same value and ID in one row

  15. 15

    SQL Combining multiple rows into one

  16. 16

    collating multiple rows of a column in a panda to one row while maintaining the data type of the column

  17. 17

    How to create multiple column in an one row UITableViewCell in swift?

  18. 18

    Condense multiple rows into one sum row

  19. 19

    How to insert multiple rows into SQL Server Parallel Data Warehouse table

  20. 20

    remove duplicate entries in one column and linearize the values in multiple rows to a single row

  21. 21

    Combine Multiple Regex into One

  22. 22

    Combine multiple JPEGs into one

  23. 23

    View comma separated values of one column in multiple rows or in multiple values

  24. 24

    Combine multiple columns of pandas data frame to one column

  25. 25

    how to select multiple same column in one column

  26. 26

    Retrieve multiple status into one row based on multiple conditions SQL 2008

  27. 27

    Android Room: How to combine data from multiple SQL queries into one ViewModel

  28. 28

    sql - how to select multiple columns with only one distinct column from joining multiple tables

  29. 29

    SQL - How to combine rows

ホットタグ

アーカイブ