Btw, how about if i will use 2 table. the desired result should be the same.thanks.
I modifed the code by Vitaliy and i got a duplicate records.
Create table #sample
(Item nvarchar(35), IdNum nvarchar(35))
Insert into #sample(Item,IdNum ) values('MAT3748','TRP005196603')
Insert into #sample(Item,IdNum) values('MAT3751','TRP005196603')
Insert into #sample(Item,IdNum) values('TX3001710DR','TRP005196603')
Insert into #sample(Item,IdNum) values('FRU300DRD','TRP005207420')
Insert into #sample(Item,IdNum) values('MAT3745','TRP005207420')
Insert into #sample(Item,IdNum) values('MAT3748','TRP005207420')
Insert into #sample(Item,IdNum) values('FRUA300DRD','TRP005207421')
Insert into #sample(Item,IdNum) values('MAT3771','TRP005207421')
Insert into #sample(Item,IdNum) values('MAT3774','TRP005207420')
Insert into #sample(Item,IdNum) values('MAT3824','TRP005207420')
Insert into #sample(Item,IdNum) values('MAT3977','TRP005207420')
Insert into #sample(Item,IdNum) values('CRR3000412iPH5','TRP005224362')
Create table #Sample2
( IdNum nvarchar(35))
Insert into #sample2(IdNum) values('TRP005196603')
Insert into #sample2(IdNum) values('TRP005207420')
Insert into #sample2(IdNum) values('TRP005207421')
Insert into #sample2(IdNum) values('TRP005207420')
Insert into #sample2(IdNum) values('TRP005224362')
Select * ,
isNull((
SELECT CAST(MAX(Case When Item like 'FRUA%' THEN 'FRUA'
When Item like 'FRUB%' THEN 'FRUB'
When Item like 'TX3%' THEN 'TX'
When Item like 'FRU%' THEN 'FRU'
ELSE null END) as Varchar(20)) AS GetType
FROM #sample x WHERE x.IdNum = s.IDNum
GROUP BY IDNum
),'Non-FRU') as REMARKS
from #sample2 s
Inner Join #sample a
On a.IdNum = s.IdNum
order by s.IdNum
No comments:
Post a Comment