-1

The source table is:

+----------------------------+
| Col A | Col B |    Col C   |
+-------+--------------------+
|  100  |    1  |  1/2/2012  |
|  100  |    2  |  1/2/2012  |
|  100  |    3  |  1/2/2012  |
|  100  |    1  |  5/2/2012  |
|  100  |    2  |  5/2/2012  |
|  100  |    3  |  5/2/2012  |
+-------+-------+------------+

Desired result:

+-----------------------------+
| Col A | Col B  |    Col C   |
+-------+--------+------------+
|  100  |    1   |   5/2/2012 |
|  100  |    2   |   5/2/2012 |
|  100  |    3   |   5/2/2012 |
+-------+--------+------------+

How do we obtain it using SQL?

I tried

SELECT * FROM Table A
GROUP BY Col C
HAVING Col C = max(Col C) AND Col A = '100'

But it does not seem to be working.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rikin
  • 283
  • 3
  • 13
  • 29
  • What [RDBMS](http://en.wikipedia.org/wiki/Relational_database_management_system) you are using? `RDBMS` stands for *Relational Database Management System*. `RDBMS is the basis for SQL`, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, etc... – John Woo May 03 '13 at 16:28
  • I am using SQL Server 2008 – Rikin May 03 '13 at 16:31
  • SELECT * FROM Table A WHERE Col C = "5/2/2012" :P – Willi Mentzel Jul 03 '17 at 17:03

3 Answers3

1

Isn't it like this?

SELECT ColA, ColB, MAX(ColC) ColC
FROM   TableName
GROUP  BY ColA, ColB
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • 1
    You can addition add a `WHERE` to filter only specific value of `ColA` example, `WHERE ColA = '100'` – John Woo May 03 '13 at 16:37
0

http://sqlfiddle.com/#!2/7d6f5/6/0

You can use MAX(). Please see the fiddle above

SELECT a, b, MAX(c) FROM a GROUP BY a, b
into the consideration if this is your schema
CREATE TABLE a( a INT, b INT, c DATE );

Willy Pt
  • 1,795
  • 12
  • 20
  • My suggestion is to post your query from the sql fiddle in your answer. Don't just post a link to the sql fiddle. – Taryn May 03 '13 at 16:36
0

I did this quickly in ms access,

SELECT 
 cola, colb, max(colc)
FROM MyTbl
where cola=100
group by cola,colb

enter image description here

MrSimpleMind
  • 7,890
  • 3
  • 40
  • 45