Reputation: 11
I could use some help with SQL to solve the following problem.
I have a table that has the following columns and data.
CUSTOMER PAYMENT_YEAR BILLS_PER_YEAR BILL_NUMBER
Chris 2010 1 1
Chris 2010 2 2
Chris 2010 3 3
I would like to return all three rows but with the max bills_per_year value in all 3 rows. It would like like this.
CUSTOMER PAYMENT_YEAR BILLS_PER_YEAR BILL_NUMBER
Chris 2010 3 1
Chris 2010 3 2
Chris 2010 3 3
Upvotes: 1
Views: 1960
Reputation: 18629
Is this you mean?
SELECT
CUSTOMER,
PAYMENT_YEAR,
MAX(BILLS_PER_YEAR) OVER(PARTITION BY CUSTOMER, PAYMENT_YEAR) BILLS_PER_YEAR,
BILL_NUMBER
FROM TableName
Upvotes: 1
Reputation: 37354
Will it work for you?
SELECT CUSTOMER, PAYMENT_YEAR, BILLS_PER_YEAR, BILL_NUMBER,
MAX(BILLS_PER_YEAR) OVER (PARTITION BY CUSTOMER,PAYMENT_YEAR) as max_per_year
FROM table1
Upvotes: 2