Radosław Wolicki 26 11 2012

Radosław Wolicki

Kierunek: ETI

Prowadzący: dr inż. A. Sikorski

Bazy Danych/ LAB 9

Wydział: Fizyka Techniczna

Data: 26.11.2012




Zaawansowane techniki wykorzystywania zapytań zagnieżdżonych

Baza Northwind


1) Wyznaczyć najlepsze miesiące po względem obrotów dla poszczególnych lat, które obejmuje baza danych Northwind


select SUM(Quantity*UnitPrice) as 'Cena',Year(o.OrderDate)as 'Rok',Month(o.OrderDate) as 'Miesi¹c' from [Order Details] od , Orders o

where

od.OrderID=o.OrderID

group by

Year(o.OrderDate),Month(o.OrderDate)



2) Dla każdego miesiąca (w poszczególnych latach) wyznaczyć pracownika, który wygenerował największe dochody



select YEAR(o1.OrderDate) as 'Rok',MONTH(o1.orderDate) as 'Miesi¹c',

(

select EmployeeID from [Order Details] od,Orders o

where

od.OrderID=o.OrderID and

YEAR(OrderDate)=YEAR(o1.OrderDate) and MONTH(OrderDate)=MONTH(o1.OrderDate)

group by

EmployeeID,EmployeeID ,YEAR(OrderDate) ,MONTH(OrderDate)

having SUM(Quantity*unitPrice)=

(

select max(total) from

(

select SUM(Quantity*unitPrice)total ,EmployeeID ,YEAR(OrderDate) y,MONTH(OrderDate) m from [Order Details] od,Orders o

where

od.OrderID=o.OrderID and

YEAR(OrderDate)=YEAR(o1.OrderDate) and MONTH(OrderDate)=MONTH(o1.OrderDate)

group by

EmployeeID,EmployeeID ,YEAR(OrderDate) ,MONTH(OrderDate)

) t

)




) [The Best Sales Representative of the Month]



from Orders o1 group by YEAR(o1.OrderDate),MONTH(o1.orderDate)




3) Zmodyfikować zapytanie z p.2 tak by znajdowało najlepszą: kategorię, produkt,

klienta, kraj (pole country w tabelce Customers)


select SUM(Quantity*unitPrice)total ,EmployeeID ,YEAR(OrderDate) y,MONTH(OrderDate) m from [Order Details] od,Orders o

where

od.OrderID=o.OrderID


group by

EmployeeID,YEAR(OrderDate),MONTH(OrderDate)

order by 3,4

select SUM(Quantity*unitPrice)total ,EmployeeID ,YEAR(OrderDate) y,MONTH(OrderDate) m from [Order Details] od,Orders o

where

od.OrderID=o.OrderID and

YEAR(OrderDate)=1996 and MONTH(OrderDate)=7

group by

EmployeeID,EmployeeID ,YEAR(OrderDate) ,MONTH(OrderDate)


select max(total) from

(

select SUM(Quantity*unitPrice)total ,EmployeeID ,YEAR(OrderDate) y,MONTH(OrderDate) m from [Order Details] od,Orders o

where

od.OrderID=o.OrderID and

YEAR(OrderDate)=1996 and MONTH(OrderDate)=7

group by

EmployeeID,EmployeeID ,YEAR(OrderDate) ,MONTH(OrderDate)

) t



select SUM(Quantity*unitPrice)total ,EmployeeID ,YEAR(OrderDate) y,MONTH(OrderDate) m from [Order Details] od,Orders o

where

od.OrderID=o.OrderID and

YEAR(OrderDate)=1996 and MONTH(OrderDate)=7

group by

EmployeeID,EmployeeID ,YEAR(OrderDate) ,MONTH(OrderDate)

having SUM(Quantity*unitPrice)=

(

select max(total) from

(

select SUM(Quantity*unitPrice)total ,EmployeeID ,YEAR(OrderDate) y,MONTH(OrderDate) m from [Order Details] od,Orders o

where

od.OrderID=o.OrderID and

YEAR(OrderDate)=1996 and MONTH(OrderDate)=7

group by

EmployeeID,EmployeeID ,YEAR(OrderDate) ,MONTH(OrderDate)

) t

)


select YEAR(o1.OrderDate),MONTH(o1.orderDate) from Orders o1 group by YEAR(o1.OrderDate),MONTH(o1.orderDate)




select YEAR(o1.OrderDate),MONTH(o1.orderDate) ,

(

select EmployeeID from [Order Details] od,Orders o

where

od.OrderID=o.OrderID and

YEAR(OrderDate)=YEAR(o1.OrderDate) and MONTH(OrderDate)=MONTH(o1.OrderDate)

group by

EmployeeID,EmployeeID ,YEAR(OrderDate) ,MONTH(OrderDate)

having SUM(Quantity*unitPrice)=

(

select max(total) from

(

select SUM(Quantity*unitPrice)total ,EmployeeID ,YEAR(OrderDate) y,MONTH(OrderDate) m from [Order Details] od,Orders o

where

od.OrderID=o.OrderID and

YEAR(OrderDate)=YEAR(o1.OrderDate) and MONTH(OrderDate)=MONTH(o1.OrderDate)

group by

EmployeeID,EmployeeID ,YEAR(OrderDate) ,MONTH(OrderDate)

) t

)



) [The Best Sales Representative of the Month]


from Orders o1 group by YEAR(o1.OrderDate),MONTH(o1.orderDate)



4) Dla poszczególnych miesięcy wyznaczyć kraj o największej średniej wartości

zamówienia



?????




5) Posługując się VS i językiem c# uzyskać wyniki z pp 1-4 wykonując w pętli skalarne

zapytanie dla poszczególnych 23 miesięcy. Przykładowo dla kategorii o największych

obrotach posłużyć się następującym wzorcem zapytania:



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Data.SqlClient;


namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=SSPI");

SqlConnection conn1 = new SqlConnection(conn.ConnectionString);

conn.Open(); conn1.Open();


SqlCommand cmd1 = new SqlCommand() { Connection = conn };

cmd1.CommandText = "select distinct YEAR(OrderDate),MONTH(OrderDate) from Orders";

SqlDataReader rdr = cmd1.ExecuteReader();

while (rdr.Read())

{


SqlCommand cmd2 = new SqlCommand() { Connection = conn1 };


cmd2.CommandText = String.Format(

"select CategoryID from Orders join [Order Details] on Orders.OrderID=[Order Details].OrderID " +

"join Products on Products.ProductID=[Order Details].ProductID " +

"where YEAR(OrderDate)={0} and MONTH(OrderDate)={1} group by CategoryID " +

"having Sum(Quantity*[Order Details].UnitPrice)= " +

"(select max(totals) from " +

"(select Sum(Quantity*[Order Details].UnitPrice) totals from Orders join [Order Details] on Orders.OrderID=[Order Details].OrderID " +

"join Products on Products.ProductID=[Order Details].ProductID " +

"where YEAR(OrderDate)={0} and MONTH(OrderDate)={1} group by CategoryID) t )", rdr.GetInt32(0), rdr.GetInt32(1));




Console.Write(rdr.GetInt32(0));

Console.Write("\t");

Console.Write(rdr.GetInt32(1));

Console.Write("\t");

int categoryId = (int)cmd2.ExecuteScalar();

Console.Write(categoryId);

Console.Write("\t");

cmd2.CommandText = "select CategoryName from Categories where CategoryID =" + categoryId.ToString();

cmd2.CommandText = string.Format("select CategoryName from Categories where CategoryID ={0}", categoryId);


Console.Write(cmd2.ExecuteScalar());

Console.Write("\n");

}

rdr.Close();



conn.Close();

//,rdr.GetInt32(0),rdr.GetInt32(1));

}

}

}



cmd1.CommandText = String.Format(

"select CategoryID from Orders join [Order Details] on Orders.OrderID=[Order Details].OrderID " +

"join Products on Products.ProductID=[Order Details].ProductID " +

"where YEAR(OrderDate)={0} and MONTH(OrderDate)={1} group by CategoryID " +

"having Sum(Quantity*[Order Details].UnitPrice)= " +

"(select max(totals) from " +

"(select Sum(Quantity*[Order Details].UnitPrice) totals from Orders join [Order Details] on

Orders.OrderID=[Order Details].OrderID " +

"join Products on Products.ProductID=[Order Details].ProductID " +

"where YEAR(OrderDate)={0} and MONTH(OrderDate)={1} group by CategoryID) t )"

,rdr.GetInt32(0),rdr.GetInt32(1));













6) Dla każdego miesiąca wyznaczyć kategorie, których produkty nie były zamówione


w SQL:


select YEAR(OrderDate) , MONTH(OrderDate),ProductName from Orders o,Products

where

not exists(

select * from [Order Details] join Orders on Orders.OrderID=[Order Details].OrderID

where YEAR(OrderDate)=YEAR(o.OrderDate) and MONTH(OrderDate)=MONTH(o.OrderDate)

and Products.ProductID=[Order Details].ProductID

)

group by YEAR(OrderDate) , MONTH(OrderDate),ProductName














Wyszukiwarka

Podobne podstrony:
Radosław Wolicki 12 11 2012
Radosław Wolicki 19 11 2012
Dzien pluszowego misia w przedszkolu 26.11.2012, BACHAMAS, Kronika 2012 2013
26 11 2012
wykład (26 11 2012)
Radoslaw Wolicki 17 12 2012
MATEMTYKA FINANSOWA ĆWICZENIA 1 (26 02 2012) i ĆWICZENIA 2 (11 03 2012)
2 Zal.11, projektowanie3 26 03 2012
wykład 11 (26 01 2012)
MATEMTYKA FINANSOWA ĆWICZENIA 1 (26 02 2012) i ĆWICZENIA 2 (11 03 2012)
MAKROEKONOMIA ĆWICZENIA 1 (26 02 2012) i ĆWICZENIA 2 (11 03 2012)
26 (11)
26 9 11
Hydrologia 2 06 11 2012
22 11 2012 traduction
Ekologia 14.11.2012, Ekologia i Ochrona środowiska
05.11.2012, Pedagogika specjalna - wykłady
Rodzina w religiach świata  11 2012

więcej podobnych podstron