Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sum on Union
Message
From
04/02/2013 10:26:42
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Sum on Union
Environment versions
SQL Server:
SQL Server 2008
Application:
Web
Miscellaneous
Thread ID:
01565061
Message ID:
01565061
Views:
58
I have a SQL command like this:
SELECT Menu.Numero,COUNT(*) AS RecordCount
 FROM Menu
 INNER JOIN MenuEntity ON Menu.Numero=MenuEntity.NoMenu
 INNER JOIN MenuPrivilege ON Menu.Numero=MenuPrivilege.NoMenu
 INNER JOIN MenuRole ON Menu.Numero=MenuRole.NoMenu
 INNER JOIN Member ON MenuEntity.NoEntity=Member.NoEntity AND
  MenuPrivilege.NoType=Member.NoType AND MenuRole.NoRole=Member.NoRole
 WHERE Menu.Numero NOT IN (SELECT Menu.Numero FROM MenuPrivilegeExceptionCase
 INNER JOIN Menu ON MenuPrivilegeExceptionCase.NoMenu=Menu.Numero
 WHERE MenuPrivilegeExceptionCase.NoMember=Member.Numero)
 GROUP BY Menu.Numero
UNION
 SELECT Menu.Numero,COUNT(*) AS RecordCount
 FROM Menu
 INNER JOIN MenuPrivilegeExceptionCase ON Menu.Numero=MenuPrivilegeExceptionCase.NoMenu AND
  MenuPrivilegeExceptionCase.Enabled=1
 INNER JOIN MenuEntity ON Menu.Numero=MenuEntity.NoMenu
 INNER JOIN Member ON MenuEntity.NoEntity=Member.NoEntity
 WHERE MenuPrivilegeExceptionCase.NoMember=Member.Numero
 GROUP BY Menu.Numero
This gets me two records for each group by. Each two-record set contains a count for the first part of the SQL command and another count for the second part of the SQL command. So, I can obtain something like this:

1 25
1 108
2 499
2 105

The goal is to update the Menu.Count field with the sum of those two counts.

So, I tried something like this:
UPDATE Menu SET Count=Temp.RecordCount FROM Menu JOIN
(SELECT Menu.Numero,COUNT(*) AS RecordCount
 FROM Menu
 INNER JOIN MenuEntity ON Menu.Numero=MenuEntity.NoMenu
 INNER JOIN MenuPrivilege ON Menu.Numero=MenuPrivilege.NoMenu
 INNER JOIN MenuRole ON Menu.Numero=MenuRole.NoMenu
 INNER JOIN Member ON MenuEntity.NoEntity=Member.NoEntity AND
  MenuPrivilege.NoType=Member.NoType AND MenuRole.NoRole=Member.NoRole
 WHERE Menu.Numero NOT IN (SELECT Menu.Numero FROM MenuPrivilegeExceptionCase
 INNER JOIN Menu ON MenuPrivilegeExceptionCase.NoMenu=Menu.Numero
 WHERE MenuPrivilegeExceptionCase.NoMember=Member.Numero)
 GROUP BY Menu.Numero
UNION
 SELECT Menu.Numero,COUNT(*) AS RecordCount
 FROM Menu
 INNER JOIN MenuPrivilegeExceptionCase ON Menu.Numero=MenuPrivilegeExceptionCase.NoMenu AND
  MenuPrivilegeExceptionCase.Enabled=1
 INNER JOIN MenuEntity ON Menu.Numero=MenuEntity.NoMenu
 INNER JOIN Member ON MenuEntity.NoEntity=Member.NoEntity
 WHERE MenuPrivilegeExceptionCase.NoMember=Member.Numero
 GROUP BY Menu.Numero) AS Temp ON Temp.Numero=Menu.Numero
But, that did not give me the expected result as it updates the Menu.Count field but with only the first count. There has to be something simple to adjust here to obtain the desired result.
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Next
Reply
Map
View

Click here to load this message in the networking platform