英文:
How to display data based on value in another table?
问题
select s.* from D365_PURCHASE_INVOICE_STAGING s
INNER JOIN USERDETAILS u
on s.COMPANY = u.D365LE
where (u.ROLE = 'Admin')
or
(upper(s.COMPANY) = (SELECT u.D365LE FROM USERDETAILS WHERE upper(u.USERNAME) = upper(:app_user)))
and (u.ROLE <> 'Admin')
英文:
I have a Interactive Report which displays data for multiple divisions, column name D365LE.
I also have a users table which contains the same column name, depending on which division the employee is a part of.
I'm able to display the data which only correlates to the users division using the following:
select * from D365_PURCHASE_INVOICE_STAGING
where upper(company) = (SELECT u.D365LE FROM USERDETAILS u WHERE upper(u.username) = upper(:app_user))
In the USERDETAILS table there is a column called ROLE which holds whether the user is an admin or not. I want the report to display all data if the user is Admin, and if not it should only show the data which correlates to their division, as above.
I've tried the following, which does compile but displays no data:
select s.* from D365_PURCHASE_INVOICE_STAGING s
INNER JOIN USERDETAILS u
on s.COMPANY = u.D365LE
where (u.ROLE = 'Admin')
or
(upper(s.COMPANY) = (SELECT u.D365LE FROM USERDETAILS WHERE upper(u.USERNAME) = upper(:app_user)))
and (u.ROLE <> 'Admin')
答案1
得分: 1
以下是翻译好的部分:
SQL> with
2 d365_purchase_invoice_staging (company) as
3 (select 'Apple' from dual union all
4 select 'IBM' from dual
5 ),
6 userdetails (username, d365le, role) as
7 (select 'LITTLE', 'IBM' , 'Admin' from dual union all
8 select 'FOOT' , 'Apple', 'Clerk' from dual
9 )
Query:
10 select *
11 from d365_purchase_invoice_staging d
12 join userdetails u on d.company = u.d365le
13 where u.username = case when u.role <> 'Admin' then u.username
14 else '&APP_USER'
15 end;
Enter value for app_user: LITTLE --> admin
COMPA USERNA D365L ROLE
----- ------ ----- -----
IBM LITTLE IBM Admin
Apple FOOT Apple Clerk
SQL> /
Enter value for app_user: FOOT --> not admin
COMPA USERNA D365L ROLE
----- ------ ----- -----
Apple FOOT Apple Clerk
SQL>;
请注意,代码部分没有翻译。如果需要进一步的翻译或有其他问题,请告诉我。
英文:
Something like this, perhaps?
Sample data:
SQL> with
2 d365_purchase_invoice_staging (company) as
3 (select 'Apple' from dual union all
4 select 'IBM' from dual
5 ),
6 userdetails (username, d365le, role) as
7 (select 'LITTLE', 'IBM' , 'Admin' from dual union all
8 select 'FOOT' , 'Apple', 'Clerk' from dual
9 )
Query:
10 select *
11 from d365_purchase_invoice_staging d
12 join userdetails u on d.company = u.d365le
13 where u.username = case when u.role <> 'Admin' then u.username
14 else '&APP_USER'
15 end;
Enter value for app_user: LITTLE --> admin
COMPA USERNA D365L ROLE
----- ------ ----- -----
IBM LITTLE IBM Admin
Apple FOOT Apple Clerk
SQL> /
Enter value for app_user: FOOT --> not admin
COMPA USERNA D365L ROLE
----- ------ ----- -----
Apple FOOT Apple Clerk
SQL>
(this was ran in SQL*Plus so I used substitution variable, '&APP_USER'
. You'd use :APP_USER
as you normally do in Apex).
答案2
得分: 1
可以使用:
```sql
select *
from D365_PURCHASE_INVOICE_STAGING d
where EXISTS(
SELECT
FROM USERDETAILS u
WHERE upper(u.username) = upper(:app_user)
AND ( upper(company) = u.D365LE
OR u.role = 'Admin' )
)
英文:
You can use:
select *
from D365_PURCHASE_INVOICE_STAGING d
where EXISTS(
SELECT
FROM USERDETAILS u
WHERE upper(u.username) = upper(:app_user)
AND ( upper(company) = u.D365LE
OR u.role = 'Admin' )
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论