Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2009
    Posts
    90

    Unanswered: Verify Total Value

    My problem is this.
    I have a table which has a total value for each column. Now, I want to compare it to the total values from another column to check if its the same.

    For example:
    I have this original data:

    Code:
    Name       ColumnA            ColumnB        Total Value
    A            2                     6                  8
    B            5                     1                  6
    C            3                     2                  5
    TOTAL       10                    9                    19

    nOW i WANt to verify if the Total Value is correct by comparing it to the sum of the columns or sum of the rows. Can you help me how to do that?

    Thanks in advance!

  2. #2
    Join Date
    Nov 2004
    Posts
    1,427
    Provided Answers: 4
    Code:
    create table DaTable (
    	id integer not null IDENTITY(1, 1),
    	ColumnA		INT,
    	ColumnB		INT,
    	TotalValue	INT,
    	CONSTRAINT pk_DaTable PRIMARY KEY (id)
    );
    
    INSERT INTO DaTable (ColumnA, ColumnB, TotalValue) 
    SELECT 2, 6, 8
    UNION ALL
    SELECT 5, 1, 6
    UNION ALL
    SELECT 3, 2, 6
    
    SELECT SumA, SumB, SumTotal
    FROM (SELECT sum (columnA) as SumA, 
    		sum(ColumnB) as SumB, 
    		sum(TotalValue) as SumTotal
    	FROM DaTable
    	) as T
    WHERE SumA + SumB <> SumTotal
    
    drop table DaTable
    With kind regards . . . . . SQL Server 2000/2005/2012
    Wim

    Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
    Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages

  3. #3
    Join Date
    Sep 2009
    Posts
    90
    THNKS A LOT WIM! Please see my other post.

  4. #4
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Don't Cross Post
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •