2014-01-09 99 views
1

從以下兩個表格案例和acct_transaction中給出以下數據,如何才能包含最大acct_transaction金額的acct_transaction.create_date,同時還計算所有金額和值的總和最大的金額?平臺是t-sql。根據另一列的max()選擇列

id amount  create_date 
---|----------|------------| 
1 | 1.99  | 01/09/2009 | 
1 | 2.99  | 01/13/2009 | 
1 | 578.23 | 11/03/2007 | 
1 | 64.57 | 03/03/2008 | 
1 | 3.99  | 12/12/2012 | 
1 | 31337.00 | 04/18/2009 | 
1 | 123.45 | 05/12/2008 | 
1 | 987.65 | 10/10/2010 | 

結果集應該是這樣的:

id amount  create_date sum  max_amount max_amount_date 
---|----------|------------|----------|-----------|----------- 
1 | 1.99  | 01/09/2009 | 33099.87 | 31337.00 | 04/18/2009 
1 | 2.99  | 01/13/2009 | 33099.87 | 31337.00 | 04/18/2009 
1 | 578.23 | 11/03/2007 | 33099.87 | 31337.00 | 04/18/2009 
1 | 64.57 | 03/03/2008 | 33099.87 | 31337.00 | 04/18/2009 
1 | 3.99  | 12/12/2012 | 33099.87 | 31337.00 | 04/18/2009 
1 | 31337.00 | 04/18/2009 | 33099.87 | 31337.00 | 04/18/2009 
1 | 123.45 | 05/12/2008 | 33099.87 | 31337.00 | 04/18/2009 
1 | 987.65 | 10/10/2010 | 33099.87 | 31337.00 | 04/18/2009 

這是我到目前爲止,我只是不知道怎麼拉的max_amount_date列最大acct_transaction量的日期。

SELECT  cases.id, acct_transaction.amount, acct_transaction.create_date AS 'create_date', SUM(acct_transaction.amount) OVER() AS 'sum', MIN(acct_transaction.amount) OVER() AS 'max_amount' 
FROM  cases INNER JOIN 
      acct_transaction ON cases.id = acct_transaction.id 
WHERE  (cases.id = '1') 
+1

哪個RDBMS你使用的是什麼? – Bohemian

+0

t-sql on ms sql server 2008 r2 – phoeneous

回答

0

謝謝,這讓我在正確的軌道上這個這是工作:

,CAST((SELECT TOP 1 t2.create_date from acct_transaction t2 
      WHERE t2.case_sk = act.case_sk AND (t2.trans_type = 'F') 
      order by t2.amount, t2.create_date DESC) AS date) AS 'max_date' 

它不會讓我給予好評,因爲我有小於15代表:(

2

你可以做一個完整的外部聯接定義的聚集表:

select id, amount, create_date, x.sum, x.max_amount, x.max_amount_date 
from table1 
full outer join 
(select sum(amount) as sum, max(amount) as max_amount, 
    (select top 1 create_date from table1 where amount = (select max(amount) from table1)) as max_amount_date 
from table1) x 
on 1 = 1 

SQL Fiddle demo

+0

嵌套select中的table1是否應該讀取table2?有兩個表,case和acct_transaction – phoeneous

4
;WITH x AS 
(
    SELECT c.id, t.amount, t.create_date, 
    s = SUM(t.amount) OVER(), 
    m = MAX(t.amount) OVER(), 
    rn = ROW_NUMBER() OVER(ORDER BY t.amount DESC) 
    FROM dbo.cases AS c 
    INNER JOIN dbo.acct_transaction AS t 
    ON c.id = t.id 
) 
SELECT x.id, x.amount, x.create_date, 
    [sum] = y.s, 
    max_amount = y.m, 
    max_amount_date = y.create_date 
FROM x CROSS JOIN x AS y WHERE y.rn = 1; 
+0

謝謝,請給出這個答案 – phoeneous

+0

如何處理與兩張桌子,案件和acct_transaction。只有案件的列是id,其餘是acct_transaction。 – phoeneous

+0

@phoeneous你嘗試過什麼嗎?爲什麼地球上的案件ID與事務ID具有相同的含義?這非常混亂。 –

1

嘗試查詢該憎惡......我沒有做聲稱其速度或優雅。我很可能應該祈禱科德憐憫我的靈魂。

下面是您提到的兩個表上的聯接輸出,但您不提供模式。

[SQL Fiddle][1] 

SELECT A.case_id 
,A.trans_id 
,A.trans_amount 
,A.trans_create_date 
,A.trans_type 
,B.max_amount 
,B.max_amount_date 
,E.sum_amount 
FROM acct_transaction AS A 
INNER JOIN (select C.case_id 
,MAX(C.trans_amount) AS max_amount 
,C.trans_create_date AS max_amount_date 
FROM acct_transaction AS C group by C.case_id, C.trans_create_date) AS B ON B.case_id = A.case_id 
inner JOIN (select D.case_id, SUM(D.trans_amount) AS sum_amount FROM acct_transaction AS D GROUP BY D.case_id) AS E on E.case_id = A.case_id 
WHERE (A.case_id = '1') AND (A.trans_type = 'F') 
GROUP BY A.case_id 
相關問題