我必须找到每个帐户的前1行effective_date
account dt_effective
-------------------------
1 14-04-2012
1 15-04-2012
2 13-02-2011
2 11-03-2012
3 1-01-2010
3 02-02-2010
3 05-01-2012
答案必须是:
account dt_effective
-------------------------
1 14-04-2012
2 13-02-2011
3 1-01-2010
你可以帮我查询一下吗?
更新了问题
答案 0 :(得分:1)
你可以尝试
SELECT account, MIN(dt_effective) FROM your_table
GROUP BY account
答案 1 :(得分:1)
SELECT account, MAX(dt_effective)
FROM [table]
GROUP BY account
答案 2 :(得分:1)
SELECT account, MAX(dt_effective)
FROM yourTableName
GROUP BY account
如果您需要输入第一个生效日期,只需将max更改为min
SELECT account, MIN(dt_effective)
FROM yourTableName
GROUP BY account
答案 3 :(得分:0)
试试这个:
select account, MAX(dt_effective) from table group by account;
答案 4 :(得分:0)
我尝试了下面提到的查询并按预期工作。
DECLARE @T TABLE(ACCOUNTID INT, ACCOUNTDATE DATETIME)
INSERT INTO @T
VALUES(1, CONVERT(DATE,'14-04-2012',105))
INSERT INTO @T
VALUES(1, CONVERT(DATE,'15-04-2012',105))
INSERT INTO @T
VALUES(2, CONVERT(DATE,'13-02-2011',105))
INSERT INTO @T
VALUES(2, CONVERT(DATE,'11-03-2012',105))
INSERT INTO @T
VALUES(3, CONVERT(DATE,'1-01-2010',105))
INSERT INTO @T
VALUES(3, CONVERT(DATE,'02-02-2010',105))
INSERT INTO @T
VALUES(3, CONVERT(DATE,'05-01-2012',105))
SELECT * FROM @T
SELECT ACCOUNTID, MAX(ACCOUNTDATE) FROM @T
GROUP BY ACCOUNTID