Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Don't show duplicates

Tags:

sql

sql-server

I want to not show duplicate rows in output for the sql below and so I have 'distinct' in there, but this doesn't work. I get output like:

PermitNumber PermitName CreatedOn  
111          ABCD       1/2/2011  
111          ABCD       3/4/2012  
222          DFES       3/6/2000  

and I want only one row with 111 but I get more than 1 row because 111 has more than one modification, but I don't care if it has 1 or 1000.

select  distinct (dbo.PermitNumber(mp.PermitId)), 
        dbo.PermitName(mp.PermitId),
    mod.[CreatedOn] as [CreatedOn]
from    tblPermit mp, dbo.[tblModification] mod
where mod.PermitId = mp.PermitId
order by 1

Using SQL Server

like image 859
numberwang Avatar asked Apr 27 '26 06:04

numberwang


1 Answers

Distinct applies to all columns so you could use an aggregate function:

  select  mp.PermitNumber,
    mp.PermitName,
    max(mod.[CreatedOn])as [CreatedOn]
  from    tblPermit mp
  Inner join dbo.[tblModification] mod
      on mod.PermitId = mp.PermitId
  Group by mp.PermitNumber,
    mp.PermitName
  order by 1
like image 89
Taryn Avatar answered Apr 28 '26 18:04

Taryn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!