如何隐藏员工姓名重复的SQL

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

How to hidden employee name duplicate sql

问题

我想要从这里隐藏员工姓名

> 如何隐藏员工姓名重复的SQL

到这里

> 如何隐藏员工姓名重复的SQL

查询

SELECT
XU.EMP_NAME,
XU.FUNCTION,
XU.ROLE_ACCESS 
FROM
XX_USERS XU LEFT JOIN 
XX_USERS_ACCESS XUA ON XU.EMP_ID=XUA.EMP_ID 
WHERE
NVL(XU.STATUS,'N')='Y'
order by  XU.EMP_NAME 

可以吗?

英文:

i want to hide employee name from this

> 如何隐藏员工姓名重复的SQL

To this

> 如何隐藏员工姓名重复的SQL

Query

SELECT
XU.EMP_NAME,
XU.FUNCTION,
XU.ROLE_ACCESS 
FROM
XX_USERS XU LEFT JOIN 
XX_USERS_ACCESS XUA ON XU.EMP_ID=XUA.EMP_ID 
WHERE
NVL(XU.STATUS,'N')='Y'
order by  XU.EMP_NAME 

its posible ?

答案1

得分: 1

根据您标记的问题中的PL/SQL标签(这表明您可能正在使用Oracle和MySQL一起使用),那么:我没有您的表,但我有Scott的示例模式。 模拟您的情况的查询如下:

SQL> select d.dname, e.ename, e.job
  2  from emp e join dept d on d.deptno = e.deptno
  3  order by d.dname, e.ename;

DNAME          ENAME      JOB
-------------- ---------- ---------
ACCOUNTING     CLARK      MANAGER
ACCOUNTING     KING       PRESIDENT
ACCOUNTING     MILLER     CLERK
RESEARCH       ADAMS      CLERK
RESEARCH       FORD       ANALYST
RESEARCH       JONES      MANAGER
RESEARCH       SCOTT      ANALYST
RESEARCH       SMITH      CLERK
SALES          ALLEN      SALESMAN
SALES          BLAKE      MANAGER
SALES          JAMES      CLERK
SALES          MARTIN     SALESMAN
SALES          TURNER     SALESMAN
SALES          WARD       SALESMAN

14 rows selected.

如果您使用SQL*Plus,您可以设置break

SQL> break on dname
SQL> /

DNAME          ENAME      JOB
-------------- ---------- ---------
ACCOUNTING     CLARK      MANAGER
               KING       PRESIDENT
               MILLER     CLERK
RESEARCH       ADAMS      CLERK
               FORD       ANALYST
               JONES      MANAGER
               SCOTT      ANALYST
               SMITH      CLERK
SALES          ALLEN      SALESMAN
               BLAKE      MANAGER
               JAMES      CLERK
               MARTIN     SALESMAN
               TURNER     SALESMAN
               WARD       SALESMAN

14 rows selected.

或者,对行进行排序(使用row_number分析函数),并有条件地显示列值:

SQL> with temp as
  2    (select d.dname, e.ename, e.job,
  3       row_number() over (partition by d.dname order by d.dname, e.ename) rn,
  4       row_number() over (order by d.dname, e.ename) rn_sort
  5     from emp e join dept d on d.deptno = e.deptno
  6    )
  7  select case when rn = 1 then dname else null end dname,
  8         ename, job
  9  from temp
 10  order by rn_sort;

DNAME          ENAME      JOB
-------------- ---------- ---------
ACCOUNTING     CLARK      MANAGER
               KING       PRESIDENT
               MILLER     CLERK
RESEARCH       ADAMS      CLERK
               FORD       ANALYST
               JONES      MANAGER
               SCOTT      ANALYST
               SMITH      CLERK
SALES          ALLEN      SALESMAN
               BLAKE      MANAGER
               JAMES      CLERK
               MARTIN     SALESMAN
               TURNER     SALESMAN
               WARD       SALESMAN

14 rows selected.

另一方面,类似这样的操作通常应在某个报表工具中使用,大多数(如果不是全部)报表工具都有类似的“分组”选项。通常情况下,我们不在纯SQL中执行此类操作。

英文:

As you tagged the question with PL/SQL tag (which suggests that you might be using Oracle alongside MySQL), then: I don't have your tables, but I do have Scott's sample schema. Query that simulates what you have is:

SQL> select d.dname, e.ename, e.job
  2  from emp e join dept d on d.deptno = e.deptno
  3  order by d.dname, e.ename;

DNAME          ENAME      JOB
-------------- ---------- ---------
ACCOUNTING     CLARK      MANAGER
ACCOUNTING     KING       PRESIDENT
ACCOUNTING     MILLER     CLERK
RESEARCH       ADAMS      CLERK
RESEARCH       FORD       ANALYST
RESEARCH       JONES      MANAGER
RESEARCH       SCOTT      ANALYST
RESEARCH       SMITH      CLERK
SALES          ALLEN      SALESMAN
SALES          BLAKE      MANAGER
SALES          JAMES      CLERK
SALES          MARTIN     SALESMAN
SALES          TURNER     SALESMAN
SALES          WARD       SALESMAN

14 rows selected.

If you used SQL*Plus, you could set a break:

SQL> break on dname
SQL> /

DNAME          ENAME      JOB
-------------- ---------- ---------
ACCOUNTING     CLARK      MANAGER
               KING       PRESIDENT
               MILLER     CLERK
RESEARCH       ADAMS      CLERK
               FORD       ANALYST
               JONES      MANAGER
               SCOTT      ANALYST
               SMITH      CLERK
SALES          ALLEN      SALESMAN
               BLAKE      MANAGER
               JAMES      CLERK
               MARTIN     SALESMAN
               TURNER     SALESMAN
               WARD       SALESMAN

14 rows selected.

Or, sort rows (using row_number analytic function) and conditionally display column value:

SQL> with temp as
  2    (select d.dname, e.ename, e.job,
  3       row_number() over (partition by d.dname order by d.dname, e.ename) rn,
  4       row_number() over (order by d.dname, e.ename) rn_sort
  5     from emp e join dept d on d.deptno = e.deptno
  6    )
  7  select case when rn = 1 then dname else null end dname,
  8         ename, job
  9  from temp
 10  order by rn_sort;

DNAME          ENAME      JOB
-------------- ---------- ---------
ACCOUNTING     CLARK      MANAGER
               KING       PRESIDENT
               MILLER     CLERK
RESEARCH       ADAMS      CLERK
               FORD       ANALYST
               JONES      MANAGER
               SCOTT      ANALYST
               SMITH      CLERK
SALES          ALLEN      SALESMAN
               BLAKE      MANAGER
               JAMES      CLERK
               MARTIN     SALESMAN
               TURNER     SALESMAN
               WARD       SALESMAN

14 rows selected.

On the other hand, something like this is supposed to be used in some reporting tool, most (if not all) of them have such a "grouping" option. Usually, we don't do that in pure SQL.

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

发表评论

匿名网友

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

确定