我有逐月输入的记录...以下是当前查询的结果,即拉动整年的结果。请注意,这是一个Oracle数据库。
我的查询:
SELECT UMEMBERID, FIRSTNAME, LASTNAME, BIRTHDATE, PUBLICATIONDT
FROM MY_TABLE
WHERE INDENTIFIER IN ('GROUP1')
AND PUBLICATIONDT >= DATE'2014-01-01'
AND PUBLICATIONDT <= DATE'2014-12-31'
实际返回结果:
UMEMBERID | FIRSTNAME | LASTNAME | BIRTHDATE | PUBLICATIONDT
--------------------------------------------------------------
1 | John | Smith | 6/26/1983 | 11/20/2014
2 | Jane | Smith | 2/2/1980 | 11/21/2014
3 | Roger | Rabbit | 8/3/1982 | 11/20/2014
4 | Bugs | Bunny | 6/20/1983 | 11/19/2014
5 | Daffy | Duck | 4/3/1982 | 11/17/2014
6 | Winnie | Pooh | 2/27/1989 | 12/15/2014
7 | Jane | Doe | 8/5/1959 | 12/11/2014
5 | Daffy | Duck | 4/3/1982 | 12/9/2014
3 | Roger | Rabbit | 8/3/1982 | 12/9/2014
10 | John | Doe | 8/9/1992 | 12/18/2014
我的预期结果如下:
UMEMBERID | FIRSTNAME | LASTNAME | BIRTHDATE | PUBLICATIONDT
--------------------------------------------------------------
1 | John | Smith | 6/26/1983 | 11/20/2014
2 | Jane | Smith | 2/2/1980 | 11/21/2014
3 | Roger | Rabbit | 8/3/1982 | 12/09/2014
4 | Bugs | Bunny | 6/20/1983 | 11/19/2014
5 | Daffy | Duck | 4/3/1982 | 12/09/2014
6 | Winnie | Pooh | 2/27/1989 | 12/15/2014
7 | Jane | Doe | 8/5/1959 | 12/11/2014
10 | John | Doe | 8/9/1992 | 12/18/2014
如果您注意到实际返回的结果,它会为每个Roger Rabbit(11/20/2014
和12/09/2014
)和Daffy Duck(11/17/2014
和{{1}获取额外的记录})。在我的预期结果中,我只想获取12/09/2014
日期并显示8条记录。
答案 0 :(得分:0)
尝试这样的事情:
SELECT UMEMBERID, FIRSTNAME, LASTNAME, BIRTHDATE, MAX(PUBLICATIONDT)
FROM MY_TABLE
WHERE INDENTIFIER IN ('GROUP1')
AND PUBLICATIONDT >= DATE'2014-01-01'
AND PUBLICATIONDT <= DATE'2014-12-31'
GROUP BY UMEMBERID, FIRSTNAME, LASTNAME, BIRTHDATE;