GROUP BY子句在生成报告时出现问题。

huangapple go评论74阅读模式
英文:

GROUP BY clause issue when generating reports

问题

我正在使用IntelliJ编写一个Java应用程序,使用SQL数据库进行预约处理。在其中的一个部分

英文:

I am writing a Java application in IntelliJ using a SQL database for appointment handling. For one portion, I must generate reports of 3 types: Number of appointment types by month, schedule of each consultant, and total number of appointments this year. I previously had this working (and haven't changed anything), and now it is returning errors on the first two report generation:

Error Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'U07sym.appointment.start' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

and

Error Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'U07sym.appointment.description' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

I have tried a few of the suggestions, but they seem to require 'super' access, and since this is a DB hosted at my school, I don't have that access.

Here is the code for the 1,2 and 3 report generator:

public void generateFirstReport() {

    try {
        
        Statement statement = DBConnection.getConnection().createStatement();
        String appointmentTypeQuery = "SELECT description, MONTHNAME(start) as 'Month', COUNT(*) as 'Total' FROM appointment GROUP BY description, MONTH(START)";
        
        ResultSet appointmentQueryResults = statement.executeQuery(appointmentTypeQuery);
        
        StringBuilder parseReport = new StringBuilder();
        
        parseReport.append(String.format("%1$-60s %2$-60s %3$s \n", "Month", "Appointment Type", "Total"));
        parseReport.append(String.join("", Collections.nCopies(163, "-")));
        parseReport.append("\n");
        
        while(appointmentQueryResults.next()) {
            
            parseReport.append(String.format("%1$-56s %2$-60s %3$s \n", appointmentQueryResults.getString("Month"), appointmentQueryResults.getString("description"), appointmentQueryResults.getInt("Total")));
        }
        
        typesOfMonthsText.setText(parseReport.toString());
    }
    
    catch(SQLException ex) {
        System.out.println("Error " + ex.getMessage());
    }
}
    
public void generateSecondReport() {
    
    try {
        
        Statement statement = DBConnection.getConnection().createStatement();
        String consultantQueryResults = "SELECT appointment.contact, appointment.description, customer.customerName, start, end " + "FROM appointment JOIN customer ON customer.customerId = appointment.customerId " +
                "GROUP BY appointment.contact, MONTH(start), start";
        
        ResultSet appointmentQueryResults = statement.executeQuery(consultantQueryResults);
        
        StringBuilder parseReport = new StringBuilder();
        parseReport.append(String.format("%1$-45s %2$-45s %3$-45s %4$-45s %5$s \n", "Consultant", "Appointment", "Customer", "Start", "End"));
        parseReport.append(String.join("", Collections.nCopies(163, "-")));
        parseReport.append("\n");
        
        while(appointmentQueryResults.next()) {
            
            parseReport.append(String.format("%1$-37s %2$-50s %3$-35s %4$-35s %5$s \n", 
                appointmentQueryResults.getString("contact"), appointmentQueryResults.getString("description"), appointmentQueryResults.getString("customerName"),
                appointmentQueryResults.getString("start"), appointmentQueryResults.getString("end")));
        }
        
        scheduleOfConsultantText.setText(parseReport.toString());         
        
    }
    catch(SQLException ex) {
       System.out.println("Error " + ex.getMessage()); 
    }       
}

public void generateThirdReport () {
    
    try {
        Statement statement = DBConnection.getConnection().createStatement();
        //"SELECT description, MONTHNAME(start) as 'Month', COUNT(*) as 'Total' FROM appointment GROUP BY description, MONTH(START)";
        String appointmentQueryResults = "SELECT YEAR(start) as 'Year', COUNT(*) as 'Total' FROM appointment GROUP BY YEAR(start)";
        
        ResultSet yearlyQueryResults = statement.executeQuery(appointmentQueryResults);
        
        StringBuilder parseReport = new StringBuilder();
        parseReport.append(String.format("%1$-50s %2$-50s \n", "Year", "Total"));
        parseReport.append(String.join("", Collections.nCopies(163, "-")));
        parseReport.append("\n");
        
        while(yearlyQueryResults.next()) {
            
            parseReport.append(String.format("%1$-50s %2$-50s \n", yearlyQueryResults.getString("Year"), yearlyQueryResults.getString("Total")));
        }
        
        totalAppointmentsThisYearText.setText(parseReport.toString());
    }
    
    catch(SQLException ex) {
        System.out.println("Error " + ex.getMessage());
    }
}

答案1

得分: 2

Your MySQL is currently supporting only the only_full_group_by mode, which means that all the columns present in the select clause must be either in group functions like sum, avg, max, etc., or in the group clause.

To resolve this, you need to change your MySQL settings.

If you are using MySQL Workbench on Windows, go to Edit >> Preferences, then select MySQL under the Modeling section.

Refer to the screenshot below:

GROUP BY子句在生成报告时出现问题。

After that, remove only_full_group_by from the MSQ_MODE to be used in generated scripts textbox and click "OK". Then restart your MySQL.

If you are using Linux, connect to MySQL from the Linux terminal.

Execute the following script:

SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
英文:

Your mysql is currently supporting only only_full_group_by means all the columns present in select clause must be in group functions like sum, avg, max etc. or in group clause.

To resolve this you have to change your mysql setting.

If you are using mysql workbench in Windows then goto Edit >> Preferences and then select MySql under Modeling

See below screenshot for reference.

GROUP BY子句在生成报告时出现问题。

After that remove only_full_group_by from MSQ_MODE to be used in generated scripts textbox and click ok. Then restart you mysql.

And if you are using Linux then connect to mysql from Linux terminal.

Execute below script.

SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

huangapple
  • 本文由 发表于 2020年7月24日 23:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63077083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定