I am trying to export an SQL query to Excel, and I need this behaviour but I can't find in anywhere.
This is what I currently have:
SELECT t1.c1, t2.c2
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id;
|c1| c2 |
----------------
|a | sometext1 |
|a | sometext2 |
|a | sometext3 |
|a | sometext4 |
|b | sometext5 |
|b | sometext6 |
|b | sometext7 |
I want to show the results like this:
|c1| c2 |
|a | sometext1 |
| | sometext2 |
| | sometext3 |
| | sometext4 |
|b | sometext5 |
| | sometext6 |
| | sometext7 |
I want to show only the first item in each group, and hide the rest so it's not shown in Excel. I am using SQL Server.
You should really do this type of processing in the application, not the database. Why? Because result sets represent unordered sets. Relying on the ordering to understand data makes the results brittle.
But you can. One method is:
select (case when seqnum = 1 then c1 end) as c1, c2
from (select, t.*,
row_number() over (partition by c1 order by (select null)) as seqnum
from t
) t
order by c1, seqnum;