T-SQL Pivot table not using IN and values from another column in the pivot data based on MIN() & MAX

markSS

I have a pivot table problem & would love help....

The tutorials I'm following aren't helping as they use an IN statement on a known list of values to create the columns.

It's my IN statement I might be stuck on...I'm trying a subquery, but it's not helping.

SELECT 
    JobDesc, YearExp
FROM
    (SELECT JobDesc, YearExp, Worker 
     FROM Q2) AS SourceTable
PIVOT 
    (MIN(YearExp)
        FOR YearExp IN (SELECT YearExp FROM Q2)
    ) AS PivotTable

The data:

Worker Job Description Years of Experience
1001 Lawyer 6
2002 Lawyer 12
3003 Lawyer 17
4004 Doctor 21
5005 Doctor 9
6006 Doctor 8
7007 Scientist 13
8008 Scientist 2
9009 Scientist 7

The output I'm trying to achieve:

Job Description Most Experienced Least Experienced
Lawyer 3003 1001
Doctor 4004 6006
Scientist 7007 8008
John Cappelletti

The window function row_number() over() in concert with a conditional aggregation should do the trick

Select [Job Description]
      ,[Most]  = max( case when RN1 = 1 then worker end)
      ,[Least] = max( case when RN2 = 1 then worker end)
 From (
        Select * 
              ,RN1 = row_number() over (partition by [Job Description] order by [Years of Experience] desc)
              ,RN2 = row_number() over (partition by [Job Description] order by [Years of Experience] asc)
         from YourTable
      ) A
 Group By [Job Description]

Results

Job Description  Most   Least
Doctor           4004   6006
Lawyer           3003   1001
Scientist        7007   8008

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pivot a table in SQL using a column and finding max and min date

Pivot data from wide to long using column suffix to get table with multiple columns with values (using pivot_longer)

Loop Pivot Table Filter based on values in column

Using Pivot long using values based on column

How to Pivot the data Based on another column value

SQL get value based on corresponding min/max(value) from column in another related table

Pivot data using t sql

Adding another column to the SQL Server pivot table

SQL PIVOT a table based on a column with ID's

Pivot data from multiple column values using R

Laravel - Get values from pivot table column, using pivot tables unique row ID

Can't group on this selection using Dates column from SQL in Pivot Table in Excel

Pivot table to find max, min, avg temp in large data set

Return index value based on MAX of another column, by month, within pivot table

SQL pivot the column values

Pivot and concatenate values from column in SQL Server

MySQL - Pivot row values into column groups based on value of another column

Pivot table data in sql

SQL table data horizontally using PIVOT

SQL Pivot query based on "SUM of a column divided by SUM of another column"

Using pivot and then counting the values in each pivot column

Find count of other column data when give min and max data based on another column in SQL

Loop to filter Pivot Table based on values in another Sheet

EPPlus: Values in column of pivot table

Select MIN, MAX Corresponding column based on another column values

SQL Pivot - adding another column

SQL - BigQuery - Using Group & MAX in several columns - Similar to a pivot table

Pivot table with multiple column in sql?

SQL Pivot Table with Additional Column