returning only one row for each value of a column along with other values in different columns

DoIt

I am working on a query on a SQL table which has several columns along with several rows of data and the query returns one row for each unique first and second columns based on the criteria given in the query.

For Example, I have the following table CC

product  term   bid  offer bidcp offercp
AA       sep14  20    10    x   y
AA       Sep14  15     9    p   q  
BA       Sep14  30    15    as  ps
XY       Sep14  25    15    r   t
XY       Oct14   30   20    t    r
XY       Oct14   25   22    p   q

When I run the query on the above table it should return the following data

product  term   bid  offer bidcp offercp
AA       sep14  20    9      x   q(coming from a record which has lowest offer) 
BA       Sep14  30    15      as  ps
XY       Sep14  25    15      r   t
XY       Oct14  30    20      t    r

When I executed the following query it grouped the data in CC even by bidcp and offercp and returned almost all the rows as both offercp and bidcp are unique in one or the other way but I just wanted bidcp and offercp to be where bid and offer are coming from assuming pair of both bid and offer are unique for each product and term

select product,term,max(bid) as bid,min(offer) as offer,bidcp,offercp from canadiancrudes where product like '%/%' group by product,term,bidcp,offercp 

But, when I removed bidcp and offercp from groupby clause it threw me an obvious error

Column 'CC.BidCP' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Is there a better way to fix it?

Tab Alleman

In that case, you need 2 CTEs:

WITH o AS (
      SELECT product,term,offer,offercp, ROW_NUMBER() OVER (PARTITION BY product, term ORDER BY offer ASC) AS rn
      FROM  canadiancrudes where product like '%/%'
)
,   b AS (
      SELECT product,term,bid,bidcp, ROW_NUMBER() OVER (PARTITION BY product, term ORDER BY bid DESC) AS rn
      FROM  canadiancrudes where product like '%/%'
)
    SELECT o.product,o.term,b.bid,o.offer,b.bidcp,o.offercp 
    FROM o
    INNER JOIN b 
      ON o.product=b.product
      AND o.term=b.term
    WHERE o.rn=1
    AND b.rn=1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Merge Row values from different columns to one column on top of each other: MySQL

For each row check if value in one column exists in two other columns

Sort each row absolute value independent of columns along with column names

Use values in different rows of a column as values in different columns but only one row

How to convert rows that contain same value for one column but different for other columns into one single row using R?

How to select only one row for each unique id, with other columns having certain condition value

Pivoting row values in one column based on values in two other columns

Check if values are the same for each row over criteria in different other columns

Delete rows with duplicate value in one column but different value in other columns

Creating DataGrid tooltips for only one column (different tooltip for each row)

Filling each row of one column of a DataFrame with different values (a random distribution)

Replicating value from one column into other columns based on row content

Comparing the value of a row in a certain column to the values in other columns

Changing values in each row of a column based on values in other columns of the corresponding row (Python/Pandas)

add one column to table with value against the Other Columns Specific Values

How to compare one column's value to multiple values in other columns?

Values of one column in different columns

Selecting rows where values in one column are different, other column(s) values are similar and values of Date columns are correct

R : how to create multiple new values in one column and repeat line for each new value, using data in other columns?

SQL Server 2000 - Returning a single column value from a 4 column table based on the values of the other 3 columns

SQL outer join on two columns, returning null in one column if only the other matches

Create One Column from Six different Columns Where all the columns for a row contain a null value except for one

How to insert a row having only one column different than the other row in the same table?

Extracting Max value along each row from strings in column

Searching for values from one dataframe in another and returning information in the corresponding row/different column

How to select rows with same values in one column but all must be different between each other in another column

SQL server query to find values grouped by one column but different in at least one of other columns

How to get data by mutiple values on one column with different values on other columns from database in Entity Framework?

Combine multiple categorical columns into one, when each row has only one non-NaN value, in Pandas

TOP Ranking

HotTag

Archive