Solved

Sybase Query

Posted on 2012-03-29
2
359 Views
Last Modified: 2012-03-29
Table1                                                     Table2

mbr_id                                                      Claim
plan                                                        mbr_id
Sel_type                                                  ps_code
                                                            rc_code)
I need to select every row from Table1 and update Sel_type with a value
of "X" if Table2 has at least one ps_code in ('191', '192', '193') and the plan = 1
If the plan = 2   and rc_code  in ('A', 'B', 'C')    Sel_type = "X"  

Table1                                                                        Table2  
mbr_id                 plan                          Claim           mbr_id      ps_cpde                rc_code
1234                       1                             abc1            4321             200              A
4321                       2                             abc2            1234             200              A
4444                       1                             abc3            4321             191              D
                                                         abc4            4444             193              A
                                                         
                                                         
 result set:
 
 mbr_id                plan                 Sel_type
 
 1234                   1                       null
 4321                   2                      X
 4444                   1                      X
 
 
 
 Please create a Select that will lead to a result as above.
0
Comment
Question by:garyinmiami2003
  • 2
2 Comments
 
LVL 9

Expert Comment

by:rajeevnandanmishra
ID: 37782289
Hi,
Try this.

select a.mbr_id, a.plan, sel_type = (case when b.ps_code is null then null else 'X' end)
from Table1 a left outer join Table2 b
on a.mbr_id = b.mbr_id and ((a.plan = 1 and b.ps_code in ('191', '192', '193') )
                  or a.plan = 2 and rc_code  in ('A', 'B', 'C'))
0
 
LVL 9

Accepted Solution

by:
rajeevnandanmishra earned 500 total points
ID: 37782304
Done some mistake with the code:
Try this one:

select a.mbr_id, a.plan, sel_type = (case when b.ps_code is null then null else 'X' end)
from Table1 a left outer join Table2 b 
on a.mbr_id = b.mbr_id and ((a.plan = 1 and b.ps_code in ('191', '192', '193') ) 
			or (a.plan = 2 and rc_code  in ('A', 'B', 'C')))

Open in new window

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sqlquerystress - To test db performance 8 42
Slow Connectivity over ODBC 8 35
sql server tables from access 18 19
Present Absent from working date rage 11 20
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question