2013-01-09 65 views
0

我可以使用一些SQL幫助來解決以下問題。SQL Server - 爲所有行在一列中返回最大值

我有一個表格,其中包含以下列和數據。

CUSTOMER PAYMENT_YEAR BILLS_PER_YEAR BILL_NUMBER 
Chris 2010   1    1 
Chris 2010   2    2 
Chris 2010   3    3 

我想返回所有三行,但在所有3行中使用max bills_per_year值。 它想這樣。

CUSTOMER PAYMENT_YEAR BILLS_PER_YEAR BILL_NUMBER 
Chris  2010   3    1 
Chris  2010   3    2 
Chris  2010   3    3 
+0

內部聯接子查詢沒有工作,因爲它只是返回一個3爲bills_per_year行。 –

+0

最大隻是bills_per_year不起作用,因爲我必須保持組中的bill_number。 –

回答

2

它適合你嗎?

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 
+0

選擇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; –

+0

這工作!!!!非常感謝大家。 –

1

是你指的嗎?

SELECT 
    CUSTOMER, 
    PAYMENT_YEAR, 
    MAX(BILLS_PER_YEAR) OVER(PARTITION BY CUSTOMER, PAYMENT_YEAR) BILLS_PER_YEAR, 
    BILL_NUMBER 
FROM TableName 
相關問題