-1

I am trying to get all unique rows along with considering one row of multiple rows having almost similar records with one duplicate column in the row

DeptId || DeptNo  ||    DeptDate
==============================
1    ||    111    ||   12-09-2021
2    ||    112    ||   12-09-2021
3    ||    113    ||   12-09-2021
1    ||    111    ||   null
1    ||    111    ||   11-09-2021

Expected result:

DeptId || DeptNo ||     DeptDate
=================================
1    ||    111   ||    null or 12-09-2021 or 11-09-2021 //Consider any but
// only one row should be considered
2    ||    112   ||    12-09-2021
3    ||    113   ||    12-09-2021   
PotatoGod
  • 15
  • 3
  • SQL Server and MySQL are completely different problems. I've removed these conflicting tags; please [edit] your question to (re)tag the (R)DBMS you are really using. – Thom A Dec 21 '22 at 12:08
  • `STRING_AGG`/`GROUP_CONCAT`? – Thom A Dec 21 '22 at 12:09
  • Modified the question, plz check on the problem statement now – PotatoGod Dec 21 '22 at 12:15
  • Probably you are after a simple aggregation `max(DeptDate)` but it's not clear. – Stu Dec 21 '22 at 12:19
  • Does this answer your question? [Comma separated results in SQL](https://stackoverflow.com/questions/18870326/comma-separated-results-in-sql) – Thom A Dec 21 '22 at 12:23

1 Answers1

-1

Try this:

SELECT DeptId,DeptNo  
      ,STRING_AGG(CAST(DeptDate AS VARCHAR(MAX)), '/')
FROM table
GROUP BY DeptId,DeptNo  

or

SELECT DeptId,DeptNo  
      ,MAX(DeptDate)
FROM table
GROUP BY DeptId,DeptNo

if you need only one of the values.

gotqn
  • 42,737
  • 46
  • 157
  • 243