1

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
2
  • inner join sub query did not work as it just returned the one row with a 3 for bills_per_year. Commented Jan 9, 2013 at 4:21
  • max on just bills_per_year does not work as I have to keep the bill_number in the group by. Commented Jan 9, 2013 at 4:21

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

select SLX_ACCOUNTID, ASSET_NUMBER, PAYMENT_YEAR, BILL_NUMBER, MAX(BILLS_PER_YEAR) OVER(PARTITION BY SLX_ACCOUNTID, ASSET_NUMBER, PAYMENT_YEAR) BILLS_PER_YEAR FROM TABLE;
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.