package bazy.sqlite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Grupowanie {

	public static void main(String[] args) {
		final String sql = "SELECT job_id, job_title, count(*), avg(salary)"
				+ " FROM employees JOIN jobs USING(job_id)"
				+ " GROUP BY job_id, job_title"
				+ " ORDER BY job_title";
		
		try (Connection c = DriverManager.getConnection("jdbc:sqlite:hr.db");
			PreparedStatement stmt = c.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery()) {
			
			while(rs.next()) {
				String job = rs.getString(1);
				String jobTitle = rs.getString(2);
				int ilosc = rs.getInt(3);
				double srednia = rs.getDouble(4);
				System.out.printf("| %-10s | %-32s | %2d | %8.2f |\n", job, jobTitle, ilosc, srednia);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}






