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

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

How to display data based on value in another table?

问题

  1. select s.* from D365_PURCHASE_INVOICE_STAGING s
  2. INNER JOIN USERDETAILS u
  3. on s.COMPANY = u.D365LE
  4. where (u.ROLE = 'Admin')
  5. or
  6. (upper(s.COMPANY) = (SELECT u.D365LE FROM USERDETAILS WHERE upper(u.USERNAME) = upper(:app_user)))
  7. 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:

  1. select * from D365_PURCHASE_INVOICE_STAGING
  2. 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:

  1. select s.* from D365_PURCHASE_INVOICE_STAGING s
  2. INNER JOIN USERDETAILS u
  3. on s.COMPANY = u.D365LE
  4. where (u.ROLE = &#39;Admin&#39;)
  5. or
  6. (upper(s.COMPANY) = (SELECT u.D365LE FROM USERDETAILS WHERE upper(u.USERNAME) = upper(:app_user)))
  7. and (u.ROLE &lt;&gt; &#39;Admin&#39;)

答案1

得分: 1

以下是翻译好的部分:

  1. SQL&gt; with
  2. 2 d365_purchase_invoice_staging (company) as
  3. 3 (select 'Apple' from dual union all
  4. 4 select 'IBM' from dual
  5. 5 ),
  6. 6 userdetails (username, d365le, role) as
  7. 7 (select 'LITTLE', 'IBM' , 'Admin' from dual union all
  8. 8 select 'FOOT' , 'Apple', 'Clerk' from dual
  9. 9 )
  10. Query:
  11. 10 select *
  12. 11 from d365_purchase_invoice_staging d
  13. 12 join userdetails u on d.company = u.d365le
  14. 13 where u.username = case when u.role &lt;&gt; 'Admin' then u.username
  15. 14 else '&amp;APP_USER'
  16. 15 end;
  17. Enter value for app_user: LITTLE --&gt; admin
  18. COMPA USERNA D365L ROLE
  19. ----- ------ ----- -----
  20. IBM LITTLE IBM Admin
  21. Apple FOOT Apple Clerk
  22. SQL&gt; /
  23. Enter value for app_user: FOOT --&gt; not admin
  24. COMPA USERNA D365L ROLE
  25. ----- ------ ----- -----
  26. Apple FOOT Apple Clerk
  27. SQL>;

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

英文:

Something like this, perhaps?

Sample data:

  1. SQL&gt; with
  2. 2 d365_purchase_invoice_staging (company) as
  3. 3 (select &#39;Apple&#39; from dual union all
  4. 4 select &#39;IBM&#39; from dual
  5. 5 ),
  6. 6 userdetails (username, d365le, role) as
  7. 7 (select &#39;LITTLE&#39;, &#39;IBM&#39; , &#39;Admin&#39; from dual union all
  8. 8 select &#39;FOOT&#39; , &#39;Apple&#39;, &#39;Clerk&#39; from dual
  9. 9 )

Query:

  1. 10 select *
  2. 11 from d365_purchase_invoice_staging d
  3. 12 join userdetails u on d.company = u.d365le
  4. 13 where u.username = case when u.role &lt;&gt; &#39;Admin&#39; then u.username
  5. 14 else &#39;&amp;APP_USER&#39;
  6. 15 end;
  7. Enter value for app_user: LITTLE --&gt; admin
  8. COMPA USERNA D365L ROLE
  9. ----- ------ ----- -----
  10. IBM LITTLE IBM Admin
  11. Apple FOOT Apple Clerk
  12. SQL&gt; /
  13. Enter value for app_user: FOOT --&gt; not admin
  14. COMPA USERNA D365L ROLE
  15. ----- ------ ----- -----
  16. Apple FOOT Apple Clerk
  17. 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

  1. 可以使用:
  2. ```sql
  3. select *
  4. from D365_PURCHASE_INVOICE_STAGING d
  5. where EXISTS(
  6. SELECT
  7. FROM USERDETAILS u
  8. WHERE upper(u.username) = upper(:app_user)
  9. AND ( upper(company) = u.D365LE
  10. OR u.role = 'Admin' )
  11. )
英文:

You can use:

  1. select *
  2. from D365_PURCHASE_INVOICE_STAGING d
  3. where EXISTS(
  4. SELECT
  5. FROM USERDETAILS u
  6. WHERE upper(u.username) = upper(:app_user)
  7. AND ( upper(company) = u.D365LE
  8. OR u.role = &#39;Admin&#39; )
  9. )

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:

确定