如何根据另一张表中的数值来显示数据?

huangapple go评论66阅读模式
英文:

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 = &#39;Admin&#39;)
or 
(upper(s.COMPANY) = (SELECT u.D365LE FROM USERDETAILS WHERE upper(u.USERNAME) = upper(:app_user)))
and (u.ROLE &lt;&gt; &#39;Admin&#39;)

答案1

得分: 1

以下是翻译好的部分:

SQL&gt; 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 &lt;&gt; 'Admin' then u.username
 14                          else '&amp;APP_USER'
 15                     end;
Enter value for app_user: LITTLE          --&gt; admin

COMPA USERNA D365L ROLE
----- ------ ----- -----
IBM   LITTLE IBM   Admin
Apple FOOT   Apple Clerk

SQL&gt; /
Enter value for app_user: FOOT           --&gt; not admin

COMPA USERNA D365L ROLE
----- ------ ----- -----
Apple FOOT   Apple Clerk

SQL>;

请注意,代码部分没有翻译。如果需要进一步的翻译或有其他问题,请告诉我。

英文:

Something like this, perhaps?

Sample data:

SQL&gt; with
  2  d365_purchase_invoice_staging  (company) as
  3    (select &#39;Apple&#39; from dual union all
  4     select &#39;IBM&#39;   from dual
  5    ),
  6  userdetails (username, d365le, role) as
  7    (select &#39;LITTLE&#39;, &#39;IBM&#39;  , &#39;Admin&#39; from dual union all
  8     select &#39;FOOT&#39;  , &#39;Apple&#39;, &#39;Clerk&#39; 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 &lt;&gt; &#39;Admin&#39; then u.username
 14                          else &#39;&amp;APP_USER&#39;
 15                     end;
Enter value for app_user: LITTLE          --&gt; admin

COMPA USERNA D365L ROLE
----- ------ ----- -----
IBM   LITTLE IBM   Admin
Apple FOOT   Apple Clerk

SQL&gt; /
Enter value for app_user: FOOT           --&gt; not admin

COMPA USERNA D365L ROLE
----- ------ ----- -----
Apple FOOT   Apple Clerk

SQL&gt;

(this was ran in SQL*Plus so I used substitution variable, &#39;&amp;APP_USER&#39;. 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 = &#39;Admin&#39; )
       )

huangapple
  • 本文由 发表于 2023年6月5日 19:14:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定