You need to sign in or sign up before continuing.
Commit d6014c58 by Yovan Martinez

Terminado las consultas en java con la base de datos

parents
Crear una base de datos que se llame bootcamp_market y restaurar la base de datos con el backup del drive.
Consultas a realizar:
* Top clientes con más facturas
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
//Woody,id:20,con 12 facturas
* Top clientes que más gastaron
SELECT cliente_id, cliente.nombre, 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
* Top monedas más utilizadas
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
//Ruble,id:5,39 facturas pagadas con la misma
* Top proveedor de productos(Que empresa vende mas productos)
//Necesito producto,proveedor y factura detalle
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
//Emard, Luettgen and Kozey, id:48,total 10
* Productos más vendidos
//Energy drink - Franks Original, id:75, 468
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
* Productos menos vendidos
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
* Consulta que muestre fecha de emision de factura, nombre y apellido de cliente, nombres de productos de esa factura, cantidades compradas, nombre de tipo de factura de una factura especfica
fecha de emision =factura
nombre y apellido =cliente
nombre de producto =producto
nombre de productos de esa factura = producto JOIN factura_detalle ON producto.id=factura_detalle.producto_id
cantidades compradas = detalles facturas
nombre de tipo de factura = factura_tipo
factura - cliente_id = cliente.nombre - cliente_id = cliente.apellido
cliente - NADA.......KARAJO
producto - proveedor_id = proveedor.id
detalle producto
tipo factura
SELECT factura.fecha_emision, cliente.nombre, cliente.apellido, producto.nombre,
factura_detalle.cantidad,factura_tipo.nombre
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 = 1
* Montos de facturas ordenadas segun totales
* Mostrar el iva 10% de los montos totales de facturas (suponer que todos los productos tienen IVA 10%)
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
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");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment