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 1234»»»

SubQuery Expand / Collapse
Author
Message
Posted Saturday, September 17, 2011 1:52 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:01 PM
Points: 21,642, Visits: 15,312
Comments posted to this topic are about the item SubQuery



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1176878
Posted Saturday, September 17, 2011 1:55 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Thursday, August 21, 2014 5:59 AM
Points: 897, Visits: 1,488
Got it wrong.

I thought that there would be an error because the StudentId column has the alias "StudentNumber" inside the subquery and in line 25 it's being declared as StudentId again.

Interesting behaviour. Learned something new. :)

Thank you for the question.

Best regards,



Best regards,

Andre Guerreiro Neto

Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2
Post #1176880
Posted Saturday, September 17, 2011 2:01 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Monday, August 25, 2014 6:32 PM
Points: 11,194, Visits: 11,140
Happy to report that I am first to get this right.
I don't enjoy this sort of question though - I am a human, not a T-SQL parser.
The question could have made the same point, with the same learning value, much more simply.




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1176882
Posted Saturday, September 17, 2011 4:21 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:01 PM
Points: 21,642, Visits: 15,312
SQL Kiwi (9/17/2011)
Happy to report that I am first to get this right.
I don't enjoy this sort of question though - I am a human, not a T-SQL parser.
The question could have made the same point, with the same learning value, much more simply.


It was due to that human nature (it is very easy for people to make a mistake in column aliasing as shown here) I wanted to demonstrate the question like this. As you said though, I could have given a much simpler example. This just happened to be one that I was working on as an answer to a forum question at the time of submission.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1176897
Posted Saturday, September 17, 2011 4:37 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Monday, August 25, 2014 6:32 PM
Points: 11,194, Visits: 11,140
SQLRNNR (9/17/2011)
...This just happened to be one that I was working on as an answer to a forum question at the time of submission.

I see. Thanks for the explanation.




Paul White
SQL Server MVP
SQLblog.com
@SQL_Kiwi
Post #1176899
Posted Sunday, September 18, 2011 4:29 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, August 28, 2013 7:06 AM
Points: 1,216, Visits: 308
Indeed, it's an interesting thing to learn. Thanks.
Post #1176964
Posted Sunday, September 18, 2011 11:19 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 8:01 PM
Points: 21,642, Visits: 15,312
Sudhir Dwivedi (9/18/2011)
Indeed, it's an interesting thing to learn. Thanks.


You're welcome.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1177005
Posted Monday, September 19, 2011 12:03 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
Nice question.
I chose the third (wrong) answer because I didn't notice the DATE data type and thought it was DATETIME
Post #1177079
Posted Monday, September 19, 2011 12:32 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Sunday, June 29, 2014 11:26 PM
Points: 1,481, Visits: 1,960
JOIN (SELECT StudentId as StudentNumber, max(RequestNbr) as RequestNb
FROM Request
...
GROUP BY StudentId) as Latest (StudentId, RequestNbr)
ON Request.StudentId = Latest.StudentId
AND Request.RequestNbr = Latest.RequestNbr

Just to clearify. The whole point was to point out that the bold parts dont match and that SQL instead is using the italic names in the join?

If above is true then read on... otherwise ignore:
I got the answer right for all the wrong reasons. I simply didnt see the change in the names. And it took one heck off a time to see the change in the names. When i first read the question i was so sure it would be something odd about the DATE datatype (since one answer had timeportion in it) or something with the MAX in the subquery. So i focused on that but couldnt find anything odd about it. So that left me with only one answer. Which was the right answer.

PS Im not being sarcastic or anything. I for one learned something... if my assumption is correct. Its just that in this case i feel like the point was drowned in code that wasnt relevant. Though atleast it made me think ALOT

/T
Post #1177086
Posted Monday, September 19, 2011 1:28 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, August 18, 2014 2:53 AM
Points: 1,657, Visits: 2,084
DOH! Right answer, for the wrong reason.
I completely missed the aliases and selected the correct answer on the DATE type.
So I learnt something and won a checky point.
Making this my favorite QotD so far this week
Post #1177096
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse