Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sqlfresher2k7
Aged Yak Warrior
623 Posts |
Posted - 2014-01-30 : 00:00:04
|
Please ignore my earlier post..I need a query which should update the master table with status based on the action columns values.If any of the values of the tableB for action column are 1,3,4,5,6 then update the status to 'Y' for the correspondingSID in the tableA else the SID will be 'N'.Below is the sample data with expected output.Thanks for your help in advance..TableASID Name---- -----10 Andy11 Sam12 pat13 Mat14 JohnTableBSID action ---- ------- 10 110 310 911 511 612 212 713 114 10Expected Output:SID Name status---- ----- -------10 Andy Y11 Sam Y12 pat N13 Mat Y14 John N |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-01-30 : 01:42:03
|
[code];with TableAAS( select 10 as SID, 'Andy' as Name union all select 11 ,'Sam' union all select 12,'Pat' union all select 13,'Mat' union all select 14,'John'),TableBas ( select 10 as SID,1 as action union all select 10,3 union all select 10, 9 union all select 11, 5 union all select 11, 6 union all select 12, 2 union all select 12, 7 union all select 13, 1 union all select 14, 10)select A.SID ,A.Name ,case when sum(valStatus) > 0 then 'Y' else 'N' end as statusfrom(select SID ,action , case when action in (1,3,4,5,6) then 1 else 0 end as valStatusfrom TableB)B inner join TableA as A on A.SiD=B.SIDGroup by A.SID ,A.Name[/code]output:[code]SID Name status10 Andy Y11 Sam Y12 Pat N13 Mat Y14 John N[/code]SCe-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mutsabinWeb |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-01-30 : 06:57:22
|
[code]SELECT a.*,CASE WHEN b.SID IS NULL THEN 'N' ELSE 'Y' ENDFROM TableA aLEFT JOIN (SELECT SID FROM TableB GROUP BY SID HAVING SUM(CASE WHEN action IN (1,3,4,5,6) THEN 1 ELSE 0 END) > 0 )bON b.SID = a.SID[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-01-30 : 16:13:18
|
[code]SELECT a.[SID], a.Name, CASE WHEN b.[SID] IS NULL THEN 'Y' ELSE 'N' END AS [Status]FROM TableA AS aLEFT JOIN ( SELECT [SID] FROM TableB WHERE [Action] IN (1, 3, 4, 5, 6) GROUP BY [SID] ) AS b ON b.[SID] = a.[SID];[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
|
|
|
|
|