Use view to join four tables : Filter view « View « SQL Server / T-SQL Tutorial






5> CREATE TABLE Customers (
6>      CustomerID nchar (5) NOT NULL ,
7>      CompanyName nvarchar (40) NOT NULL ,
8>      ContactName nvarchar (30) NULL ,
9>      ContactTitle nvarchar (30) NULL ,
10>     Address nvarchar (60) NULL ,
11>     City nvarchar (15) NULL ,
12>     Region nvarchar (15) NULL ,
13>     PostalCode nvarchar (10) NULL ,
14>     Country nvarchar (15) NULL ,
15>     Phone nvarchar (24) NULL ,
16>     Fax nvarchar (24) NULL
17> )
18> GO
1>
2> INSERT Customers VALUES('1','A','Maria',    'Sales',  'Str. 57', 'Berlin'    ,NULL,'12209', 'Germany','111-1111111','111-1111111')
3> INSERT Customers VALUES('2','M','Joe',      'Owner',  'Ave. 231','Vancouver' ,NULL,'05023', 'Mexico', '(222) 222-3332',NULL)
4> INSERT Customers VALUES('3','H','Thomas',   'Sales',  'Sq.  111','London'    ,NULL,'1D00P', 'UK',     '(444) 444-4444','(444) 444-4444')
5> INSERT Customers VALUES('4','B','Berg',     'Order',  'Blv    8','Toronto'   ,NULL,'00222', 'Sweden', '4444-55 55 65','5555-55 55 55')
6> INSERT Customers VALUES('5','S','Moos',     'Sales',  'Fort  57','New York'  ,NULL,'68306', 'Germany','6666-66666','6666-77777')
7> INSERT Customers VALUES('6','F','Cite',     'Manager','24      ','Dalles'    ,NULL,'67000', 'France', '88.60.15.31','88.60.15.32')
8> INSERT Customers VALUES('7','C','Sommer',   'Owner',  'Araq, 67','Paris'     ,NULL,'28023', 'Spain',  '(91) 555 22 82','(91) 555 91 99')
9> INSERT Customers VALUES('8','P','Leb',      'Owner',  '12      ','Beijing'   ,NULL,'13008', 'France', '91.24.45.40','91.24.45.41')
10> INSERT Customers VALUES('9','D','Elizabeth','Manager','23 Blvd.','Tsawassen','BC', 'T2F8M4','Canada', '(604) 555-4729','(604) 555-3745')
11> go

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1>
2>
3> CREATE TABLE Orders (
4>      OrderID int IDENTITY (1, 1) NOT NULL ,
5>      CustomerID nchar (5) NULL ,
6>      EmployeeID int NULL ,
7>      OrderDate datetime NULL ,
8>      RequiredDate datetime NULL ,
9>      ShippedDate datetime NULL ,
10>     ShipVia int NULL ,
11>     Freight money NULL DEFAULT (0),
12>     ShipName nvarchar (40) NULL ,
13>     ShipAddress nvarchar (60) NULL ,
14>     ShipCity nvarchar (15) NULL ,
15>     ShipRegion nvarchar (15) NULL ,
16>     ShipPostalCode nvarchar (10) NULL ,
17>     ShipCountry nvarchar (15) NULL
18> )
19> GO
1>
2> CREATE TABLE Products (
3>      ProductID int NOT NULL ,
4>      ProductName nvarchar (40) NOT NULL ,
5>      SupplierID int NULL ,
6>      CategoryID int NULL ,
7>      QuantityPerUnit nvarchar (20) NULL ,
8>      UnitPrice money NULL,
9>      UnitsInStock smallint NULL,
10>     UnitsOnOrder smallint NULL,
11>     ReorderLevel smallint NULL,
12>     Discontinued bit NOT NULL
13> )
14> GO
1> INSERT Products VALUES(1,'F',15,4,'10 - 999 g pkgs.',61.5,66,6,6,6)
2> INSERT Products VALUES(2,'M',14,4,'24 - 888 g pkgs.',34.8,74,7,7,7)
3> INSERT Products VALUES(3,'R',17,8,'24 - 777 g jars',17,171,0,5,0)
4> INSERT Products VALUES(4,'L',4,7,'5 kg pkg.',10,4,20,5,0)
5> INSERT Products VALUES(5,'R',12,1,'24 - 0.5 l bottles',1.23,445,0,25,0)
6> INSERT Products VALUES(6,'L',23,1,'500 ml',18,57,1,20,0)
7> INSERT Products VALUES(7,'O',12,2,'12 boxes',13,23,0,15,0)
8> go

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1>
2>
3> CREATE TABLE OrderDetails (
4>      OrderID int NOT NULL ,
5>      ProductID int NOT NULL ,
6>      UnitPrice money NOT NULL DEFAULT (0),
7>      Quantity smallint NOT NULL DEFAULT (1),
8>      Discount real NOT NULL DEFAULT (0)
9> )
10> GO
1> INSERT OrderDetails VALUES(10248,11,14,12,0)
2> INSERT OrderDetails VALUES(10248,42,9.8,10,0)
3> INSERT OrderDetails VALUES(10248,72,34.8,5,0)
4> INSERT OrderDetails VALUES(10249,14,18.6,9,0)
5> INSERT OrderDetails VALUES(10249,51,42.4,40,0)
6> INSERT OrderDetails VALUES(10250,41,7.7,10,0)
7> INSERT OrderDetails VALUES(10250,51,42.4,35,0.15)
8> INSERT OrderDetails VALUES(10250,65,16.8,15,0.15)
9> INSERT OrderDetails VALUES(10251,22,16.8,6,0.05)
10> INSERT OrderDetails VALUES(10251,57,15.6,15,0.05)
11> go

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1>
2>
3>    CREATE VIEW YesterdaysOrders_vw
4>    AS
5>    SELECT   cu.CompanyName,
6>             o.OrderID,
7>             o.OrderDate,
8>             od.ProductID,
9>             p.ProductName,
10>             od.Quantity,
11>             od.UnitPrice,
12>             od.Quantity * od.UnitPrice AS ExtendedPrice
13>    FROM     Customers AS cu
14>    INNER JOIN   Orders AS o
15>          ON cu.CustomerID = o.CustomerID
16>    INNER JOIN   OrderDetails AS od
17>          ON o.OrderID = od.OrderID
18>    INNER JOIN   Products AS p
19>          ON od.ProductID = p.ProductID
20>    WHERE CONVERT(varchar(12),o.OrderDate,101) =
21>          CONVERT(varchar(12),DATEADD(day, -1,GETDATE()),101)
22> GO
1>
2> drop VIEW YesterdaysOrders_vw;
3> GO
1> drop table Customers;
2> drop table orders;
3> drop table orderdetails;
4> drop table Products;
5> GO








16.6.Filter view
16.6.1.A script that creates a stored procedure as a filter-view
16.6.2.A SELECT statement that uses the BankersMin view with where clause
16.6.3.Use view to join four tables
16.6.4.Create the view that combines all sales tables with union
16.6.5.Create a view with outer join and full join