重複のある2つの列があるテーブルがあります。
id name classname description
-----------------------------
1 a aa aa:abcd
2 a Unknown Unknown
3 b bb unknown
4 c cc abcd
ここで、すべての重複を除外する必要がある選択クエリがあり、説明が識別子として表示されます。結果は次のようになります。
id name identifier
-----------------
1 a aa
2 b NULL
3 c NULL
ここで、char インデックスとして「:」がないすべての説明は、NULL として表示されるか、不明として Null として表示されます。
以下の選択クエリを使用して「名前」列の重複をフィルタリングしていますが、説明をトリミングするために結果を取得するためにケースを使用しているため、説明に同じクエリを使用できません 'aa: abcd ' へ
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
そして私が得ている結果は
TagID タグ名 発信元契約者 システム システム説明 TagIdentifier
2609005 ZIH-210053 現代重工業 (トップサイド) 210 スラグキャッチャー NULL 2609005 ZIH-210053 現代重工業 (トップサイド) 210 スラグキャッチャー ZIH
タグ識別子がnullで、重複がない行もあります