-4

I have 5 columns in sql table and I need all the 5 columns as out put but with distinct operation on three columns

Need to return all the columns with distinct operation on three column

How to achive this?

Timestamp                   Name    State     Context
2013-06-24 11:51:03.2550000 tst1    Started     E1
2013-06-24 11:56:03.2550000 tst1    Completed   E1
2013-06-24 11:51:03.2550000 tst1    Started     E1
2013-06-24 11:56:03.2550000 tst1    Completed   E1
2013-06-24 11:45:03.2550000 tst1    Started     E1
2013-06-24 11:50:03.2550000 tst1    Completed   E1
2013-06-24 11:45:03.2550000 tst1    Started     E1
2013-06-24 11:50:03.2550000 tst1    Completed   E1

Here I am getting all the distinct result for above table by applying distinct on three columns using below query. But I need the distinct of these three columns also need to select context column without applying distict on Context column

SELECT DISTINCT Timestamp,Name,State FROM TableName group by Timestamp,Name,State

Rephrasing my question :

I need to select unique columns from above table . Here only unquie column selection considered as Timestamp,Name,State

Timestamp                   Name    State     Context
2013-06-24 11:51:03.2550000 tst1    Started     E1
2013-06-24 11:56:03.2550000 tst1    Completed   E1
2013-06-24 11:45:03.2550000 tst1    Started E1
2013-06-24 11:50:03.2550000 tst1    Completed   E1
usr021986
  • 3,421
  • 14
  • 53
  • 64

2 Answers2

7

Can group by 3 but must use some type of aggregate function like min or max on the other two

select col1, col2, col3, max(col4), min(col5) 
from tbl 
group by col1, col2, col3
paparazzo
  • 44,497
  • 23
  • 105
  • 176
-2

You can use a groupby for the three columns you want 'distinct'.

Alicia
  • 1,152
  • 1
  • 23
  • 41