Chris Bittakis
Chris Bittakis

Reputation: 11

SQL Server - Return Max Value In One Column for All Rows

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

Answers (2)

TechDo
TechDo

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

a1ex07
a1ex07

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

Related Questions