简体   繁体   中英

Merge table results using SQL Query

I have these query results from MenuTable for different User Groups. A user can have more Groups. So, I want to merge the permissions for all the Groups for same PageID the User belongs to. How can I get this done using SQL Query?

First Result:

UserGroupID   PageID   CanView     CanDelete     CanEdit
----------------------------------------------------------------------
   1           1         0            0            0
   1           2         1            1            0
   1           3         1            0            0 

Second Result

UserGroupID   PageID   CanView     CanDelete     CanEdit
----------------------------------------------------------------------
   2            1         1            1            1
   2            2         0            0            0
   2            3         0            0            0 

I want to get this Result.

 PageID   CanView     CanDelete     CanEdit
------------------------------------------------------------
   1         1            1            1
   2         1            1            0
   3         1            0            0 

A simple query like this can do :

SELECT PageID,
       MAX(CanView)   [CanView],
       MAX(CanDelete) [CanDelete],
       MAX(CanEdit)   [CanEdit]
FROM   table
--WHERE  UserGroupID in(1,2)
GROUP  BY PageID 

in addition you can use where clause if you want to look in specific groups (commented condition).

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