Question

I have a table in which there are two columns with duplicates.

id name  classname description 
-----------------------------
1  a     aa        aa:abcd  
2  a     Unknown   Unknown 
3  b     bb        unknown 
4  c     cc        abcd 

Now I have a select query where in I have to filter out all the duplicates and my description is shown as identifier, my result should be like this,

id name identifier
-----------------
1  a   aa
2  b   NULL 
3  c   NULL

where all the description having either without ':' as its char index should display as NULL or Unknown as Null.

I'm using the below select query to filter the duplicates in the 'name' column , but I'm unable to use the same query for description as I'm using case for obtaining result in order to trim my description 'aa: abcd' to aa

    select distinct 
           id,
           (select top 1 name 
             from table1 t 
            where t.name = t1.name 
            order by case t1.classname 
                        when 'Unknown Tag Class' then 0 
                        else 1 
                     end
            ) name,
            (case when charindex(':',Description)> 0
               then substring(Description,1,(charindex(':',Description)-1)) 
            end
            ) as Identifier
    from table1  t1

In the above query I want to modify the case statement of description so that i can filter duplicates and also trim the values like "aa:abcd" to "aa" and put them in identifier column.

Need help on this.


this is the query i am using 

IF  EXISTS (SELECT * FROM sys.objects     WHERE object_id = OBJECT_ID(N'[dbo].[EXEC_REP_TransposedTagAttributes]')
AND type in (N'U'))
BEGIN
   select distinct 
      [Att : 42674] as TagID
       ,Tagname
       ,isnull([Att : 14591],'-') as OriginatingContractor
       ,isnull([Att : 14594],'-') as System
      ,(case when charindex(':',TargetName)> 0 then 
    substring(TargetName,(charindex(':',TargetName)+1),len(TargetName)) 
   end) as SystemDescription
,(case when charindex(':',TagClassDescription)> 0 then 
    substring(TagClassDescription,1,(charindex(':',TagClassDescription)-1)) 
   end) as TagIdentifier
from EXEC_REP_TransposedTagAttributes t1
LEFT JOIN (SELECT SourceName, TargetName FROM EXEC_REP_Associations WHERE AssociationType = '3' and TargetClassName = 'SUB SYSTEM') b ON TagName = b.SourceName
where tagname='ZIH-210053' Order by [Att : 42674]
END
ELSE
 select 'Reporting Database is being refreshed, please wait.' as errMsg

and the result i am geting is

TagID Tagname OriginatingContractor System SystemDescription TagIdentifier

2609005 ZIH-210053 Hyundai Heavy Industries (Topsides) 210 Slugcatcher NULL 2609005 ZIH-210053 Hyundai Heavy Industries (Topsides) 210 Slugcatcher ZIH

there are also rows which have tag identifier as null and donot have duplicates

Was it helpful?

Solution

select id,name,
       CASE WHEN charindex(':',description)>0
         THEN LEFT(description,charindex(':',description)-1) 
         ELSE NULL
       END as identifier

from t as t1
where description like '%:%'
      or NOT EXISTS (select * from t where t.id<>t1.id and t.name=t1.name);

SQLFiddle demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top