SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Building Tips (Need Them!)


Query Building Tips (Need Them!)

Author
Message
robertafricker
robertafricker
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 36
Hey Gail,

Thanks for the "inside dope." I find what you said to be very interesting.
I have built queries using the ON portion of the syntax, although not often as of yet.
I will keep this info in my bag of tricks.
Also, I realized that since right now I am trying to figure out how to interpret a query piece by piece and then turn it into syntax, I need to submit a different type of post.
It's going to be called "Interpreting the Query" and ask some specific questions about "building" the syntax.
Your WITH CUBE and WITH ROLLUP link was especially helpful as it showed the result set tables and really helped me to "see" the thang.
Pls keep an eye out for the next post, "Interpreting the Query." Thanks!

-Roberta-
robertafricker
robertafricker
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 36
Hello Gail,

I have saved your info for a later date when I'm ready for it.
I am familiar with aliases and derived tables, however, not ready
for prime time yet.
Pls see my other response, gracias.

-RF-
robertafricker
robertafricker
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 36
Hi again,

Jeff's post about CTE's and derived tables got my looking at your post again even thought I'm sending a new post on building queries.
I'd like to tell you what I think you are saying so that I can understand the syntax more clearly. Here goes:

1. Major "Aha" for me on the definition of "alias." I have been very aware of that usage in syntax however did not know it was referred to as alias, e.g.
USE pubs
SELECT p.pub_id, p.pub_name
FROM publishers AS p

2. Derived tables are result sets used as table sources in a query.

And then there's the syntax, nicely built by the way, I understand most of it except, please remind me what this line means:

ON TheTable.Col1 = TheDerivedTable.Col1

Thanks, u rock...Cool

-RF-
GilaMonster
GilaMonster
SSC Guru
SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)SSC Guru (223K reputation)

Group: General Forum Members
Points: 223800 Visits: 46302
It specified on what columns the join is done.

Same as for a normal inner/outer join between two tables:
FROM Itable1 INNER JOIN Table2 On Table1.Col1 = Table2.Col2

Just in this case it's a join between a table and a subquery

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


robertafricker
robertafricker
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 36
HI,

Sorry for the delayed response, I got piled up. Thanks for the explanation on the "ON" portion of the syntax. I'll keep it in my
stockpile.
I should be posting another syntax question shortly, promise...BigGrin

-RF-
robertafricker
robertafricker
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 36
Hello GS,

I apologize for the late response, I have been piled up big time...Exclamation Mark
I will look over your info tomorrow and respond.
Thank you again for your continued input...

-Roberta-
jezemine
jezemine
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1901 Visits: 589
I think this is a nice little article illustrating a good, systematic method for attacking complicated queries:

http://weblogs.sqlteam.com/jeffs/archive/2007/04/30/60192.aspx

---------------------------------------
elsasoft.org
robertafricker
robertafricker
SSC-Enthusiastic
SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)SSC-Enthusiastic (159 reputation)

Group: General Forum Members
Points: 159 Visits: 36
HI jezemine,

Thank you for the article, it is much closer to the idea that I've been having about "breaking it down" piece by piece.
At this moment, I am going to submit another post titled, "Interpreting the Query."
If you have a moment, please take a look and respond.
In the meantime, I will look over your article more closely, and I'd like to ask you a couple of questions about it.
Talk with you soon....Wink

-Roberta-
jezemine
jezemine
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1901 Visits: 589
however much I'd like to take credit for that article, it's not mine. Smile

Jeff Smith wrote it - his blog is very good btw.

---------------------------------------
elsasoft.org
Jim Russell-390299
Jim Russell-390299
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2255 Visits: 1403
I have learned that if Gail says it, it must be true.
So when I trip over something she says, I worry that I am misunderstanding something. She said:
"It's not recommended to do joins in the where clause any more. Especially since, in 2005 and higher, the old style outer join (*=) does not work."

The original query used the form FROM tableA,tableB, which yields a cartesian product which gets pruned by the subsequent WHERE clause. (No debate on depreciating the old style outer join notation, and in the case under discussion, as you suggested, an INNER JOIN is clearer.)

But I think the phrase "joins in the where clause" is misleading, the join is being done in the FROM clause.

I find cartesian product joins extremely powerful and useful in many situations, and I don't understand why I should replace one keystroke (",") with 12 (" CROSS JOIN ".) Is the "," construct now being discouraged?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search