Results 1 to 7 of 7

Thread: sql select sum (column) not correct sum?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2023
    Posts
    128

    sql select sum (column) not correct sum?

    this give me not the corrected sum(42), suppose to be 43,91

    Gold_gr is a text field column which has numbers

    Code:
    Using conn As New SQLiteConnection(connection)
        conn.Open()
        Dim query As String = "SELECT SUM(Gold_gr) from users"
        Using command As New SQLiteCommand(query, conn)
            TextBox6.Text = Convert.ToString(command.ExecuteScalar())
        End Using
    Attached Images Attached Images  
    Last edited by marsias; Nov 26th, 2024 at 11:31 AM.
    Many ways exist to Babylon.
    Only the shortest is the Best.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,845

    Re: sql select sum (column) not correct sum?

    Text fields aren’t number fields. It looks like the sql is trying to convert the text to numbers, but because they aren’t valid decimal numbers (7.32 instead of 7,32) it isn’t interpreting it correctly.

  3. #3
    Member
    Join Date
    Sep 2024
    Posts
    49

    Re: sql select sum (column) not correct sum?

    Code:
    Dim connectionString As String = "Your_Connection_String_Here"
    Dim query As String = "SELECT SUM(ColumnName) FROM YourTableName"
    Dim total As Decimal
    
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(query, connection)
        connection.Open()
        total = Convert.ToDecimal(command.ExecuteScalar())
    End Using
    
    Console.WriteLine("Total Sum: " & total)

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,845

    Re: sql select sum (column) not correct sum?

    Quote Originally Posted by Red.x View Post
    Code:
    Dim connectionString As String = "Your_Connection_String_Here"
    Dim query As String = "SELECT SUM(ColumnName) FROM YourTableName"
    Dim total As Decimal
    
    Using connection As New SqlConnection(connectionString)
        Dim command As New SqlCommand(query, connection)
        connection.Open()
        total = Convert.ToDecimal(command.ExecuteScalar())
    End Using
    
    Console.WriteLine("Total Sum: " & total)
    command.ExecuteScalar() Returns an Object. Converting it to Decimal at that point wouldn’t fix the problem.
    I’m still fairly sure it’s because of CultureInfo Decimal formatting…

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,913

    Re: sql select sum (column) not correct sum?

    Quote Originally Posted by marsias View Post
    Gold_gr is a text field column which has numbers
    Why? Do you have control of the database? If so, you should change that.

    Failing that, you will need to actually convert the text to a number explicitly before summing, instead of expecting the system to do it for you and then complaining that it doesn't do it how you want. The first thing you should do is search for information on how to convert text to a number in SQLite.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,913

    Re: sql select sum (column) not correct sum?

    This question really has nothing to do with VB.NET and is purely related to SQL and SQLite, so it has been moced to the Database Development forum.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,872

    Re: sql select sum (column) not correct sum?

    Yepp.
    Definitely the Column as Text issue instead of Datatype REAL

    6 x 7.32 = 43.92
    6 x 7 = 42
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Posting Permissions

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



Click Here to Expand Forum to Full Width