Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQueryModel GROUP BY
Forum Updated to NodeBB v4.3 + New Features

QSqlQueryModel GROUP BY

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 268 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    ZNohre
    wrote on last edited by
    #1

    Does QSqlQueryModel support the GROUP BY statement?

    The below code is returning the properly grouped records, but the sum field is incorrectly returning 0 despite other information in the database.

        QSqlQueryModel m;
        m.setQuery("SELECT ClassCode, SUM(EstimatedChargeablePayroll) AS Total FROM Payroll GROUP BY ClassCode")
    

    Screenshot of the connected view:

    f5150596-db19-4ff2-9d90-8d2aa0c28bb6-image.png

    Screenshot of SQL Server Management Studies with the same query:

    9ecf99b3-0c9b-47b6-83d1-d74808e46860-image.png

    JonBJ 1 Reply Last reply
    0
    • Z ZNohre

      Does QSqlQueryModel support the GROUP BY statement?

      The below code is returning the properly grouped records, but the sum field is incorrectly returning 0 despite other information in the database.

          QSqlQueryModel m;
          m.setQuery("SELECT ClassCode, SUM(EstimatedChargeablePayroll) AS Total FROM Payroll GROUP BY ClassCode")
      

      Screenshot of the connected view:

      f5150596-db19-4ff2-9d90-8d2aa0c28bb6-image.png

      Screenshot of SQL Server Management Studies with the same query:

      9ecf99b3-0c9b-47b6-83d1-d74808e46860-image.png

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @ZNohre
      QSqlQueryModel does not understand, use or look at the SQL statement. That is interpreted at the database and the result set sent back. I don't know why you are seeing what you show.

      Z 1 Reply Last reply
      1
      • JonBJ JonB

        @ZNohre
        QSqlQueryModel does not understand, use or look at the SQL statement. That is interpreted at the database and the result set sent back. I don't know why you are seeing what you show.

        Z Offline
        Z Offline
        ZNohre
        wrote on last edited by
        #3

        @JonB Thanks, this rules out the issues regarding preparation/interpretation of the query so I'm now concerned it's an issue related to the results being sent back/presentation of the data by QSqlQueryModel. I ran a debug on the executed record and I'm getting the below.

        QSqlRecord(2)
         0: QSqlField("ClassCode", QString, tableName: "Payroll", length: 255, required: no, generated: yes, autoValue: false, readOnly: false) "" 
         1: QSqlField("Total", double, tableName: "(not specified)", length: 38, precision: 2, required: no, generated: yes, autoValue: false, readOnly: false) "0"
        

        Nothing stands out to me here, especially since the simplified query without GROUP BY is properly displaying the results to the view, and the record information looks identical

        setQuery("SELECT SUM(EstimatedChargeablePayroll) AS Total FROM Payroll");
        

        Results in the correct data being shown based on my test data:

        65d46644-e917-4433-9fdd-005e391d288f-image.png

        qDebug output:

        QSqlRecord(1)
         0: QSqlField("Total", double, tableName: "(not specified)", length: 38, precision: 2, required: no, generated: yes, autoValue: false, readOnly: false) "0"
        

        If anyone has confirmation that the GROUP BY statement is working in their QSqlQueryModel it would be appreciated.

        I'll keep digging.

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I don't see why it should not work. Please provide a minimal, compilable example of the problem - an in-memory sqlite db + some code, think it should not take more than 50loc to create a reproducer here.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • Z ZNohre

            @JonB Thanks, this rules out the issues regarding preparation/interpretation of the query so I'm now concerned it's an issue related to the results being sent back/presentation of the data by QSqlQueryModel. I ran a debug on the executed record and I'm getting the below.

            QSqlRecord(2)
             0: QSqlField("ClassCode", QString, tableName: "Payroll", length: 255, required: no, generated: yes, autoValue: false, readOnly: false) "" 
             1: QSqlField("Total", double, tableName: "(not specified)", length: 38, precision: 2, required: no, generated: yes, autoValue: false, readOnly: false) "0"
            

            Nothing stands out to me here, especially since the simplified query without GROUP BY is properly displaying the results to the view, and the record information looks identical

            setQuery("SELECT SUM(EstimatedChargeablePayroll) AS Total FROM Payroll");
            

            Results in the correct data being shown based on my test data:

            65d46644-e917-4433-9fdd-005e391d288f-image.png

            qDebug output:

            QSqlRecord(1)
             0: QSqlField("Total", double, tableName: "(not specified)", length: 38, precision: 2, required: no, generated: yes, autoValue: false, readOnly: false) "0"
            

            If anyone has confirmation that the GROUP BY statement is working in their QSqlQueryModel it would be appreciated.

            I'll keep digging.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @ZNohre
            As I said, like @Christian-Ehrlicher I would have expected what you have to work. I just don't see what relevance the GROUP BY should have.

            I would quickly test the following:

            SELECT ClassCode, 350000.00 AS Total FROM Payroll GROUP BY ClassCode
            

            I would also test your

            SELECT ClassCode, SUM(EstimatedChargeablePayroll) AS Total FROM Payroll GROUP BY ClassCode
            

            as a QSqlQuery without the Model. And examine results from all of these directly from model, eliminating the view.

            Z 1 Reply Last reply
            1
            • JonBJ JonB

              @ZNohre
              As I said, like @Christian-Ehrlicher I would have expected what you have to work. I just don't see what relevance the GROUP BY should have.

              I would quickly test the following:

              SELECT ClassCode, 350000.00 AS Total FROM Payroll GROUP BY ClassCode
              

              I would also test your

              SELECT ClassCode, SUM(EstimatedChargeablePayroll) AS Total FROM Payroll GROUP BY ClassCode
              

              as a QSqlQuery without the Model. And examine results from all of these directly from model, eliminating the view.

              Z Offline
              Z Offline
              ZNohre
              wrote on last edited by
              #6

              @JonB Solved!! Thank you so much for the help here troubleshooting which made the issue obvious after trying your suggested tests.

              As you expected, the QSqlQuery is not the issue as the proper data is being returned;

              Test #2

                  QSqlQuery q;
                  qDebug() << "Prep: " << q.prepare("SELECT ClassCode, SUM(EstimatedChargeablePayroll) AS Total FROM Payroll GROUP BY ClassCode");
                  qDebug() << "Exec: " << q.exec();
              
                  while(q.next()){
                      QVariant code = q.value(0);
                      QVariant total =  q.value(1);
              
                      qDebug() << code << total;
                  }
              

              Results in the correct records being returned.

              Prep:  true
              Exec:  true
              QVariant(QString, "5555") QVariant(double, 350000)
              QVariant(QString, "6666") QVariant(double, 50000)
              

              This completely ruled out the QSqlQueryModel issue and forced me to review the rest of the code. The class I've been working on is derived from QSqlQueryModel and has an overridden data() method that was not properly presenting the data to the view. The model was always working in the background it just didn't look like it.

              @Christian-Ehrlicher, I was spooling up the example when this dawned on me, but appreciate the assist here.

              1 Reply Last reply
              0
              • Z ZNohre has marked this topic as solved on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved