Oracle - produce unique rows for each unique column value and convert rows to columns

squashbuff

Oracle 11g R2 is in use. This is my source table:

ASSETNUM    WONUM   WODATE     TYPE1    TYPE2   LOCATION
--------------------------------------------------------
W1          1001    2015-10-10  N       N       loc1
W1          1002    2015-10-02  Y       N       loc2
W1          1003    2015-10-04  Y       N       loc2
W1          1004    2015-10-05  N       Y       loc2
W1          1005    2015-10-07  N       Y       loc2
W2          2001    2015-10-11  N       N       loc1
W2          2002    2015-10-03  Y       N       loc2
W2          2003    2015-10-02  Y       N       loc2
W2          2004    2015-10-08  N       Y       loc3
W2          2005    2015-10-06  N       Y       loc3

http://sqlfiddle.com/#!4/8ee297/1

I want to write a query to get following data:

ASSETNUM    LATEST      LOCATION for   LATEST_WODATE_FOR   LATEST_WODATE_FOR    
            WODATE      LATEST WODATE   TYPE1=Y             TYPE2=Y 
----------------------------------------------------------------------------
W1          2015-10-10   loc1          2015-10-04          2015-10-07
W2          2015-10-11   loc1          2015-10-03          2015-10-08

I need a similar resultset with only one row for each unique value in ASSETNUM.

Any help would be appreciated!

Justin Cave

Analytic functions to the rescue.

http://sqlfiddle.com/#!4/8ee297/4

select assetnum,
       wodate,
       wonum,
       location,
       last_type1_wodate,
       last_type2_wodate
 from(select assetnum,
             wodate,
             wonum,
             location,
             rank() over (partition by assetnum order by wodate desc) rnk_wodate,
             max(case when type1 = 'Y' then wodate else null end) 
               over (partition by assetnum) last_type1_wodate,
             max(case when type2 = 'Y' then wodate else null end)
               over (partition by assetnum) last_type2_wodate
        from t)
   where rnk_wodate = 1

Walking through what that's doing

  • rank() over (partition by assetnum order by wodate desc) takes all the rows for a particular assetnum and sorts them by wodate. The predicate on the outside where rnk_wodate = 1 returns just the most recent row. If there can be ties, you may want to use dense_rank or row_number in place of rank depending on how you want ties to be handled.
  • max(case when type1 = 'Y' then wodate else null end) over (partition by assetnum) takes all the rows for a particular assetnum and finds the value that maximizes the case expression. That will be the last row where type1 = 'Y' for that assetnum.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert column values to rows for each unique value in a dataframe in R?

Summarizing rows on unique value, except time column

Grouping unique rows from max column value

Extract all rows containing second unique value for each column by group

Python numpy: Efficiently get rows containing min value of column for each unique tuple of 3 other columns

Select rows with unique value in a column, ordered by Timestamp?

How to select unique rows by certain columns and for each date in SQL?

Produce unique columns for each unique values in a column

Python Pandas - filter pandas dataframe to get rows with minimum values in one column for each unique value in another column

Pandas Calculation Grouped By Unique Rows of Column Value

Select the X newest rows for each unique value of a column

Unique Rows based on Column

For each unique value of some groupid column, how do I get rows with last 3 dates?

[SQL]: Unique subsets of rows where a column value has each of a set of values

Counting occurrence of unique rows and produce summary in table

Pandas: create rows for each unique value of a column, even with missing data

MySQL get n percent of rows matching each unique column value

Move Duplicate Rows Unique Value to New Column

How to create new date rows for each unique value in column?

Select Rows with unique column value in SQL

How do I convert multiple columns to individual rows/values if there is no other unique column in pandas?

Select rows with Max(Column Value) for each unique combination of two other columns

Pandas Dataframe duplicate rows with mean-based on the unique value in one column and so that each unique value have same number of rows

multiplying group of columns for each unique variant in a column and fill all rows of the columns with that value

assigning unique value to new column based on multiple columns/rows

Create n rows up to highest value in column for each unique client

Conditionally drop rows if each unique value in a column appears less than n times

Select rows where the combination of two columns is unique and we only display rows where the first column is not unique

List values in all rows and columns based on all unique values in another column to find 80th percentile value for each unique value