-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPradosDeEchinacea.sql
More file actions
297 lines (236 loc) · 9.33 KB
/
PradosDeEchinacea.sql
File metadata and controls
297 lines (236 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
-- Base de datos --
create database PradosDeEchinacea
go
-- Usar Base de Datos --
use PradosDeEchinacea
go
-- Tabla Sexos --
create table Sexos
(IdSexo int not null primary key,
NombreSexo varchar(70)
)
Insert into Sexos(IdSexo,NombreSexo) values(1,'Masculino')
Insert into Sexos(IdSexo,NombreSexo) values(2,'Femenino')
-- Tabla Tipos --
create table TiposDeDocumentos
(IdTipo int not null primary key,
NombreTipo varchar(70)
)
select *
from TiposDeDocumentos
Insert into TiposDeDocumentos(IdTipo,NombreTipo) values(1,'DNI')
Insert into TiposDeDocumentos(IdTipo,NombreTipo) values(2,'CI')
Insert into TiposDeDocumentos(IdTipo,NombreTipo) values(3,'CC')
Insert into TiposDeDocumentos(IdTipo,NombreTipo) values(4,'CE')
-- Tabla Clientes --
create table Clientes
(IdCliente int not null identity(1,1) primary key,
NombreCompleto varchar(120),
Telefono int,
IdTipo int not null,
NroDocumento int,
IdSexo int not null,
Calle varchar(70),
NroCalle int,
CodigoPostal int,
Ciudad varchar(70),
Barrio varchar(70),
Email varchar(70)
constraint fk_TiposDeDocumentos_Clientes foreign key (IdTipo)
references TiposDeDocumentos (IdTipo),
constraint fk_Sexos_Clientes foreign key (IdSexo)
references Sexos (IdSexo)
)
select c.IdCliente, c.NombreCompleto, c.Telefono, td.NombreTipo, c.NroDocumento, s.NombreSexo, c.Calle, c.NroCalle, c.CodigoPostal, c.Ciudad, c.Barrio, c.Email
from Clientes c INNER JOIN TiposDeDocumentos td ON c.IdTipo = td.IdTipo
INNER JOIN Sexos s ON c.IdSexo = s.IdSexo
order by c.NombreCompleto
-- Cbo --
insert into Clientes(NombreCompleto)
values('Es un cliente normal')
-- Tabla Productos --
create table Productos
(NroProducto int not null identity(1,1) primary key,
NombreProducto varchar(70),
Descripcion varchar(500),
PrecioUnitario money
)
select PrecioUnitario
from Productos
where NombreProducto = 'Echinacea'
select NroProducto, NombreProducto, Descripcion, cast(PrecioUnitario AS Decimal(8,2)) AS PrecioUnitario
from Productos
where NroProducto = NroProducto
order by NroProducto
--Reportes Productos
select NombreProducto 'Producto'
from Productos
where PrecioUnitario = (select max(PrecioUnitario) from Productos)
-- Tabla Estados --
create table Estados
(IdEstado int not null primary key,
nombreEstado varchar(70)
)
Insert into Estados(IdEstado,nombreEstado) values(1,'En Preparacion')
Insert into Estados(IdEstado,nombreEstado) values(2,'Cancelado')
Insert into Estados(IdEstado,nombreEstado) values(3,'En Curso')
Insert into Estados(IdEstado,nombreEstado) values(4,'Entregado')
-- Tabla Pedidos --
create table Pedidos
(NroPedido int not null identity(1,1) primary key,
Descripcion varchar(500),
IdEstado int,
FechaRealizacionDelPedido varchar(70),
FechaEntrega varchar(70),
IdCliente int not null
constraint fk_Clientes_Pedidos foreign key (IdCliente)
references Clientes (IdCliente),
constraint fk_Estados_Pedidos foreign key (IdEstado)
references Estados (IdEstado)
)
select * from Pedidos where FechaRealizacionDelPedido between '02/07/2017' and '03/04/2017'
select * from Ventas where FechaVenta between '02/07/2017' and '03/04/2017'
select p.NroPedido, p.Descripcion, e.nombreEstado, p.FechaRealizacionDelPedido, p.FechaEntrega, c.NombreCompleto
from Pedidos p INNER JOIN Estados e ON p.IdEstado = e.IdEstado INNER JOIN Clientes c ON p.IdCliente = c.IdCliente
where FechaRealizacionDelPedido between '02/06/2017' and '02/09/2017'
-- Tabla Detalle Pedidos --
create table DetallePedidos
(NroDetalle int not null identity(1,1) primary key,
Cantidad int,
PrecioProducto money,
NroProducto int not null,
NroPedido int not null
constraint fk_Productos_DetallePedidos foreign key (NroProducto)
references Productos (NroProducto),
constraint fk_Pedidos_DetallePedidos foreign key (NroPedido)
references Pedidos (NroPedido)
)
select sum(Cantidad * PrecioProducto) Total
from DetallePedidos
where NroPedido = 1
insert into DetallePedidos(Cantidad,PrecioProducto,NroProducto,NroPedido)
values(5,100,1,1)
select dp.NroDetalle, dp.Cantidad, p.PrecioUnitario, p.NombreProducto, dp.NroPedido
from DetallePedidos dp INNER JOIN Productos p ON dp.NroProducto = p.NroProducto
order by p.NombreProducto
-- Tabla Cobranzas --
create table Cobranzas
(NroCobro int not null identity(1,1) primary key,
Monto money,
FechaDelCobro varchar(70),
IdCliente int not null
constraint fk_Clientes_Cobranzas foreign key (IdCliente)
references Clientes (IdCliente)
)
SET IDENTITY_INSERT Cobranzas ON;
GO
select c.NroCobro, c.Monto, c.FechaDelCobro, cli.NombreCompleto
from Cobranzas c INNER JOIN Clientes cli ON c.IdCliente = cli.IdCliente
order by c.NroCobro
-- Tabla Ventas --
create table Ventas
(NroVenta int not null identity(1,1) primary key,
Importe money,
FechaVenta varchar(70),
NroProducto int,
NombreCliente varchar(70),
NroPedido int not null,
IdCliente int
constraint fk_Pedidos_Ventas foreign key (NroPedido)
references Pedidos (NroPedido),
constraint fk_Productos_Ventas foreign key (NroProducto)
references Productos (NroProducto),
constraint fk_Clientes_Ventas foreign key (IdCliente)
references Clientes (IdCliente)
)
-- Traer datos --
-- Clientes --
select c.NombreCompleto
from Pedidos p INNER JOIN Clientes c ON p.IdCliente = c.IdCliente
where c.NombreCompleto = 'Rodrigo Agostinelli'
select c.NombreCompleto
from Clientes c
where c.NombreCompleto = 'Rodrigo Agostinelli'
-- 1
select c.NombreCompleto
from Clientes c, Pedidos p
where p.IdCliente = c.IdCliente and c.NombreCompleto = 'Rodrigo Agostinelli'
-- 2
select c.NombreCompleto
from Pedidos p INNER JOIN Clientes c ON p.IdCliente = c.IdCliente
where c.NombreCompleto = 'Rodrigo Agostinelli'
select * from Ventas
select v.NroVenta, v.Importe, v.FechaVenta, p.NroPedido
from Ventas v INNER JOIN Pedidos p ON v.NroPedido = p.NroPedido
Order by v.NroVenta
-- Producto --
select p.NombreProducto
from Pedidos pe, Productos p, DetallePedidos dp
where dp.NroProducto = p.NroProducto and dp.NroPedido = pe.NroPedido and p.NombreProducto = 'Cedron'
select COUNT(*) 'Pedido'
from Ventas
where NroPedido = 3
-- Modificaciones --
alter table Ventas
alter column NroProducto int null
alter table Ventas
alter column IdCliente int null
select v.NroVenta, v.Importe, v.FechaVenta, p.NroPedido
from Ventas v INNER JOIN Pedidos p ON v.NroPedido = p.NroPedido
where p.NroPedido = 1
Order by v.NroVenta
-- Reportes --
-- Productos ordenados por cantidad vendida (ranking) venta entre fechas
/*==============================================================================================*/
select top 10 p.NombreProducto, sum(dp.Cantidad) 'TotalProductos' , cast(sum(dp.Cantidad * dp.PrecioProducto) as decimal(8,2)) as 'TotalImporte'
from DetallePedidos dp Inner join Productos p ON dp.NroProducto = p.NroProducto
group by p.NombreProducto, dp.Cantidad, dp.PrecioProducto
Order by dp.Cantidad asc
/*==============================================================================================*/
select Top 10 p.NombreProducto, SUM(p.PrecioUnitario) as 'Total De Ventas'
from Productos p
group by NombreProducto
/*Segundo reporte: Cobranzas por cliente:
1) Permitir que el usuario seleccione las fechas de realización de las cobranzas desde/hasta como filtro.
2) Agrupar por cliente los totales cobrados (sumar los montos).
3) Ordenar por cliente
4) Mostrar un total de montos.*/
-- Cobranzas entre fechas (cliente, fecha, importe, totales)--
select cli.NombreCompleto, Sum(c.Monto) 'TotalDeMontos'
from Cobranzas c INNER JOIN Clientes cli ON c.IdCliente = cli.IdCliente
where FechaDelCobro between '04/27/2017' and '10/27/2017'
group by cli.NombreCompleto, c.Monto
order by cli.NombreCompleto asc
/*Tercer reporte: Ventas entre fechas:
1) Permitir que el usuario seleccione las fechas de venta desde/hasta como filtro.
2) Mostrar 1 registro por venta por cliente, con el nombre del cliente, número del comprobante de venta, fecha de la venta, y total de la venta.
3) Ordenarlo por fecha.
4) Mostrar total de ventas (cuantas ventas realizadas) y total de monto (suma de importes)*/
-- Ventas entre fechas (cliente, fecha importe, totales) --
select v.NroVenta, v.NombreCliente, v.FechaVenta, sum(dp.Cantidad) 'TotaldeVentas', v.Importe 'TotalDelMonto'
from DetallePedidos dp left join Ventas v ON dp.NroPedido = v.NroPedido
where v.FechaVenta = v.FechaVenta
group by v.NroVenta, v.FechaVenta, v.NombreCliente, v.Importe
order by v.FechaVenta, v.NroVenta desc
select *
from Ventas
/*SELECT TOP 10 ART_MOV, SUM(TOT_MOV) AS Total_Ventas
FROM VENTAS
GROUP BY ART_MOV
HAVING DIA_MOV BETTWEN ([Desde Fecha] and [Hasta Fecha])
ORDER BY SUM(TOT_MOV) DESC*/
-- PDF --
/*1) Agregar una columna con la descripción del artículo.
2) Colocar precio unitario (y dejar el total).
3) Dejar solo con 2 decimales el monto y alinearlo a la derecha.
4) Agregar un total general del remito al pié.
5) Una vez que hagas estos ajustes, enviame un remito de por lo menos 2 artículos (2 renglones) y con cantidad mayor a 1.*/
select dp.NroProducto 'N°', p.NombreProducto ,dp.Cantidad, p.PrecioUnitario ,dp.Cantidad * dp.PrecioProducto 'Total'
from DetallePedidos dp INNER JOIN Productos p ON dp.NroProducto = p.NroProducto
where dp.NroPedido = dp.NroPedido and dp.Cantidad > 1
order by dp.NroPedido desc
select v.NroVenta, v.NombreCliente, v.FechaVenta, sum(dp.Cantidad) 'TotaldeVentas', cast(v.Importe as decimal(8,2)) as 'TotalDelMonto'
from DetallePedidos dp left join Ventas v ON dp.NroPedido = v.NroPedido
where v.FechaVenta = v.FechaVenta
group by v.NroVenta, v.FechaVenta, v.NombreCliente, v.Importe
order by v.FechaVenta, v.NroVenta desc