Question: I have a table with the following data and trying to group by the, but not getting the data as I want . 

```
create table #test 
( 
     id int, 
     value int, 
     code int, 
     info varchar(20)  
)

insert into #test
values (1234, 7666, 1, '1stfail'),
       (1234, 7665, 2, '2ndfail'),
       (1234, 7664, 3, '3rdfail'),
       (2434, 8777, 1, '1stfail')

select id, value, code, min(info)  
from #test  

```

I am getting result as below

[![enter image description here](https://i.stack.imgur.com/etVZm.png)](https://i.stack.imgur.com/etVZm.png)

I want result like this

[![enter image description here](https://i.stack.imgur.com/cRYna.png)](https://i.stack.imgur.com/cRYna.png)

Answer: 
You could use a `CASE` and only output the ID when `row_number()` equals 1.

```
SELECT CASE
         WHEN row_number() OVER (PARTITION BY id
                                 ORDER BY code) = 1 THEN
           id
       END id,
       value,
       code,
       info 
       FROM #test;

```

[db<>fiddle](https://dbfiddle.uk/?rdbms=sqlserver_2012&fiddle=c4eac2b279af3f7c9aacb4be18687f40)
you need to use LAG in order to achive this. LAG advances to next row to get value of column.

```
select 
  case when idNew = id then null else id end idCol, 
  value, code, info 
from (
   SELECT id, LAG(id) OVER(Order by id) idNew, code, value, info from #test
) as t

```