Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sum on Union
Message
 
 
To
04/02/2013 10:47:39
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Environment versions
SQL Server:
SQL Server 2008
Application:
Web
Miscellaneous
Thread ID:
01565061
Message ID:
01565066
Views:
43
>I have found this, but this requires an additional step:
>
>
>UPDATE Menu SET Count=Temp2.RecordCount FROM Menu JOIN
>(SELECT Menu2.Numero,SUM(RecordCount) As RecordCount FROM
>(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) Temp
>INNER JOIN Menu AS Menu2 ON Temp.Numero=Menu2.Numero
>GROUP BY Menu2.Numero) AS Temp2 ON Temp2.Numero=Menu.Numero
>
>
>If there is a way to shortening that out, you may let me know.

No, this is an OK way although I would rather use MERGE command instead. Also, if the inner select always returns 2 rows and you want it to return 2 rows, use UNION ALL instead of UNION.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform