Rabu, 20 April 2016

Re: [MS_AccessPros] Example of subquery.

 

John-
Thanks a lot.
Best Regards,
Kevin

Life has an uncanny way of responding to your need.
在 "John Viescas JohnV@msn.com [MS_Access_Professionals]" <MS_Access_Professionals@yahoogroups.com>,2016年4月20日 下午7:13写道:

 

Kevin-


OK.  Here's a preview from the upcoming Effective SQL book that I and my team are writing.

Table Subquery:

SELECT BeefRecipes.RecipeTitle

FROM

  (SELECT Recipes.RecipeID, Recipes.RecipeTitle

          FROM (Recipes INNER JOIN Recipe_Ingredients

          ON Recipes.RecipeID = Recipe_Ingredients.RecipeID)

          INNER JOIN Ingredients

          ON Ingredients.IngredientID =

                 Recipe_Ingredients.IngredientID

          WHERE Ingredients.IngredientName = 'Beef')

                 AS BeefRecipes

  INNER JOIN

  (SELECT Recipe_Ingredients.RecipeID

          FROM Recipe_Ingredients INNER JOIN Ingredients

          ON Ingredients.IngredientID =

                 Recipe_Ingredients.IngredientID

          WHERE Ingredients.IngredientName = 'Garlic')

                 AS GarlicRecipes

ON BeefRecipes.RecipeID = GarlicRecipes.RecipeID; 

The above query uses two table subqueries in the FROM clause to find the recipes that have both Beef and Garlic.


Table Subquery with one column:

SELECT Products.ProductName

FROM Products

WHERE Products.ProductNumber NOT IN

  (SELECT Order_Details.ProductNumber

  FROM Orders INNER JOIN Order_Details

  ON Orders.OrderNumber = Order_Details.OrderNumber

  WHERE Orders.OrderDate

          BETWEEN '2015-12-01' AND '2015-12-31');

The above query finds the products that were not ordered in the last quarter of 2015.  Note that in Access, you would use # as a delimiter on the date constants instead of a single quote.


Scalar subquery - returns exactly one value:

SELECT Products.ProductNumber, Products.ProductName,

  (SELECT MAX(Orders.OrderDate)

  FROM Orders INNER JOIN Order_Details

  ON Orders.OrderNumber = Order_Details.OrderNumber

  WHERE Order_Details.ProductNumber = Products.ProductNumber)

          AS LastOrder

FROM Products;

The above query lists all products and the date of the latest order for that product.

In each case, the (SELECT … ) is a subquery.


John Viescas, Author
Microsoft Access 2010 Inside Out
Microsoft Access 2007 Inside Out
Microsoft Access 2003 Inside Out
Building Microsoft Access Applications 
SQL Queries for Mere Mortals 
(Paris, France)



On Apr 20, 2016, at 12:53 PM, zhaoliqingoffice zhaoliqingoffice@163.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:

John-
Thanks for your reply. I think an example might be the best way to see how a subquery works. Please help.
Best Regards,
Kevin

Life has an uncanny way of responding to your need.
在 "John Viescas JohnV@msn.com [MS_Access_Professionals]" <MS_Access_Professionals@yahoogroups.com>,2016年4月20日 下午6:16写道:



Kevin-


What are you trying to learn?  A subquery is simply a subset of data using a SELECT statement enclosed in parentheses.  You can have a table subquery (multiple columns and rows), a list subquery (one column returning a value from multiple rows), or a scalar subquery (one column returning exactly one value).  Using a subquery isn't dependent on the version of Access unless you go back earlier than Version 2.

John Viescas, Author
Microsoft Access 2010 Inside Out
Microsoft Access 2007 Inside Out
Microsoft Access 2003 Inside Out
Building Microsoft Access Applications 
SQL Queries for Mere Mortals 
(Paris, France)



On Apr 20, 2016, at 12:07 PM, 'zhaoliqingoffice@163.com' zhaoliqingoffice@163.com [MS_Access_Professionals] <MS_Access_Professionals@yahoogroups.com> wrote:




Dear All,
Can anybody upload an exmample of ms access file (2010) containing a subquery to the group? Thanks in advance.
Best Regards,
Kevin







__._,_.___

Posted by: zhaoliqingoffice <zhaoliqingoffice@163.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (5)

Upgrade your account with the latest Yahoo Mail app
Get organized with the fast and easy-to-use Yahoo Mail app. Upgrade today!


.

__,_._,___

Tidak ada komentar:

Posting Komentar