0

我必须找到每个帐户的第一行有效日期

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

你能帮我查询一下吗?

更新了问题

4

5 回答 5

1

你可以试试

SELECT account, MIN(dt_effective) FROM your_table
GROUP BY account
于 2012-04-17T07:46:24.507 回答
1
SELECT account, MAX(dt_effective)
FROM [table]
GROUP BY account
于 2012-04-17T07:46:47.347 回答
1
SELECT account, MAX(dt_effective)
FROM yourTableName
GROUP BY account

如果您需要输入的第一个生效日期,只需将 max 更改为 min

SELECT account, MIN(dt_effective)
FROM yourTableName
GROUP BY account
于 2012-04-17T07:49:06.853 回答
0

尝试这个:

select account, MAX(dt_effective) from table group by account;
于 2012-04-17T07:47:08.737 回答
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
于 2012-04-17T07:58:04.880 回答