如何在java eclipse中执行数据透视查询

时间:2015-02-03 06:40:47

标签: java mysql servlet-3.0

这是我正在尝试将行转换为列的查询,它在浏览器中完美地工作但在java中

 SET @sql = NULL ;
      SELECT
        GROUP_CONCAT(
          DISTINCT CONCAT(
            'max(CASE WHEN attendance.date = ''',
            DATE_FORMAT(DATE, '%Y-%m-%d'),
            ''' THEN coalesce(p.present, '''') END) AS `',
            DATE_FORMAT(DATE, '%Y-%m-%d'),
            '`'
          )
        ) INTO @sql
      FROM
        calendar
      WHERE DATE >= '2015-01-01'
        AND DATE <= '2015-01-15' ;
      SET @sql = CONCAT(
        'SELECT attendance.user_id, ', @sql,'
            from
                (
                  select c.date, a.user_id
                  from calendar c
                  cross join attendance a
                ) attendance
                left join attendance  p
                  on attendance.user_id= p.user_id
                  and attendance.date = p.attendance_date
               where attendance.date>=''2015-01-01''
                  and attendance.date <= ''2015-01-15''
              group by attendance.user_id'
      ) ;
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

这是我正在尝试的java代码,但我无法得到结果:

 String setsqlMax = "SET SESSION group_concat_max_len = 100000000000;";
                String setsql ="SET @sql = NULL;";
                String dynamicsql = 

                               "SELECT"+
                               " GROUP_CONCAT(DISTINCT"+
                               " CONCAT("+
                               "'max(CASE WHEN attendance.date = ''',"+
                               " date_format(date, '%Y-%m-%d'),"+
                               " ''' THEN coalesce(p.present, '''') END) AS `',"+
                               " date_format(date, '%Y-%m-%d'), '`'"+
                               ")"+
                               " ) INTO @sql"+
                               " FROM calendar"+
                               " where date>='01-01-2015'"+
                               "  and date <= '31-01-2015';";


                String mainsql   =  "SET @sql = CONCAT('SELECT p.user_id,',@sql,'"+
                                " from"+
                                "("+
                                "select c.user_id"+
                                " from calendar c"+
                                " cross join attendance a"+
                                " )"+
                                " attendance LEFT JOIN attendance p"+
                                " on attendance.user_id= p.user_id"+
                               " and attendance.date = p.attendance_date"+
                               " where attendance.date>=''01-01-2015''"+
                               "  and attendance.date <= ''31-01-2015''"+
                               " group by attendance.user_id') ;";
                   String preparesql = " PREPARE stmt FROM @sql";
                   String ExcuteSql = "EXECUTE stmt ;";
                   String deallocateSql = "DEALLOCATE PREPARE stmt;";
        String totalQuery = setsqlMax + setsql + dynamicsql + mainsql + preparesql+ ExcuteSql + deallocateSql;

我正在执行totalQuery Like This

        try {
            connection = DBUtil.getInstance().getConnection();

            callableStatement = connection.prepareStatement(totalQuery);
            resultSet = callableStatement.executeQuery();




            while(resultSet.next()) {

                Attendance attendance = new Attendance();

                attendance.setAttendanceDate(resultSet.getDate("attendance_date"));
                attendance.setPresent(resultSet.getString("present"));
                attendnaces.add(attendance);
            }
        }

0 个答案:

没有答案