DATA WAREHOUSE DW_Lab03.doc GUIA PRACTICA DE LABORATORIO #3 PRÁCTICA 1: QUERYS 1. Retornar solo las columnas (au_fname, au_lname, phone) de authors que viven en California y no tienen de apellido McBadden. Se ha adicionado un encabezado de Columna. SELECT au_fname, au_lname, phone AS Telephone FROM authors WHERE state = 'CA' and au_lname <> 'McBadden' ORDER BY au_lname ASC, au_fname ASC 2. Retornar el total year-to-date sales y el monto que le corresponde a author y publisher. SELECT ytd_sales AS Sales, authors.au_fname + ' '+ authors.au_lname AS Author, ToAuthor = (ytd_sales*royalty)/100, ToPublisher = ytd_sales-(ytd_sales*royalty)/100 FROM titles INNER JOIN titleauthor ON titles.title_id = titleauthor.title_id INNER JOIN authors ON titleauthor.au_id = authors.au_id ORDER BY Sales DESC, Author ASC 3. Este ejercicio crea la tabla temporal #coffeetabletitles en la tempdb. (#) DROP TABLE #coffeetabletitles SELECT * INTO #coffeetabletitles FROM titles WHERE price < $20 SELECT name FROM tempdb..sysobjects WHERE name LIKE '#c%' 4. Uso de FUNCIONES AGREGADAS y condiciones sobre agrupaciones SELECT pub_id, SUM(advance), AVG(price) FROM titles WHERE price >= $5 GROUP BY pub_id HAVING SUM(advance) > $15000 AND AVG(price) < $20AND pub_id > '0800' ORDER BY pub_id 5. Uso de Indices para optimizar las busquedas SELECT au_lname, au_fname, phone FROM authors WITH (INDEX(aunmind)) WHERE au_lname = 'Smith' Lic. Hugo Guerrero Page 1 de 3 DATA WAREHOUSE DW_Lab03.doc PRÁCTICA 2: OPTIMIZACIONES 1. Funciones MIN/MAX select MinPrice=min(price) ,Maxprice=max(price) from titles --Vrs select MinPrice=(select min(price) from titles) ,Maxprice=(select max(price) from titles) 2. COUNT Eficientes count(*) --Vrs count(column_name) 3. EXISTS Vrs. COUNT if(select count(*) from titles where type='business')>0 print 'Tu tienes libros de negocio' --Vrs if EXISTS(select * from titles where type='business') print 'Tu tienes libros de negocio' 4. Comparaciones Eficientes select * from titles where price>=11 --Vrs select * from titles where price>10.99 5. Eficiencia a travez de muchas consultas relacionadas select d.stor_id, d.ord_num, d.qty, t.title from titles t, sales d where t.title_id=d.title_id and d.title_id='BU1032' --Vrs declare @title varchar(20) select @title=title from titles where title_id='BU1032' select stor_id, ord_num, qty,@title from sales where title_id='BU1032' Lic. Hugo Guerrero Page 2 de 3 DATA WAREHOUSE DW_Lab03.doc PRÁCTICA 3: FUNCIONES 1. Funciones de Fecha getdate, datename, datepart, datediff, dateadd 2. Funciones de Cadena char_length, len, left, right, ltrim, rtrim, substring, upper, lower 3. ISNULL select price ,price=isnull(price,0.0) from titles 4. Funciones Matematicas abs, ceiling, floor, sign, rand 5. Funciones Caracteristicas (efecto Matriz) Numeric: X=Y 1-abs(sign(X-Y)) X>Y 1-sign(1-sign(X-Y)) String: X=Y charindex(X,Y)*charindex(Y,X) Date: X=Y 1-abs(sign(datediff(dd,Y,X))) X>Y 1-sign(1-sign(datediff(dd,Y,X))) Lic. Hugo Guerrero Page 3 de 3