Base de Datos
Base de Datos
--- Supported versions of SQL Server: 2005, 2008, 2008 R2, 2012, SQL Azure
--- Based originally on the Northwind sample database
-- with changes in both schema and data
--- Last updated: 20120328
--- Itzik Ben-Gan
---------------------------------------------------------------------
-- For on-premises SQL Server use the steps in section A and then proceed to section C
-- For SQL Azure use the steps in section B and then proceed to section C
-- Drop database
IF DB_ID('TSQL2012') IS NOT NULL DROP DATABASE TSQL2012;
-- Create database
CREATE DATABASE TSQL2012;
GO
USE TSQL2012;
GO
-- 3. Proceed to section C
/*
-- 1. Connect to SQL Azure, master database
USE master; -- used only as a test; will fail if not connected to master
GO
-- 5. Proceed to section C
*/
---------------------------------------------------------------------- Section C - for both on-premises SQL Server and SQL Azure
---------------------------------------------------------------------
INT
lastname
firstname
title
DATETIME
NOT NULL,
hiredate
DATETIME
NOT NULL,
address
city
region
postalcode
NVARCHAR(15) NULL,
NVARCHAR(10) NULL,
country
phone
mgrid
INT
NULL,
phone
fax
NVARCHAR(24) NULL,
NOT NULL,
categoryid INT
NOT NULL,
unitprice MONEY
NOT NULL
NOT NULL
INT
phone
fax
NVARCHAR(24) NULL,
ON Sales.Customers(city);
ON Sales.Customers(region);
shipperid INT
INT
INT
INT
DATETIME
NOT NULL,
requireddate DATETIME
NOT NULL,
shippeddate DATETIME
NULL,
shipperid
freight
INT
MONEY
NOT NULL,
NOT NULL
ON Sales.Orders(custid);
ON Sales.Orders(empid);
ON Sales.Orders(shipperid);
ON Sales.Orders(orderdate);
NOT NULL,
NOT NULL,
NOT NULL
SMALLINT
NOT NULL
TINYINT
NOT NULL
---------------------------------------------------------------------
VALUES(5, N'Supplier EQPNC', N'Holm, Michael', N'Export Administrator', N'Calle del Rosal 4567',
N'Oviedo', N'Asturias', N'10029', N'Spain', N'(98) 123 45 67', NULL);
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(6, N'Supplier QWUSF', N'Popkova, Darya', N'Marketing Representative', N'8901 Setsuko
Chuo-ku', N'Osaka', NULL, N'10028', N'Japan', N'(06) 789-0123', NULL);
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(7, N'Supplier GQRCV', N'Rbild, Jesper', N'Marketing Manager', N'5678 Rose St. Moonie
Ponds', N'Melbourne', N'Victoria', N'10018', N'Australia', N'(03) 123-4567', N'(03) 456-7890');
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(8, N'Supplier BWGYE', N'Iallo, Lucio', N'Sales Representative', N'9012 King''s Way',
N'Manchester', NULL, N'10021', N'UK', N'(161) 567-8901', NULL);
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(9, N'Supplier QQYEU', N'Basalik, Evan', N'Sales Agent', N'Kaloadagatan 4567',
N'Gteborg', NULL, N'10022', N'Sweden', N'031-345 67 89', N'031-678 90 12');
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(10, N'Supplier UNAHG', N'Barnett, Dave', N'Marketing Manager', N'Av. das Americanas
2345', N'Sao Paulo', NULL, N'10034', N'Brazil', N'(11) 345 6789', NULL);
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(11, N'Supplier ZPYVS', N'Jain, Mukesh', N'Sales Manager', N'Tiergartenstrae 3456',
N'Berlin', NULL, N'10016', N'Germany', N'(010) 3456789', NULL);
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(12, N'Supplier SVIYA', N'Regev, Barak', N'International Marketing Mgr.', N'Bogenallee
9012', N'Frankfurt', NULL, N'10024', N'Germany', N'(069) 234567', NULL);
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(13, N'Supplier TEGSC', N'Brehm, Peter', N'Coordinator Foreign Markets', N'Frahmredder
3456', N'Cuxhaven', NULL, N'10019', N'Germany', N'(04721) 1234', N'(04721) 2345');
VALUES(22, N'Supplier FNUXM', N'Skelly, Bonnie L.', N'Accounting Manager', N'Verkoop Rijnweg
8901', N'Zaandam', NULL, N'10014', N'Netherlands', N'(12345) 8901', N'(12345) 5678');
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(23, N'Supplier ELCRN', N'LaMee, Brian', N'Product Manager', N'Valtakatu 1234',
N'Lappeenranta', NULL, N'10032', N'Finland', N'(953) 78901', NULL);
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(24, N'Supplier JNNES', N'Clark, Molly', N'Sales Representative', N'6789 Prince Edward
Parade Hunter''s Hill', N'Sydney', N'NSW', N'10030', N'Australia', N'(02) 234-5678', N'(02) 5678901');
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(25, N'Supplier ERVYZ', N'Sprenger, Christof', N'Marketing Manager', N'7890 Rue St.
Laurent', N'Montral', N'Qubec', N'10017', N'Canada', N'(514) 456-7890', NULL);
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(26, N'Supplier ZWZDM', N'Cunha, Gonalo', N'Order Administrator', N'Via dei Gelsomini,
5678', N'Salerno', NULL, N'10020', N'Italy', N'(089) 4567890', N'(089) 4567890');
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(27, N'Supplier ZRYDZ', N'Leoni, Alessandro', N'Sales Manager', N'4567, rue H. Voiron',
N'Montceau', NULL, N'10036', N'France', N'89.01.23.45', NULL);
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(28, N'Supplier OAVQT', N'Teper, Jeff', N'Sales Representative', N'Bat. B 2345, rue des
Alpes', N'Annecy', NULL, N'10010', N'France', N'01.23.45.67', N'89.01.23.45');
INSERT INTO Production.Suppliers(supplierid, companyname, contactname, contacttitle, address,
city, region, postalcode, country, phone, fax)
VALUES(29, N'Supplier OGLRK', N'Walters, Rob', N'Accounting Manager', N'0123 rue Chasseur',
N'Ste-Hyacinthe', N'Qubec', N'10009', N'Canada', N'(514) 567-890', N'(514) 678-9012');
SET IDENTITY_INSERT Production.Suppliers OFF;
VALUES(78, N'Customer NLTYP', N'Young, Robin', N'Marketing Assistant', N'0123 Grizzly Peak
Rd.', N'Butte', N'MT', N'10107', N'USA', N'(406) 555-0121', N'(406) 555-0131');
INSERT INTO Sales.Customers(custid, companyname, contactname, contacttitle, address, city,
region, postalcode, country, phone, fax)
VALUES(79, N'Customer FAPSM', N'Wickham, Jim', N'Marketing Manager', N'Luisenstr. 0123',
N'Mnster', NULL, N'10118', N'Germany', N'0251-456789', N'0251-012345');
INSERT INTO Sales.Customers(custid, companyname, contactname, contacttitle, address, city,
region, postalcode, country, phone, fax)
VALUES(80, N'Customer VONTK', N'Geschwandtner, Jens', N'Owner', N'Avda. Azteca 4567',
N'Mxico D.F.', NULL, N'10044', N'Mexico', N'(5) 678-9012', NULL);
INSERT INTO Sales.Customers(custid, companyname, contactname, contacttitle, address, city,
region, postalcode, country, phone, fax)
VALUES(81, N'Customer YQQWW', N'Nagel, Jean-Philippe', N'Sales Representative', N'Av. Ins de
Castro, 1234', N'Sao Paulo', N'SP', N'10120', N'Brazil', N'(11) 123-4567', N'(11) 234-5678');
INSERT INTO Sales.Customers(custid, companyname, contactname, contacttitle, address, city,
region, postalcode, country, phone, fax)
VALUES(82, N'Customer EYHKM', N'Veninga, Tjeerd', N'Sales Associate', N'1234 DaVinci Blvd.',
N'Kirkland', N'WA', N'10119', N'USA', N'(206) 555-0124', N'(206) 555-0133');
INSERT INTO Sales.Customers(custid, companyname, contactname, contacttitle, address, city,
region, postalcode, country, phone, fax)
VALUES(83, N'Customer ZRNDE', N'Fonteneau, Karl', N'Sales Manager', N'Smagsloget 3456',
N'rhus', NULL, N'10090', N'Denmark', N'23 45 67 89', N'01 23 45 67');
INSERT INTO Sales.Customers(custid, companyname, contactname, contacttitle, address, city,
region, postalcode, country, phone, fax)
VALUES(84, N'Customer NRCSK', N'Tuntisangaroon, Sittichai', N'Sales Agent', N'6789, rue du
Commerce', N'Lyon', NULL, N'10072', N'France', N'78.90.12.34', N'78.90.12.35');
INSERT INTO Sales.Customers(custid, companyname, contactname, contacttitle, address, city,
region, postalcode, country, phone, fax)
VALUES(85, N'Customer ENQZT', N'McLin, Nkenge', N'Accounting Manager', N'5678 rue de
l''Abbaye', N'Reims', NULL, N'10082', N'France', N'56.78.90.12', N'56.78.90.13');
INSERT INTO Sales.Customers(custid, companyname, contactname, contacttitle, address, city,
region, postalcode, country, phone, fax)
VALUES(86, N'Customer SNXOJ', N'Syamala, Manoj', N'Sales Representative', N'Adenauerallee
7890', N'Stuttgart', NULL, N'10086', N'Germany', N'0711-345678', N'0711-901234');
SELECT
YEAR(O.orderdate) AS orderyear,
SUM(OD.qty) AS qty
FROM Sales.Orders AS O
JOIN Sales.OrderDetails AS OD
ON OD.orderid = O.orderid
GROUP BY YEAR(orderdate);
GO
SELECT
O.custid,
DATEADD(month, DATEDIFF(month, 0, O.orderdate), 0) AS ordermonth,
SUM(OD.qty) AS qty
FROM Sales.Orders AS O
JOIN Sales.OrderDetails AS OD
ON OD.orderid = O.orderid
GROUP BY custid, DATEADD(month, DATEDIFF(month, 0, O.orderdate), 0);
GO
WITH SCHEMABINDING
AS
SELECT
O.empid,
DATEADD(month, DATEDIFF(month, 0, O.orderdate), 0) AS ordermonth,
SUM(OD.qty) AS qty,
CAST(SUM(OD.qty * OD.unitprice * (1 - discount))
AS NUMERIC(12, 2)) AS val,
COUNT(*) AS numorders
FROM Sales.Orders AS O
JOIN Sales.OrderDetails AS OD
ON OD.orderid = O.orderid
GROUP BY empid, DATEADD(month, DATEDIFF(month, 0, O.orderdate), 0);
GO
FROM Nums
ORDER BY rownum;
GO