import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class App { public static void main(String args[]) { Connection c = null; Statement testamento = null; Statement tabla_cliente = null; try { Class.forName("org.postgresql.Driver"); c = DriverManager .getConnection("jdbc:postgresql://localhost:5433/bootcamp_market", "postgres", "postgres"); testamento = c.createStatement(); tabla_cliente = c.createStatement(); ResultSet cliente = tabla_cliente.executeQuery("SELECT * FROM public.cliente"); ResultSet rs = testamento.executeQuery("SELECT cliente_id, nombre, COUNT (cliente_id) AS total FROM factura INNER JOIN cliente ON factura.cliente_id=cliente.id GROUP BY cliente_id, nombre ORDER BY total DESC"); System.out.println("--------------------------------------------------------------------------------------------------------"); //CLIENTE CON MAS FACTURAS System.out.println("*Cliente con mas facturas: "); while (rs.next()) { int cliente_id = rs.getInt("cliente_id"); String nombre = rs.getString("nombre"); int total = rs.getInt("total"); System.out.println("id: " + cliente_id+" nombre: " + nombre + " total:" + total); break; } System.out.println("--------------------------------------------------------------------------------------------------------"); //TOP CLIENTES QUE MAS GASTARON System.out.println("*Top clientes que mas gastaron: "); rs = testamento.executeQuery("SELECT cliente_id, cliente.nombre, TRUNC(SUM(cantidad * precio)) AS total_gastado FROM factura JOIN factura_detalle ON factura.id=factura_detalle.factura_id JOIN producto ON producto.id=factura_detalle.producto_id JOIN cliente ON factura.cliente_id=cliente.id GROUP BY cliente_id, cliente.nombre ORDER BY total_gastado DEsc"); while (rs.next()) { int cliente_id= rs.getInt("cliente_id"); String nombre = rs.getString("nombre"); int total = rs.getInt("total_gastado"); System.out.println("id: "+cliente_id + " nombre: " + nombre + " total gastado: " + total); break; } System.out.println("--------------------------------------------------------------------------------------------------------"); //TOP MONEDAS MAS UTILIZADAS System.out.println("*Top monedas mas utilizadas: "); rs = testamento.executeQuery("SELECT moneda_id, nombre, COUNT (moneda_id) AS total FROM factura INNER JOIN moneda ON factura.moneda_id = moneda.id GROUP BY moneda_id, nombre ORDER BY total DESC"); while (rs.next()) { int moneda_id = rs.getInt("moneda_id"); String nombre = rs.getString("nombre"); int total = rs.getInt("total"); System.out.println("id: " + moneda_id + "\nnombre: " + nombre + "\ntotal:" + total); break; } System.out.println("--------------------------------------------------------------------------------------------------------"); //TOP PROVEEDOR DE PRODUCTOS System.out.println("*Top proveedor de productos: "); rs = testamento.executeQuery("SELECT proveedor_id, proveedor.nombre, COUNT (proveedor_id) AS total FROM producto JOIN proveedor ON producto.proveedor_id = proveedor.id GROUP BY proveedor_id ,proveedor.nombre ORDER BY total DESC"); while (rs.next()) { int proveedor_id = rs.getInt("proveedor_id"); String nombre = rs.getString("nombre"); int total = rs.getInt("total"); System.out.println("Id: " + proveedor_id + "\nNombre: " + nombre + "\nCantidad de productos que provee: " + total); break; } System.out.println("--------------------------------------------------------------------------------------------------------"); //TOP PRODUCTOS MAS VENDIDOS System.out.println("*Top productos mas vendidos: "); rs = testamento.executeQuery("SELECT producto_id, producto.nombre, floor(SUM(cantidad)) AS total FROM factura_detalle JOIN producto ON producto.id=factura_detalle.producto_id GROUP BY producto_id, producto.nombre ORDER BY total DESC"); while (rs.next()) { int producto_id = rs.getInt("producto_id"); String nombre = rs.getString("nombre"); int total = rs.getInt("total"); System.out.println("Id: " + producto_id + "\nNombre: " + nombre + "\nCantidades vendidas: " + total); break; } System.out.println("--------------------------------------------------------------------------------------------------------"); //TOP PRODUCTOS MENOS VENDIDOS System.out.println("*Top productos menos vendidos: "); rs = testamento.executeQuery("SELECT producto_id, producto.nombre, floor(SUM(cantidad)) AS total FROM factura_detalle JOIN producto ON producto.id=factura_detalle.producto_id GROUP BY producto_id, producto.nombre ORDER BY total DESC"); while (rs.next()) { int producto_id = rs.getInt("producto_id"); String nombre = rs.getString("nombre"); int total = rs.getInt("total"); System.out.println("Id: " + producto_id + "\nNombre: " + nombre + "\nCantidades vendidas: " + total); break; } System.out.println("--------------------------------------------------------------------------------------------------------"); //DETALLES CON ID DE FACTURA ESPECIFICO System.out.println("*Detalles de factura: "); int id_factura_especifica = 5; rs = testamento.executeQuery("SELECT factura.fecha_emision, cliente.nombre AS nombre_cliente, cliente.apellido, producto.nombre AS producto, factura_detalle.cantidad,factura_tipo.nombre AS tipo_de_Factura FROM factura JOIN factura_detalle ON factura_detalle.factura_id=factura.id JOIN factura_tipo ON factura.factura_tipo_id=factura_tipo.id JOIN producto ON factura_detalle.producto_id=producto.id JOIN cliente ON factura.cliente_id=cliente.id WHERE factura.id = "+ id_factura_especifica); while (rs.next()) { Date fecha_emision = rs.getDate("fecha_emision"); String cliente_nombre = rs.getString("nombre_cliente"); String cliente_apellido = rs.getString("apellido"); String producto = rs.getString("producto"); int cantidad = rs.getInt("cantidad"); String tipo_de_factura = rs.getString("tipo_de_factura"); System.out.println("Fecha de emision: " + fecha_emision + "\nNombre: " + cliente_nombre + "\nApellido: " + cliente_apellido + "\nProducto: " + producto + "\nCantidad: "+ cantidad + "\nTipo de factura: "+ tipo_de_factura); break; } System.out.println("--------------------------------------------------------------------------------------------------------"); //MONTOS DE FACTURAS ORDENADAS SEGUND TOTALES System.out.println("*Montos de facturas ordenadas segun totales: "); rs = testamento.executeQuery("SELECT factura_id, TRUNC(SUM(cantidad * precio)) AS total_monto FROM factura_detalle JOIN producto ON factura_detalle.producto_id = producto.id JOIN factura ON factura_detalle.factura_id = factura.id GROUP BY factura_id ORDER BY total_monto DESC"); while (rs.next()) { int factura_id = rs.getInt("factura_id"); int cantidad = rs.getInt("total_monto"); System.out.println("Id factura: " + factura_id + "\nNombre: " + "\nTotal: " + cantidad); break; } System.out.println("--------------------------------------------------------------------------------------------------------"); //MOSTRAR EL IVA 10% DE LOS TOTALES DE MONTOS DE FACTURAS System.out.println("*Iva 10% del total de monto de cada fatura: "); rs = testamento.executeQuery("SELECT factura_id, TRUNC(SUM(cantidad * precio )* 0.1) AS total_monto FROM factura_detalle JOIN producto ON factura_detalle.producto_id = producto.id JOIN factura ON factura_detalle.factura_id = factura.id GROUP BY factura_id ORDER BY total_monto DESC"); while (rs.next()) { int factura_id = rs.getInt("factura_id"); int cantidad = rs.getInt("total_monto"); System.out.println("Id factura: " + factura_id + "\nNombre: " + "\nTotal iva 10% : " + cantidad); break; } System.out.println("--------------------------------------------------------------------------------------------------------"); rs.close(); testamento.close(); c.close(); tabla_cliente.close(); cliente.close(); } catch (Exception e) { e.printStackTrace(); System.err.println(e.getClass().getName()+": "+e.getMessage()); System.exit(0); } System.out.println("Opened database successfully"); } }