Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Stairway to T-SQL Part 2: Beyond T-SQL Basics: Level 2: Writing Subqueries Expand / Collapse
Author
Message
Posted Thursday, March 6, 2014 7:54 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 12, 2015 8:17 AM
Points: 1, Visits: 5
The example for Listing 6 is wrong. It has the same query as listing 5. Please correct it.
Post #1548297
Posted Tuesday, December 23, 2014 8:27 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, March 5, 2015 8:28 AM
Points: 4, Visits: 27
lara.krefski (3/6/2014)
The example for Listing 6 is wrong. It has the same query as listing 5. Please correct it.


I think the author meant something like:

Select
OrderDate,
DateDiff(day,OrderDate,(Select MAX(OrderDate) from Sales.SalesOrderHeader)) 'Days to Max OrderDate',
(Select MAX(OrderDate) from Sales.SalesOrderHeader) 'Max Order Date'
from Sales.SalesOrderHeader

Post #1646194
Posted Wednesday, January 7, 2015 9:48 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, January 7, 2015 9:49 AM
Points: 2, Visits: 16
you might be using new version of adventure works database. change the year in order date and you will get results.
Post #1649176
Posted Tuesday, March 10, 2015 9:08 PM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: 2 days ago @ 5:28 PM
Points: 28, Visits: 106
Agree with the others; listings 5 and 6 have been switched. Which led to a lot of confusion, as I'm looking at this:

To demonstrate using a subquery in a function call, suppose that you have the requirement to display the number of days between the OrderDate and the maximum OrderDate for each Sales.SalesOrderHeader records. The code in Listing 6 meets this requirement.

SELECT count(*), OrderDate 
FROM [Sales].[SalesOrderHeader]
GROUP BY OrderDate
HAVING count(*) >
(SELECT count(*)
FROM [Sales].[SalesOrderHeader]
WHERE OrderDate = '2006-05-01 00:00:00.000');



...and thinking... "Wait. That makes no sense at all. Where does it grab any number of days? It looks like this... grabs all the order dates and the number of orders on those dates, where the number of orders is greater than the number of orders on 5/1/2006. Which isn't even remotely what it says it's supposed to do..."

If I'd been a total newbie, this would have been misleading. As it was, it was just confusing.
Post #1667399
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse