我似乎无法想出这个。我有一个交易表(很多),有日期,客户ID等。
我想在特定日期选择当天每个客户的第一笔和最后一笔交易的日期时间,如下所示:
custID FirstT LastT
318 08:05 18:35
968 03:21 13:54
488 12:34 14:28
SQL Server 2008.听起来很简单,但我已经准备好了...请各位帮忙..
答案 0 :(得分:1)
SELECT
custID,
MIN(transaction_date) AS FirstT,
MAX(transaction_date) AS LastT
FROM
yourTable
WHERE
transaction_date >= '2014-04-01'
AND transaction_date < '2014-04-02'
GROUP BY
custID
答案 1 :(得分:0)
试试这个:
SELECT custid, DATEADD(dd, 0, DATEDIFF(dd, 0, date)) Date, MIN(date) FirstT, MAX(date) LastT
FROM transactions
GROUP BY custid, DATEADD(dd, 0, DATEDIFF(dd, 0, date))
答案 2 :(得分:0)
不知道你的架构,但你应该做这样的事情:
select custID, max(transactionDate), min(transactionDate)
where cast(transactionDate as date) = '20140101'
group by custID
答案 3 :(得分:0)
SELECT * FROM
(SELECT custID, transaction_date FROM transactions ORDER BY transaction_date LIMIT 1) t
UNION
SELECT * FROM
(SELECT custID, transaction_date FROM transactions ORDER BY transaction_date DESC LIMIT 1) t1