如果值 = 'ADMIN',则删除 "where" 子句

Getting rid of the "where" clause if a value = 'ADMIN'

所以有主要的 table(名为 Table1),其中的一列 table 有一个名为公司名称的列。还有另一个 table 称为帐户,其中 table 它具有用户的用户名和与用户关联的公司名称。因此只应显示 table1 中与用户公司相关的信息,除非他们是管理员。

我正在 Oracle 上开发 APEX 应用程序

我目前有一个如下所示的查询:

Select
SUPPLIER,
sum(NUMBER_OF_TICKETS) TICKETS,
round((sum(NUMBER_OF_TICKETS*AMOUNT_PAYABLE__FOREIGN_CUR)*.1),2) COMMISION,
round(sum(NUMBER_OF_TICKETS*AMOUNT_PAYABLE__FOREIGN_CUR),2) TOTAL_SALES,
round((sum(NUMBER_OF_TICKETS*AMOUNT_PAYABLE__FOREIGN_CUR)-(sum(NUMBER_OF_TICKETS*AMOUNT_PAYABLE__FOREIGN_CUR)*.1)),2) COMPANY_OWE,
CURRENCY
FROM TABLE1
WHERE
 Supplier = (select COMPANYNAME from Account where lower(USERNAME)=lower(:APP_USER)) AND
     PURCHASE_DATE_AND_TIME >= TO_DATE(:P2_START)
      AND PURCHASE_DATE_AND_TIME < TO_DATE(:P2_END)+ 1
group by
SUPPLIER,
CURRENCY


我 运行 在使用 "WHERE" 函数时遇到了麻烦。 因为我基本上想要一个 if...then... (或者在这种情况下是一个案例陈述) 在哪里

如果
((select COMPANYNAME from Account where lower(USERNAME)=lower(:APP_USER)) == 'COMPANY1' AND (select USERTYPE from Account where lower (用户名)=较低(:APP_USER)) == 'ADMIN')

然后
显示所有行;又名,Supplier = (select COMPANYNAME from BBAccount where lower(USERNAME)=lower(:APP_USER)) in the query

有人知道如何为此编写查询吗?
我试着做

Case statement When statement Then NULL

它不起作用

我试着画了一张图,让我知道这是否更清楚:
Diagram

So there is the main table (named Table1) and one of the columns in that table has a column called company name. and there's a another table called Account and in that table it has the usernames of people and the company name that user is associated with. so only the information in table1 associated with the user's company should be shown, unless if they're an admin

我认为您可以简单地加入 - 例如:

SELECT ...
FROM TABLE1 t
INNER JOIN ACCOUNTS a 
    ON  lower(a.username) = lower(:APP_USER)
    AND (a.companyname = t.companyname OR a.usertype = 'ADMIN')
WHERE
    t.purchase_date_and_time >= TO_DATE(:P2_START)
    AND t.purchase_date_and_time < TO_DATE(:P2_END) + 1
GROUP BY ...

ACCOUNTS 上的内部联接用于实现过滤逻辑:用户必须属于同一公司,或者具有管理员类型。

EXISTS(...)是你的朋友:


Select SUPPLIER
        ,sum(NUMBER_OF_TICKETS) TICKETS
        ,round((sum(NUMBER_OF_TICKETS*AMOUNT_PAYABLE__FOREIGN_CUR)*.1),2) COMMISION
        ,round(sum(NUMBER_OF_TICKETS*AMOUNT_PAYABLE__FOREIGN_CUR),2) TOTAL_SALES
        ,round((sum(NUMBER_OF_TICKETS*AMOUNT_PAYABLE__FOREIGN_CUR)
              -(sum(NUMBER_OF_TICKETS*AMOUNT_PAYABLE__FOREIGN_CUR)*.1)),2) COMPANY_OWE
        , CURRENCY
FROM TABLE1 t
WHERE EXISTS (
        SELECT 1
        from Account x
        where x.Supplier = t.COMPANYNAME
        AND lower(x.USERNAME) = lower(:APP_USER))
        AND x.PURCHASE_DATE_AND_TIME >= TO_DATE(:P2_START)
        AND x.PURCHASE_DATE_AND_TIME < TO_DATE(:P2_END)+ 1
        )
group by SUPPLIER, CURRENCY
        ;

因为是关于Apex,我建议你创建一个函数:

create or replace function f_is_admin_01 (par_app_user in varchar2)
  return number
is
  -- function returns 1 if PAR_APP_USER is admin; otherwise, it returns 0
  l_usertype  account.usertype%type;
begin
  select a.usertype
    into l_usertype
    from account a
    where lower(a.username) = lower(par_app_user);

  return case when l_usertype = 'ADMIN' then 1
              else 0
         end;

exception
  when no_data_found then
    return 0;
end;

现在,您可以在查询中使用它作为

select ...
from account a join table1 t on a.companyname = t.companyname
where (lower(a.username) = lower(:APP_USER) or f_is_admin_01 (:APP_USER) = 1)
  and t.purchasedate ...

这种方法(我的意思是,有一个功能)在其他地方可能会有用;例如,如果您只想向管理员显示某些页面区域 - 您可以输入

return f_is_admin_01(:APP_USER) = 1;

进入区域的 "Server side condition"(其类型为 "Function that returns Boolean")。

看看有没有帮助。