Saturday, November 12

Displaying total value of a column in SQL

Im going to show how to display total value of your data in the database. This script will works on integer datatype (Im not sure if it work in currency or money datatype but Maybe it will work).

The SQL syntax is:

SELECT  ISNULL(Column1, 'TheLastWord') AS ALIAS, Column2      
FROM (
      SELECT Column1,
             GROUP_FUNCTION(Column1) AS Column1,
             GROUP_FUNCTION(Column2) AS Column2
      FROM TABLE
          GROUP BY Column1 WITH ROLLUP
     ) AS ALIAS



Example:

SELECT ISNULL(Sales_MONTHS, 'TOTAL SALES in 2011') AS MONTH
   TRANSACTION ,      
   SALES
    
FROM (
      SELECT Sales_MONTHS,
             SUM(TRANSACTION ) AS TRANSACTION ,
             SUM(SALES) AS SALES
           
      FROM SALES_TABLE
          GROUP BY Sales_MONTHS WITH ROLLUP
     ) AS BI






Note that this script or systax works in MS SQL. Other database may have different syntax.
Thats it, I hope this simple tutorial would helped.





3 Then Jesus told them this parable: 4 “Suppose one of you has a hundred sheep and loses one of them. Doesn’t he leave the ninety-nine in the open country and go after the lost sheep until he finds it? 5 And when he finds it, he joyfully puts it on his shoulders 6 and goes home. Then he calls his friends and neighbors together and says, ‘Rejoice with me; I have found my lost sheep.’ 7 I tell you that in the same way there will be more rejoicing in heaven over one sinner who repents than over ninety-nine righteous persons who do not need to repent.

Luke 15:3-7(NIV)

To God all the Glory!

No comments:

Post a Comment