Vous êtes sur la page 1sur 5

alter authorization on database::ventas

to sa
/*Consulta SQL que liste a todos los vendedores
que atendieron facturas, asimismo, agregar el
nombre del distrito donde vide el vendedor*/
select fac.COD_VEN,NOM_VEN + ' ' +APE_VEN,
NOM_DIS,FEC_FAC,EST_FAC
from tb_vendedor as ven
inner join tb_factura as fac
on fac.cod_ven = ven.cod_ven
inner join tb_distrito as d
on d.cod_dis = ven.cod_dis
/*Consulta SQL que liste a todos los clientes
que adquirieron productos, asimismo, agregar
el nombre del distrito donde vive el cliente*/
select C.COD_CLI,RAZ_SOC_CLI,NUM_FAC,FEC_FAC,NOM_DIS
from tb_cliente as c
inner join tb_factura as f
on c.cod_cli=f.cod_cli
inner join tb_distrito as d
on d.cod_dis = c.cod_dis
/*Consulta SQL que liste a todos los clientes
que adquirieron productos, asimismo, agregar
el nombre del distrito donde vive el cliente.
Solo listar a los clientes con facturas registradas
entre los años 2010 al 2012*/
select C.COD_CLI,RAZ_SOC_CLI,NUM_FAC,FEC_FAC,NOM_DIS
from tb_cliente as c
inner join tb_factura as f
on c.cod_cli=f.cod_cli
inner join tb_distrito as d
on d.cod_dis = c.cod_dis
where year(fec_fac) between 2010 and 2012
/*
year = Capturar el año
month = Capturar el mes
day = Capturar el dia
*/
/*Subconsultas*/

/*Listar a todos los clientes que tienen facturas


entre los años 2010 al 2012*/
select *
from tb_cliente
where cod_cli IN(select cod_cli
from tb_factura

where year(fec_fac) between 2010 and 2012)

/*Listar todos los distritos donde NO viven


clientes*/
/*combinaciones externas - LEFT*/
select d.cod_dis,nom_dis
from tb_distrito as d
left join tb_cliente as c
on d.cod_dis = c.cod_dis
where c.cod_cli is null
/*SUBCONSULTAS*/
select *
from tb_distrito
where cod_dis not in(select cod_dis
from tb_cliente)
/*Listar a todos los vendedores que viven en
surco*/

select *
from tb_vendedor
where cod_dis = (select cod_dis from tb_distrito
where nom_dis = 'surco')

/*Listar a todos los productos que tienen cantidades


(CAN_VEN) mayor a 10
*/
select *
from tb_producto
where cod_pro IN (select cod_pro
from tb_detalle_factura
where can_ven >10)
/*exists*/
select p.*
from tb_producto as p
where exists(select cod_pro
from tb_detalle_factura as df
where df.can_ven >10 and
df.cod_pro=p.cod_pro)

/*con joins*/
select distinct p.*
from tb_producto as p
join tb_detalle_factura as f
on p.cod_pro = f.cod_pro
where can_ven >10
order by des_pro asc
/*Consulta que liste a todos los productos cuyo
precio es mayor o igual al precio de venta de la
tabla detalle de factura*/
select *
from tb_producto
where pre_pro >any(select pre_ven
from tb_detalle_factura
where pre_ven >=5)
/*Grupos*/
select * from tb_detalle_factura
/*Funciones de agregado
AVG = PROMEDIO
SUM = SUMA
COUNT = CANTIDAD DE FILAS
MAX = MAXIMO
MIN = MINIMO
*/
/*Cantidad de registros de la tabla tb_producto*/
select count(*)as cantidad,avg(pre_pro),
max(pre_pro),min(pre_pro), sum(pre_pro)
from tb_producto
/*Listar el codigo de la factura con el total
de precio*/
select num_fac,sum(pre_ven) as PrecioTotal
from tb_detalle_factura
group by num_fac
having sum(pre_ven)>10
/**/
select * from tb_detalle_factura
/*Listar el codigo de la factura con el total
de precio. El numero de factura sea igual a FA001*/
select num_fac,sum(pre_ven) as PrecioTotal
from tb_detalle_factura
where num_fac = 'fa001'
group by num_fac
having sum(pre_ven)>10
/**/
/*Listar el codigo de la factura con el total*/
select num_fac,sum(pre_ven*can_ven) as PrecioTotal
from tb_detalle_factura
where num_fac = 'fa001'
group by num_fac
having sum(pre_ven)>10
/*Consulta que muestre el codigo del cliente y
razon social, junto a la cantidad de facturas
que tiene*/
select c.cod_cli,raz_soc_cli, count(*) as cantidad
from tb_cliente as c
inner join tb_factura as f
on c.cod_cli=f.cod_cli
group by c.cod_cli,raz_soc_cli
/*Consulta que muestre el codigo del cliente,
razon social, nombre del distrito, junto con el
monto total de las factura del cliente*/
select c.cod_cli, raz_soc_cli, nom_dis,
sum(can_ven*pre_ven) as Total
from tb_cliente as c
join tb_distrito as d on c.cod_dis=d.cod_dis
join tb_factura as f on c.cod_cli=f.cod_cli
join tb_detalle_factura as df
on df.num_fac=f.num_fac
group by c.cod_cli, raz_soc_cli, nom_dis
/*Consulta que muestre el nombre del distrito
junto con el monto total vendido por distrito*/

alter authorization on database::ventas


to sa
/*Listar a todos los distritos donde
no viven clientes*/
select D.COD_DIS,NOM_DIS,COD_CLI,
RAZ_SOC_CLI,CLI.COD_DIS
from tb_distrito as d
left join tb_cliente as cli
on cli.cod_dis=d.cod_dis
where cli.cod_dis is null
--con join
select D.COD_DIS,NOM_DIS,COD_CLI,
RAZ_SOC_CLI,CLI.COD_DIS
from tb_distrito as d
join tb_cliente as cli
on cli.cod_dis=d.cod_dis
--derecha
select D.COD_DIS,NOM_DIS,COD_CLI,
RAZ_SOC_CLI,CLI.COD_DIS
from tb_cliente as cli
right join tb_distrito as d
on cli.cod_dis=d.cod_dis
where cli.cod_dis is null
--Full
select D.COD_DIS,NOM_DIS,COD_CLI,
RAZ_SOC_CLI,CLI.COD_DIS
from tb_distrito as d
full join tb_cliente as cli
on cli.cod_dis=d.cod_dis
where cli.cod_dis is null
--Listar al vendedor que atendió la factura
--FA003
--CON JOINS
SELECT V.COD_VEN,NOM_VEN + ' ' + APE_VEN
FROM TB_VENDEDOR AS V
INNER JOIN TB_FACTURA AS F
ON V.COD_VEN = F.COD_VEN
WHERE F.NUM_FAC='FA003'
--SUB CONSULTA
SELECT COD_VEN,NOM_VEN + ' ' + APE_VEN
FROM TB_VENDEDOR
WHERE COD_VEN = ( SELECT COD_VEN
FROM TB_FACTURA
WHERE NUM_FAC='FA003' )
--Listar a todos los distritos donde no
--viven clientes
SELECT *
FROM TB_DISTRITO
WHERE COD_DIS
NOT IN(SELECT COD_DIS FROM TB_CLIENTE)
--Listar a todos clientes que generaron
--facturas entre los años 2010 al 2015
select *
from tb_cliente
where cod_cli IN
( SELECT cod_cli
FROM tb_factura
where year(fec_fac) between 2010 and 2015)
--con exists
select c.*
from tb_cliente as c
where exists(
select cod_cli
from tb_factura as f
where f.cod_cli = c.cod_cli
AND
year(fec_fac) between 2010 and 2015
)
--
--Listar a todos los productos con precios
--mayores o iguales al precio de venta
--de la tabla tb_detralle_factura
select *
from tb_producto
where pre_pro >= any(select pre_ven
from tb_detalle_factura
)
--Grupos
select * from tb_detalle_factura
--Funciones de agregado
/*
sum = sumar
max = el maximo
min = el minimo
avg = promedio
count = cantidad de registros
*/
select count(*) from tb_cliente
go
select sum(pre_pro),max(pre_pro), min(pre_pro),
avg(pre_pro), count(*)
from tb_producto
go
--Listar a todos las facturas junto con el
--total de precios por factura
select num_fac,sum(pre_ven) as precio
from tb_detalle_factura
group by num_fac
----Listar a todos las facturas junto con el
--total de precios por factura siempre y cuando
--el total de precios es mayor a 10
--Having es igual a where pero para
--grupos
select num_fac,sum(pre_ven) as precio
from tb_detalle_factura
group by num_fac
having sum(pre_ven) > 10
--Listar a todos las facturas junto con el
--total de precios por factura siempre y cuando
--el total de precios es mayor a 10, así mismo
--considerar el num_fac igual a FA001
select num_fac,sum(pre_ven) as precio
from tb_detalle_factura
WHERE num_fac = 'FA001'
group by num_fac
having sum(pre_ven) > 10

Vous aimerez peut-être aussi