SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sqlfresher2k7
Aged Yak Warrior

583 Posts

Posted - 06/15/2012 :  14:38:08  Show Profile  Reply with Quote


I need a query which should validate when there is child update status with B and update the status with A parent.

Table:Masterip                         
------------------------------   
5.46.200.1.462222.2.1.1.1.1.1    
5.46.200.1.462222.2.1.1.1.1.1.1  
5.46.200.1.462222.2.1.1.1.1.1.2  
5.46.200.1.462222.2.1.1.1.1.1.3  
5.46.200.1.462222.2.1.1.1.1.1.4  
5.46.200.1.462222.2.1.1.1.1.5    
5.46.200.1.462222.2.1.1.1.1.5.1 
5.46.200.1.462222.2.1.1.1.1.6   
 


Expected output

Masterip                              status
------------------------------   -------------
5.46.200.1.462222.2.1.1.1.1.1         A
5.46.200.1.462222.2.1.1.1.1.1.1       B
5.46.200.1.462222.2.1.1.1.1.1.2       B
5.46.200.1.462222.2.1.1.1.1.1.3       B
5.46.200.1.462222.2.1.1.1.1.1.4       B
5.46.200.1.462222.2.1.1.1.1.5         A
5.46.200.1.462222.2.1.1.1.1.5.1       B 
5.46.200.1.462222.2.1.1.1.1.6         A

Thanks for you help in advance !

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 06/15/2012 :  16:10:04  Show Profile  Reply with Quote

UPDATE t
SET t.status= CASE WHEN Cnt > 0 THEN 'A' ELSE 'B' END
FROM table t
OUTER APPLY (SELECT COUNT(1) AS Cnt
             WHERE Masterip LIKE t.Masterip + '.%'
             AND Masterip <> t.Masterip
             )t1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

583 Posts

Posted - 06/17/2012 :  13:41:11  Show Profile  Reply with Quote
Thanks Visakh !
The query is not giving the correct expected results


DECLARE @Masterip table(
    Masterip Varchar(100) NOT NULL,
    status varchar(20) 
    );
  
 insert into @masterip
 
 select 
'5.46.200.1.462222.2.1.1.1.1.1',NULL  
union all
select  
'5.46.200.1.462222.2.1.1.1.1.1.1',NULL 
union all
select  
'5.46.200.1.462222.2.1.1.1.1.1.2',NULL
union all
select  
'5.46.200.1.462222.2.1.1.1.1.1.3',NULL 
union all
select  
'5.46.200.1.462222.2.1.1.1.1.1.4',NULL  
union all
select  
'5.46.200.1.462222.2.1.1.1.1.5',NULL 
union all
select  
'5.46.200.1.462222.2.1.1.1.1.5.1',NULL 
union all
select  
'5.46.200.1.462222.2.1.1.1.1.6',NULL



UPDATE t
SET t.status= CASE WHEN Cnt > 0 THEN 'A' ELSE 'B' END
FROM @Masterip t
OUTER APPLY (SELECT COUNT(1) AS Cnt
             WHERE Masterip LIKE t.Masterip + '.%'
             AND Masterip <> t.Masterip
             )t1
             
             
select * from @Masterip

Query results:

Masterip                       Status
----------------------------- ---
5.46.200.1.462222.2.1.1.1.1.1	B
5.46.200.1.462222.2.1.1.1.1.1.1	B
5.46.200.1.462222.2.1.1.1.1.1.2	B
5.46.200.1.462222.2.1.1.1.1.1.3	B
5.46.200.1.462222.2.1.1.1.1.1.4	B
5.46.200.1.462222.2.1.1.1.1.5	B
5.46.200.1.462222.2.1.1.1.1.5.1	B
5.46.200.1.462222.2.1.1.1.1.6	B


Expected Results:
Masterip                       Status
-----------------------------  ----
5.46.200.1.462222.2.1.1.1.1.1	A
5.46.200.1.462222.2.1.1.1.1.1.1	B
5.46.200.1.462222.2.1.1.1.1.1.2	B
5.46.200.1.462222.2.1.1.1.1.1.3	B
5.46.200.1.462222.2.1.1.1.1.1.4	B
5.46.200.1.462222.2.1.1.1.1.5	A
5.46.200.1.462222.2.1.1.1.1.5.1	B
5.46.200.1.462222.2.1.1.1.1.6	A

Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

583 Posts

Posted - 06/17/2012 :  13:46:41  Show Profile  Reply with Quote
I was able to correct the query from table is missing..

UPDATE t
SET t.status= CASE WHEN Cnt > 0 THEN 'A' ELSE 'B' END
FROM table t
OUTER APPLY (SELECT COUNT(1) AS Cnt from table
WHERE Masterip LIKE t.Masterip + '.%'
AND Masterip <> t.Masterip
)t1

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 06/17/2012 :  17:07:41  Show Profile  Reply with Quote
ok cool

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000