View previous topic :: View next topic |
Author |
Message |
Bharath.AK Beginner

Joined: 13 Jul 2007 Posts: 34 Topics: 10 Location: Los Angeles
|
Posted: Fri May 20, 2011 11:05 am Post subject: WHERE statements based on IF-THEN-ELSE conditions - PROC SQL |
|
|
Hello,
Please help me out with this scenario:
I have 2 tables - One is GROUP and another is LIBUSER.
GROUP contains AdGroup and Library variables;
LIBUSER contains Library and AdUser variables.
Sample data for GROUP is: Code: |
-------------------
AdGroup Library
-------------------
ABC Sales
ABC Cost
ABC Price
HNK Sales
HNK Cost
HNK Price
USA Sales
USA Cost
USA Price
IND Sales
IND Cost
IND Price
-------------------
|
Sample data for LIBUSER is:
-------------------- Code: |
Library AdUser
--------------------
Sales Bharath
Sales Ajay
Sales Vijay
Cost Sekhar
Cost Ajay
Cost Ram
Cost Arjun
Price ALL |
--------------------
Here, I need to write a query such that
If AdUser = 'ALL' in the LIBUSER table, then run one WHERE statement in my SQL Select statement.
if AdUser NOT = 'ALL' in the LIBUSER table, then run another WHERE statement in my SQL select statement.
Which means I have only one SELECT statement but based on the valus for AdUser in LIBUSER table, I need SQL to run that specific WHERE condition.
Please help me how to go about it. Any help would be greatly appreciated.
Thanks,
Bharath. _________________ Regards,
BK
***** " If You Have To Do It, You Might As Well Do It Right.....If It Is Worth Doing At All, It's Worth Doing It Right !!! " ***** |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Fri May 20, 2011 11:19 am Post subject: |
|
|
Bharath.AK,
What is the Final output you want from the select statement?
Kolusu |
|
Back to top |
|
 |
Bharath.AK Beginner

Joined: 13 Jul 2007 Posts: 34 Topics: 10 Location: Los Angeles
|
Posted: Fri May 20, 2011 11:42 am Post subject: |
|
|
Hello Kolusu,
The thing is we are working on restricting our SAS users to have access to only few libraries instead of all available libraries.
When the value of AdUser in LIBUSER table is 'ALL', then all users have access to that library. If the value is other than 'ALL', that means only specific users hould have access to that library.
create table temp as
select distinct library from Group a, LIBUSER d
where a.library=d.library
Here we have all the Users and libraries in TEMP table.
Now we are trying to restrict users and have to put a condition to restrict on a library level
if AdUser = ALL, accesses by every user. etc.
Did I answer your question? Please let me know. _________________ Regards,
BK
***** " If You Have To Do It, You Might As Well Do It Right.....If It Is Worth Doing At All, It's Worth Doing It Right !!! " ***** |
|
Back to top |
|
 |
Bharath.AK Beginner

Joined: 13 Jul 2007 Posts: 34 Topics: 10 Location: Los Angeles
|
Posted: Fri May 20, 2011 11:53 am Post subject: |
|
|
Kolusu, if this helps,
1. If the LIBUSER.AdUser NOT = 'ALL', then I should create the TEMP table as
create table temp as
select distinct a.adgroup, d.aduser
from group a, libuser d
where a.library=d.library
2. If the LIBUSER.AdUser = 'ALL', then I should create the TEMP table as
create table temp as
select distinct a.adgroup, d.aduser
from group a, libuser d
where a.library=d.library and userlogin = d.libuser
Here we get userlogin value from some other source (can be manageable) And these should be accomplished in a single select statement. _________________ Regards,
BK
***** " If You Have To Do It, You Might As Well Do It Right.....If It Is Worth Doing At All, It's Worth Doing It Right !!! " ***** |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Fri May 20, 2011 1:13 pm Post subject: |
|
|
Bharath.AK,
Here is an untested sql which would dynamically build your DDL statements. I assumed that you have a column named LOGON_USER where you pass the login name to select the ADUSER column
Code: |
SELECT Z.C1
FROM (SELECT CHAR('CREATE TABLE TEMP AS',80) AS C1
,INT(1) AS C2
FROM SYSIBM.SYSDUMMY1
UNION
SELECT CHAR('SELECT DISTINCT A.ADGROUP, D.ADUSER',80) AS C1
,INT(2) AS C2
FROM SYSIBM.SYSDUMMY1
UNION
SELECT CHAR(' FROM GROUP A, LIBUSER D',80) AS C1
,INT(3) AS C2
FROM SYSIBM.SYSDUMMY1
UNION
SELECT CHAR(' WHERE A.LIBRARY = D.LIBRARY',80) AS C1
,INT(4) AS C2
FROM SYSIBM.SYSDUMMY1
UNION
SELECT CASE WHEN ADUSER = 'ALL' THEN
CHAR(' AND USERLOGIN = D.LIBUSER',80)
END AS C1
,INT(5) AS C2
FROM Your_table
WHERE LOGON_USER = 'USERNAME') Z
ORDER BY Z.C2
; |
|
|
Back to top |
|
 |
Bharath.AK Beginner

Joined: 13 Jul 2007 Posts: 34 Topics: 10 Location: Los Angeles
|
Posted: Fri May 20, 2011 1:19 pm Post subject: |
|
|
Kolusu, thatk you for your time to help me out in this regard. Actually I am not coding this PROC SQL stmt on mainframe, but in a SAS application. SYSIBM.SYSDUMMY1 is a utility used in JCL, correct?
I'm sorry I should have told this earlier itself. I will try to amend the code accordingly, but when you get time, can you also pls provide the SAS editor application of this code?
Thanks a lot again. _________________ Regards,
BK
***** " If You Have To Do It, You Might As Well Do It Right.....If It Is Worth Doing At All, It's Worth Doing It Right !!! " ***** |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Fri May 20, 2011 1:26 pm Post subject: |
|
|
Bharath.AK wrote: | Kolusu, thatk you for your time to help me out in this regard. Actually I am not coding this PROC SQL stmt on mainframe, but in a SAS application. SYSIBM.SYSDUMMY1 is a utility used in JCL, correct? |
where do you get such information? SYSIBM.SYSDUMMY1 is a DB2 catalog table which just contains 1 row. I am not sure as to why you think the solution is a PROC for you. It is a simple SQL statement which can be run in spufi/qmf/program. You need to brush up your basic knowledge about SQL and DB2 tables.
Bharath.AK wrote: |
I'm sorry I should have told this earlier itself. I will try to amend the code accordingly, but when you get time, can you also pls provide the SAS editor application of this code?
Thanks a lot again. |
*sigh* I don't know what to make out from the above statement. |
|
Back to top |
|
 |
papadi Supermod
Joined: 20 Oct 2009 Posts: 594 Topics: 1
|
Posted: Fri May 20, 2011 9:20 pm Post subject: |
|
|
If you want to restrict who can use which tables, suggest you get rid of the "custom" "who can use what" that has been implemented.
Use or restriction of "things" is better accomplished by having proper permissions or security settings.
Possibly i misunderstand. . .
Quote: | SYSIBM.SYSDUMMY1 is a utility used in JCL |
What was the origin of this bit of misinformation? Suggest this source not be used for information that you really need. _________________ All the best,
di
Last edited by papadi on Wed May 25, 2011 2:32 pm; edited 1 time in total |
|
Back to top |
|
 |
Bharath.AK Beginner

Joined: 13 Jul 2007 Posts: 34 Topics: 10 Location: Los Angeles
|
Posted: Wed May 25, 2011 9:43 am Post subject: |
|
|
Thank you kolusu & papadi...
I'm not at all into sql or mainframe or db2 programming, but had to work on this. Yes, I have to 'learn' db2 (but not 'brush up') And yes, I definitely will avoid following those misleading sources of information henceforth.
I've got the solution for my issue with no much complexity anyway.
Thanks for all your time to help me in this regard.
Cheers  |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed May 25, 2011 1:21 pm Post subject: |
|
|
Bharath.AK wrote: |
I've got the solution for my issue with no much complexity anyway.
|
How about sharing the solution here? so that we can also learn.
Kolusu |
|
Back to top |
|
 |
GuyC Supermod
Joined: 27 May 2011 Posts: 35 Topics: 0
|
Posted: Mon May 30, 2011 3:30 am Post subject: |
|
|
Does this solve the requirement?
Code: | create table temp as
select distinct a.library from Group a, LIBUSER d
where a.library=d.library
and d.aduser in ('all',:hv-userlogin) |
or even better
Code: | create table temp as
select a.library from Group a
where exists( select 1 from LIBUSER d
where a.library=d.library
and d.aduser in ('all',:hv-userlogin)) |
|
|
Back to top |
|
 |
|
|