Correlated subquery using TOP 1 : Correlated subquery « Subquery « SQL Server / T-SQL Tutorial

SQL Server / T-SQL Tutorial
1. Query
2. Insert Delete Update
3. Table
4. Table Join
5. Data Types
6. Set Operations
7. Constraints
8. Subquery
9. Aggregate Functions
10. Date Functions
11. Math Functions
12. String Functions
13. Data Convert Functions
14. Analytical Functions
15. Sequence Indentity
16. View
17. Index
18. Cursor
19. Database
20. Transact SQL
21. Procedure Function
22. Trigger
23. Transaction
24. XML
25. System Functions
26. System Settings
27. System Tables Views
28. User Role
29. CLR
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL Tutorial » Subquery » Correlated subquery 
8. 2. 4. Correlated subquery using TOP 1
6>
7CREATE TABLE Product(
8>     ProductID               int                NOT NULL,
9>     Name                    nvarchar(25)       NOT NULL,
10>     ProductNumber           nvarchar(25)               ,
11>     Color                   nvarchar(15)       NULL,
12>     StandardCost            money              NOT NULL,
13>     Size                    nvarchar(5)        NULL,
14>     Weight                  decimal(82)      NULL,
15>     ProductLine             nchar(20)           NULL,
16>     SellStartDate           datetime           NOT NULL,
17>     SellEndDate             datetime           NULL
18)
19> GO
1insert into Product values(1,'Product A', '1','Red',123.123,'1',1,'ProductLine A','1999-03-22','2000-03-22');
2> GO

(rows affected)
1insert into Product values(2,'Product B', '2','Yellow',234.234,'1',3,'ProductLine B','2000-03-22','2001-03-22');
2> GO

(rows affected)
1insert into Product values(3,'Product C', '3','Pink',345.345,'1',3,'ProductLine V','2001-09-22','2006-02-22');
2> GO

(rows affected)
1insert into Product values(4,'Product D', '4','White',456.456,'1',4,'ProductLine D','2002-08-22','2006-03-22');
2> GO

(rows affected)
1insert into Product values(5,'Product E', '5','Black',567.567,'1',5,'ProductLine E','2003-01-22','2003-04-22');
2> GO

(rows affected)
1insert into Product values(6,'Product F', '6','Blue',678.678,'1',6,'ProductLine W','2004-02-22','2005-05-22');
2> GO

(rows affected)
1insert into Product values(7,'Product G', '7','Drak',789.789,'1',7,'ProductLine Q','2005-03-22','2006-03-22');
2> GO

(rows affected)
1insert into Product values(8,'Product H', '8','Gray',234.123,'1',8,'ProductLine F','2006-04-22','2006-09-22');
2> GO

(rows affected)
1insert into Product values(9,'Product I', '9','Red',543.123,'1',9,'ProductLine R','2007-05-22','2008-03-22');
2> GO

(rows affected)
1insert into Product values(0,'Product J', '0','Gold',765.123,'1',0,'ProductLine J','2008-06-22','2009-03-22');
2> GO

(rows affected)
1>
2>
3>
4CREATE TABLE SalesOrderDetail(
5>     SalesOrderID            int                NOT NULL,
6>     SalesOrderDetailID      int                NOT NULL,
7>     CarrierTrackingNumber   nvarchar(25)       NULL,
8>     OrderQty                smallint           NOT NULL,
9>     ProductID               int                NOT NULL,
10>     SpecialOfferID          int                NOT NULL,
11>     UnitPrice               money              NOT NULL,
12>     UnitPriceDiscount       money              NOT NULL DEFAULT (0.0),
13>     LineTotal               AS ISNULL([UnitPrice(1.0 [UnitPriceDiscount]) [OrderQty]0.0)
14);
15> GO
1>
2insert into SalesOrderDetail values (1,1,'1',1,1,1,$1,$1);
3> GO

(rows affected)
1insert into SalesOrderDetail values (2,2,'2',2,2,2,$2,$2);
2> GO

(rows affected)
1insert into SalesOrderDetail values (3,3,'3',3,3,3,$3,$3);
2> GO

(rows affected)
1insert into SalesOrderDetail values (4,4,'4',4,4,4,$4,$4);
2> GO

(rows affected)
1insert into SalesOrderDetail values (5,5,'5',5,5,5,$5,$5);
2> GO

(rows affected)
1insert into SalesOrderDetail values (6,6,'6',6,6,6,$6,$6);
2> GO

(rows affected)
1insert into SalesOrderDetail values (7,7,'7',7,7,7,$7,$7);
2> GO

(rows affected)
1insert into SalesOrderDetail values (8,8,'8',8,8,8,$8,$8);
2> GO

(rows affected)
1insert into SalesOrderDetail values (9,9,'9',9,9,9,$9,$9);
2> GO

(rows affected)
1>
2SELECT ProductID
3>   , Name
4FROM Product AS P
5WHERE =
6>   (SELECT TOP ProductID
7>   FROM SalesOrderDetail As SD
8>   WHERE P.ProductID = SD.ProductID)
9> GO
ProductID   Name
----------- -------------------------
          Product A

(rows affected)
1>
2> drop table Product;
3> GO
1>
2>
3> drop table SalesOrderDetail;
4> GO
8. 2. Correlated subquery
8. 2. 1. A correlated subquery relies on the main query for its processing.
8. 2. 2. using SELECT DISTINCT or a GROUP BY statement in the inner query
8. 2. 3. Correlated subquery using Group By
8. 2. 4. Correlated subquery using TOP 1
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.