1.-¿cúal es la cantidad de dinero que se recaudo en
arriendo de películas el mes de junio y julio del año
2005 para la tienda 2?
r:
select s.store_id,SUM(p.amount) from payment as p inner join staff as s on (s.staff_id= p.staff_id)
where s.store_id = 2 and DATEPART(YEAR,p.payment_date) = 2005 and DATEPART(MONTH,p.payment_date) in (6,7)
group by s.store_id
--ahi esta listo el 1
2.- ¿Cúal es la cantidad de unidades de películas de
categoría children que fueron arrendadas en la
tienda 2 para el mes de julio y agosto del año 2005?
r:
select count(*) as cantidad_arriendos from rental where
DATEPART(YEAR,rental_date) = 2005 and DATEPART(MONTH,rental_date) in (7,8) and
inventory_id in(select inventory_id
from inventory where film_id in (select film_id from film_category where category_id = 3))
3.- ¿cúal es la cantidad de unidades de peliculas de
categoria "children" que fueron arrendadas el mes de
mayo del año 2005?, en la cual participo el actor
"helen voight"?
r:
select count(*) as cantidad_arriendos_que_participo_helen_voight from rental where
DATEPART(YEAR,rental_date) = 2005 and DATEPART(MONTH,rental_date) in (5) and --mayo 2005
inventory_id in(select inventory_id
from inventory where film_id in (select film_id from film_category where category_id = 3)
And film_id in (select film_id
from film_actor as fa inner join actor as a on (fa.actor_id = a.actor_id)
where a.actor_id = 17)
)
4.- cual es la cantidad de unidades de peliculas que
fueron arendadas por tienda para el mes de febrero
del año 2005?
r:
select * from rental where DATEPART(YEAR,rental_date) = 2005 and DATEPART(MONTH,rental_date) in (2)
--esta es la 4 esta lista
select st.store_id,count(*) as cantidad_arriendos_por_tienda
from rental as r inner join staff as s on (s.staff_id = r.staff_id)
inner join store as st on (st.store_id = s.store_id)
where DATEPART(YEAR,r.rental_date) = 2005 and DATEPART(MONTH,r.rental_date) in (2)
GROUP by st.store_id
5.- ¿Cúal fue el cliente que más arrendo películas el
mes de mayo del 2005?
r:
select top 1 c.first_name,c.last_name,count(r.rental_id) as cantidad_de_arriendo_mes_mayo2005
from customer as c inner join rental as r on (c.customer_id = r.customer_id)
where DATEPART(YEAR,r.rental_date) = 2005 and DATEPART(MONTH,r.rental_date) in (5)
group by c.first_name,c.last_name
order by count(r.rental_id) desc
Cúal es la primera película arrendada?, donde
participo el actor identificado como nº65
select * from actor where actor_id = 65 'ANGELA HUDSON'
select * from film
select * from film
select MIN(rental_date) from rental
select * from inventory
--ahora tenemos que saber cual es la primera peli arrendada, eso lo sacamos con la tabla de rental y un min ? si
SELECT top 1 dbo.film.film_id AS FID,dbo.film.title,MIN(dbo.rental.rental_date) as primera_fecha_arriendo
FROM dbo.category LEFT OUTER JOIN
dbo.film_category ON dbo.category.category_id = dbo.film_category.category_id LEFT OUTER JOIN
dbo.film ON dbo.film_category.film_id = dbo.film.film_id INNER JOIN
dbo.film_actor ON dbo.film.film_id = dbo.film_actor.film_id INNER JOIN
dbo.actor ON dbo.film_actor.actor_id = dbo.actor.actor_id inner join
dbo.inventory on dbo.inventory.film_id = dbo.film.film_id inner join
dbo.rental on dbo.rental.inventory_id = dbo.inventory.inventory_id
where actor.actor_id = 65
group by dbo.film.film_id ,dbo.film.title
order by MIN(dbo.rental.rental_date) asc
7.- ¿Cúal es la cantidad de unidades de películas que
fueron arrendadas con valores mayores a 11usd durante
el mes de julio para la tienda 2?
r:
SELECT s.store_id as tienda, count(p.rental_id) AS Conteo_arriendos
FROM dbo.payment AS p INNER JOIN
dbo.rental AS r ON p.rental_id = r.rental_id INNER JOIN
dbo.inventory AS i ON r.inventory_id = i.inventory_id INNER JOIN
dbo.store AS s ON i.store_id = s.store_id INNER JOIN
dbo.address AS a ON s.address_id = a.address_id INNER JOIN
dbo.city AS c ON a.city_id = c.city_id INNER JOIN
dbo.country AS cy ON c.country_id = cy.country_id INNER JOIN
dbo.staff AS m ON s.manager_staff_id = m.staff_id
where DATEPART(month,r.rental_date)=7
and p.amount > 10 and s.store_id = 2
GROUP BY s.store_id
pu
Buscar este blog
jueves, 23 de julio de 2015
TAREA SQL - SAKILA :P
Etiquetas:
base de datos,
Inacap,
sakila,
SQL,
tarea
lunes, 13 de julio de 2015
COMO SABER SI EXISTE UN OBJETO (TABLA, STORE PROCEDURE) EN SQL
Con este simple script podemos verificar si existe un objeto como una tabla o store procedure en nuestra base de datosSQL utilizando la función EXISTS.
Codigo
USE NombreBaseDeDatos
GO
GO
IF EXISTS (SELECT * FROM sysobjects WHERE name=’sp_Procedimiento‘)
BEGIN
print ‘Existe’
END
print ‘Existe’
END
ELSE BEGIN
print ‘No existe‘
END
print ‘No existe‘
END
Usando la función EXISTS de SQL y haciendo un SELECT a la tabla SYSOBJECTS, podemos saber si existe el objeto que estamos buscando. El resultado de la funcion EXISTS nos devolvera un valor boleano, para este ejemplo utilizamos un IF, el cual, dependiendo del valor devuelto imprimira un mensaje indicandonos si econtro o no el objeto buscado(sp_Procedimiento).
Etiquetas:
exists table,
MSSQL2012,
script,
SQL
Suscribirse a:
Entradas (Atom)