Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Click here to copy the following block
Create Table #Repair(
Column1 int, Column2 int, Column3 int,
);
Insert Into #Repair VALUES( 1, 2, 3 );
Insert Into #Repair VALUES( 3, 4, 5 );

SELECT Column1, Column2, Column3
FROM #Repair
ORDER BY Column1
COMPUTE SUM( Column2 );
-- Output of SELECT statement using the COMPUTE clause
-- Please note that this generates multiple resultsets.
/*
Column1   Column2   Column3  
----------- ----------- -----------
     1      2      3
     3      4      5

      sum
      ===========
      6
*/


-- It requires more programming to handle the multiple
-- resultsets generated by COMPUTE & it gets messy if
-- you are calculating values based on multiple columns.
-- Hence, the alternative SELECT approach using ROLLUP.
SELECT Column1, Column2, Column3 , SUM( Column2 ) AS Overall_Sum
FROM #Repair
GROUP BY Column1 , Column2 , Column3
WITH ROLLUP
HAVING (
    GROUPING( Column1 ) = 0 And
    GROUPING( Column2 ) = 0 And
    GROUPING( Column3 ) = 0
    ) or
    (
    GROUPING( Column1 ) = 1 And
    GROUPING( Column2 ) = 1 And
    GROUPING( Column3 ) = 1
    )
-- Use ORDER BY to push the summary / overal sum row to the end
ORDER BY COALESCE( Column1 , 2147483647 /* Maximum INTEGER value */);

-- Output of SELECT statement:
/*
Column1   Column2   Column3   Overall_Sum
----------- ----------- ----------- -----------
     1      2      3      2
     3      4      5      4
NULL    NULL    NULL         6
*/


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.