简体   繁体   中英

SQL query duplicating names in output

I'm writing a query that calls an employee name , employee's username , security level , menu , menu's tabs , and menu descriptions . I'm wanting the query only display the employee name and username once as a column and then display the security level , menu , menu tabs , and menu description .

Select usname as Name,
Ususer as Username,
Uswosecl as WO_SecurityLevel,
AoOpID as Operation_ID, aoseclevel as Operation_Security,
AoMenu as Menu, AoMenuItem as Tab, AoText as Description
from cudtatet.xxpuser
join fmsusrfua.xxpauops on uswosecl >= aoseclevel
and aoseclevel >= 0
where ususer NOT IN (‘*ALL’,’daffron’)
and aoAuOpID >=70000 and aoAuOpID < 80000
order by usname, ususer

I would post my output but its rather large so I'm going to summarize what I receive.

NAME        USERNAME      SEC LEVEL      MENU         TAB           DESC   
AIKO R.    ariggins       4              HELP        TIME ENTRY     MESSAGES
AIKO R.    ariggins       4              HELP        ABOUT          ABOUT
AKIO R.    ariggins       4              HELP        CHANGEPASS   CHANGEPASS
.......

What I would like to receive as output is:

 NAME: AKIO R.    USERNAME: ariggins   SECLEVEL: 4
 Menu: HELP > TIME ENTRY > MESSAGES
       HELP > ABOUT > ABOUT
       HELP > CHANGE PASSWORD > CHANGE PASSWORD
.....
 NAME: THE NEXT EMPLOYEE USERNAME: EMPLOYEE USERNAME SECLEVEL: 6
 MENU: HELP > TIME ENTRY > TIME ENTRY
       HELP > ABOUT > ABOUT
       HELP > AD-HOC > AD-HOC
....CONTINUES UNTIL COMPLETE

I'm putting this query into a resultset. If it can be done through a query that would be great. If I need to do this in my application code some guidance would be appericated too because I'm not sure how to produce this output.

This sort of thing is often done in application code (java, php, etc).

MySQL can do it internally with a nonstandard aggregate function known as GROUP_CONCAT()

Try something like this .... (not debugged).

Select usname as Name, 
       Ususer as Username,
       Uswosecl as WO_SecurityLevel,
       AoOpID as Operation_ID, aoseclevel as Operation_Security,
       GROUP_CONCAT(CONCAT( AoMenu, '>' , AoMenuItem , '>', AoMenuItem )) menu
  from whatever
  join whatever ON ....
  where ...
  group by  usname  Ususer, Uswosecl, AoOpID, aoseclevel
  order by usname, ususer

Depending on how many different menu detail items each user has, you may want to use this

 SET group_concat_max_len = 10000;

so the list won't get truncated. You can use any reasonable length. You may need to look at max-packet-length if you have many hundreds of items.

Personally I don't think the query is the right place to do the formatting. Instead I would have a query return the data in columns and then format it in my code or reporting software.

But, to answer your question, you could union the list of users to get the Names and then sort the results to get the menus interspersed with the rights. The row_number is used to hide the menu on the first item.

Note: I removed some of the fields to simplify, but you can add them back.

SELECT case when x.MenuName IS NULL THEN 'Name: ' + x.Name + ' UserName: ' + x.UserName'
ELSE case when rn=1 then 'Menu: ' else '     ' end + x.Menu END Output
FROM
(Select usname Name, Ususer UserName, NULL Menu, 0 rn
From  cudtatet.xxpuser
UNION
Select usname as Name,
Ususer as Username,
AoMenu as Menu,
Row_Number() Over (Partition BY usname Order BY AoMenu) rn
from cudtatet.xxpuser
join fmsusrfua.xxpauops on uswosecl >= aoseclevel
and aoseclevel >= 0
where ususer NOT IN (‘*ALL’,’daffron’)
and aoAuOpID >=70000 and aoAuOpID < 80000)
) x
ORDER BY x.Name, x.rn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM