I am very new to SQL.
I have a table like this:
| ID | TeamID | UserID | ElementID | PhaseID | Effort |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| 1 | 1 | 1 | 3 | 5 | 6.74 |
| 2 | 1 | 1 | 3 | 6 | 8.25 |
| 3 | 1 | 1 | 4 | 1 | 2.23 |
| 4 | 1 | 1 | 4 | 5 | 6.8 |
| 5 | 1 | 1 | 4 | 6 | 1.5 |
And I was told to get data like this
ElementID | PhaseID1 | PhaseID5 | PhaseID6 |
---|---|---|---|
3 | NULL | 6.74 | 8.25 |
4 | 2.23 | 6.8 | 1.5 |
I understand I need to use PIVOT function. But can't understand it clearly. It would be great help if somebody can explain it in above case.(or any alternatives if any)
7条答案
按热度按时间2skhul331#
A
PIVOT
used to rotate the data from one column into multiple columns.For your example here is a STATIC Pivot meaning you hard code the columns that you want to rotate:
Here is a SQL Demo with a working version.
This can also be done through a dynamic PIVOT where you create the list of columns dynamically and perform the PIVOT.
The results for both:
pcww981p2#
These are the very basic pivot example kindly go through that.
SQL SERVER – PIVOT and UNPIVOT Table Examples
Example from above link for the product table:
renders:
Similar examples can be found in the blog post Pivot tables in SQL Server. A simple sample
3npbholx3#
Ive something to add here which no one mentioned.
The
pivot
function works great when the source has 3 columns: One for theaggregate
, one to spread as columns withfor
, and one as a pivot forrow
distribution. In the product example it'sQTY, CUST, PRODUCT
.However, if you have more columns in the source it will break the results into multiple rows instead of one row per pivot based on unique values per additional column (as
Group By
would do in a simple query).See this example, ive added a timestamp column to the source table:
Now see its impact:
In order to fix this, you can either pull a subquery as a source as everyone has done above - with only 3 columns (this is not always going to work for your scenario, imagine if you need to put a
where
condition for the timestamp).Second solution is to use a
group by
and do a sum of the pivoted column values again.vc6uscn94#
A pivot is used to convert one of the columns in your data set from rows into columns (this is typically referred to as the spreading column). In the example you have given, this means converting the
PhaseID
rows into a set of columns, where there is one column for each distinct value thatPhaseID
can contain - 1, 5 and 6 in this case.These pivoted values are grouped via the
ElementID
column in the example that you have given.Typically you also then need to provide some form of aggregation that gives you the values referenced by the intersection of the spreading value (
PhaseID
) and the grouping value (ElementID
). Although in the example given the aggregation that will be used is unclear, but involves theEffort
column.Once this pivoting is done, the grouping and spreading columns are used to find an aggregation value. Or in your case,
ElementID
andPhaseIDX
lookupEffort
.Using the grouping, spreading, aggregation terminology you will typically see example syntax for a pivot as:
This gives a graphical explanation of how the grouping, spreading and aggregation columns convert from the source to pivoted tables if that helps further.
c7rzv4ha5#
t9eec4r06#
To set Compatibility error
use this before using pivot function
v6ylcynt7#
FOR XML PATH might not work on Microsoft Azure Synapse Serve. A possible alternative, following @Taryn dynamic generated cols approach, same results is obtained by using STRING_AGG .