0

我创建了一个名为“捐赠”的表格。
这个表有

supporter_KEY ( member unique identifier)
first_name,
last_name,
donation_key (transaction unique identifier)
email 
amount and date

这是一些数据

supporter_KEY   first_name  last_name   email               donation_key    (date)        (amount)
37519405        ALEX        LANGER      alex@email.com      12447199        2011-04-14    10:38:00.000  100
37519483        Anthony     Russo       anthony@smail3.com  12464169        2012-07-30    14:12:00.000  125
37519656        Bert        Kaplan      sample1@aol.com     12460672        2011-11-25    08:08:00.000  35
37519905        Brett       Graham      sample2@yahoo.com   12466260        2013-01-14    10:43:00.000  100
37519939        Bruce       Decker      sample3@hotmail.com 12466441        2013-03-20    08:59:00.000  25
37520331        Craig       Pettigrew   sample4@aol.com     12464780        2012-08-28    13:52:00.000  25
37520787        Donn        Schaible    sample9@aol.com     12466886        2013-04-09    16:50:00.000  125
37521145        George      Cooper      sample43@gmail.com  12420119        2011-03-09    10:17:00.000  100
37521145        George      Cooper      sample43@gmail.com  12459908        2011-07-19    09:19:00.000  50

我试图在一个查询中提取最新的交易金额和日期,并在另一个查询中提取第一个交易日期和金额

以下是每个查询:
查询首次交易日期和金额:

SELECT supporter_KEY,
   first_name,
   last_name,
   email,
   donation_key,
   MIN(date) as first_transaction_date,
   MIN(Amount) as first_transaction_amount
FROM donation
WHERE supporter_KEY > '1'
GROUP BY supporter_KEY,first_name,last_name,email,donation_key

以及最新交易日期和金额的查询:

SELECT supporter_KEY  ,
   first_name ,
   last_name,
   email,
   donation_key ,
   MAX(date) as latest_transaction_date,
   MAX(Amount) as latest_transaction_amount
WHERE  supporter_KEY > '1'
FROM donation
GROUP BY supporter_KEY,first_name,last_name,email,donation_key

我只是想确保我在正确的轨道上,我还想拉最大的Trx日期和金额(?)。

有人可以帮我吗?

4

1 回答 1

1

查询基本相同。这是第一笔交易的查询,要获取最新交易MIN,请将子查询中的MAX. 请注意,如果有不止一行具有相同的date.

SELECT TOP 1 
    supporter_KEY, 
    first_name, 
    last_name, 
    email, 
    donation_key, 
    date as first_transaction_date, 
    amount as first_transaction_amount
FROM 
    donation 
WHERE 
    date = (SELECT MIN(date) FROM donation)
于 2013-09-18T20:09:44.163 回答