SQL Cookbook 1st Edition Anthony Molinaro download
SQL Cookbook 1st Edition Anthony Molinaro download
download
https://ebookultra.com/download/sql-cookbook-1st-edition-anthony-
molinaro/
https://ebookultra.com/download/microsoft-sql-server-2012-t-sql-1st-
edition-tom-coffing/
https://ebookultra.com/download/essential-sql-on-sql-server-2008-1st-
edition-dr-sikha-bagui/
https://ebookultra.com/download/sql-server-t-sql-recipes-4th-edition-
jason-brimhall/
https://ebookultra.com/download/inside-microsoft-sql-
server-2008-t-sql-querying-1st-edition-itzik-ben-gan/
Beginning SQL 1st Edition Paul Wilton
https://ebookultra.com/download/beginning-sql-1st-edition-paul-wilton/
https://ebookultra.com/download/sql-and-relational-theory-how-to-
write-accurate-sql-code-1st-edition-c-j-date/
https://ebookultra.com/download/joe-celko-s-sql-for-smarties-fourth-
edition-advanced-sql-programming-joe-celko/
https://ebookultra.com/download/sql-and-relational-theory-how-to-
write-accurate-sql-code-2ed-edition-date/
https://ebookultra.com/download/joe-celko-s-sql-for-smarties-advanced-
sql-programming-third-edition-joe-celko/
SQL Cookbook 1st Edition Anthony Molinaro Digital
Instant Download
Author(s): Anthony Molinaro
ISBN(s): 9780596009762, 0596009763
Edition: 1
File Details: PDF, 5.81 MB
Year: 2005
Language: english
SQL Cookbook ™
Other resources from O’Reilly
Related titles SQL Tuning MySQL Cookbook™
Mastering Oracle SQL Learning SQL
SQL Pocket Reference Database in Depth
SQL in a Nutshell Transact-SQL Cookbook™
Anthony Molinaro
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (safari.oreilly.com). For more information, contact our corporate/insti-
tutional sales department: (800) 998-9938 or corporate@oreilly.com.
Printing History:
December 2005: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. The Cookbook series designations, SQL Cookbook, the image of an Agamid lizard,
and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information
contained herein.
ISBN: 978-0-596-00976-2
[LSI] [2012-12-14]
To my mom:
You’re the best! Thank you for everything.
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
1. Retrieving Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Retrieving All Rows and Columns from a Table 1
1.2 Retrieving a Subset of Rows from a Table 2
1.3 Finding Rows That Satisfy Multiple Conditions 2
1.4 Retrieving a Subset of Columns from a Table 3
1.5 Providing Meaningful Names for Columns 3
1.6 Referencing an Aliased Column in the WHERE Clause 4
1.7 Concatenating Column Values 5
1.8 Using Conditional Logic in a SELECT Statement 7
1.9 Limiting the Number of Rows Returned 8
1.10 Returning n Random Records from a Table 9
1.11 Finding Null Values 11
1.12 Transforming Nulls into Real Values 11
1.13 Searching for Patterns 12
vii
3. Working with Multiple Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.1 Stacking One Rowset Atop Another 28
3.2 Combining Related Rows 30
3.3 Finding Rows in Common Between Two Tables 31
3.4 Retrieving Values from One Table That Do Not Exist in Another 33
3.5 Retrieving Rows from One Table That Do Not Correspond
to Rows in Another 38
3.6 Adding Joins to a Query Without Interfering with Other Joins 40
3.7 Determining Whether Two Tables Have the Same Data 42
3.8 Identifying and Avoiding Cartesian Products 49
3.9 Performing Joins When Using Aggregates 50
3.10 Performing Outer Joins When Using Aggregates 55
3.11 Returning Missing Data from Multiple Tables 58
3.12 Using NULLs in Operations and Comparisons 62
5. Metadata Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
5.1 Listing Tables in a Schema 84
5.2 Listing a Table’s Columns 85
5.3 Listing Indexed Columns for a Table 86
5.4 Listing Constraints on a Table 88
Table of Contents | ix
8. Date Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
8.1 Adding and Subtracting Days, Months, and Years 190
8.2 Determining the Number of Days Between Two Dates 193
8.3 Determining the Number of Business Days Between Two Dates 195
8.4 Determining the Number of Months or Years Between Two Dates 200
8.5 Determining the Number of Seconds, Minutes, or Hours
Between Two Dates 202
8.6 Counting the Occurrences of Weekdays in a Year 204
8.7 Determining the Date Difference Between the Current Record
and the Next Record 216
x | Table of Contents
11.6 Finding Records with the Highest and Lowest Values 340
11.7 Investigating Future Rows 342
11.8 Shifting Row Values 345
11.9 Ranking Results 348
11.10 Suppressing Duplicates 350
11.11 Finding Knight Values 352
11.12 Generating Simple Forecasts 359
Table of Contents | xi
14. Odds ‘n’ Ends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
14.1 Creating Cross-Tab Reports Using SQL Server’s PIVOT Operator 474
14.2 Unpivoting a Cross-Tab Report Using SQL Server’s UNPIVOT
Operator 476
14.3 Transposing a Result Set Using Oracle’s MODEL Clause 478
14.4 Extracting Elements of a String from Unfixed Locations 482
14.5 Finding the Number of Days in a Year (an Alternate Solution
for Oracle) 485
14.6 Searching for Mixed Alphanumeric Strings 486
14.7 Converting Whole Numbers to Binary Using Oracle 489
14.8 Pivoting a Ranked Result Set 492
14.9 Adding a Column Header into a Double Pivoted Result Set 496
14.10 Converting a Scalar Subquery to a Composite Subquery in Oracle 507
14.11 Parsing Serialized Data into Rows 509
14.12 Calculating Percent Relative to Total 513
14.13 Creating CSV Output from Oracle 515
14.14 Finding Text Not Matching a Pattern (Oracle) 520
14.15 Transforming Data with an Inline View 523
14.16 Testing for Existence of a Value Within a Group 524
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 591
SQL is the language in the database world. If you’re developing for or reporting
from relational databases, your ability to put data into a database and then get it
back out again ultimately comes down to your knowledge of SQL. Yet many practi-
tioners use SQL in a perfunctory manner, and are unaware of the power at their dis-
posal. This book aims to change all that, by opening your eyes to what SQL can
really do for you.
The book you’re holding in your hands is a cookbook. It’s a collection of common
SQL problems and their solutions that I hope you’ll find helpful in your day-to-day
work. Recipes are categorized into chapters of related topics. When faced with a new
SQL problem that you haven’t solved before, find the chapter that best seems to
apply, skim through the recipe titles, and hopefully you will find a solution, or at
least inspiration for a solution.
More than 150 recipes are available in this 600-plus page book, and I’ve only
scratched the surface of what can be done using SQL. The number of different SQL
solutions available for solving our daily programming problems is eclipsed only by
the number of problems we need to solve. You won’t find all possible problems cov-
ered in this book. Indeed, such coverage would be impossible. You will, however,
find many common problems and their solutions. And in those solutions lie tech-
niques that you’ll learn how to expand upon and apply to other, new problems that I
never thought to cover.
xiii
The yell which had come to the ears of Jake Parsons, was
sounded from the lips of Tom Rutter.
“Quick! Follow them! Don’t stand here idle. Your lives depend on
it.”
Such were the exclamations which Rutter gave vent to; and the
man by his side gradually dispensed with the sneer on his face, as
he began to understand fully how matters were.
To turn around, to leave the apartment, to call upon the two men
who were in the other room, to mount their steeds and descend into
the pass, all this was the work of but a few moments.
When, at length, they burst out upon the plain, the first sight that
met their eye was a band of some twenty Blackfeet. It was that part
of Tom Rutter’s party which had not been at the fray of the great
crossing. The sudden appearance of the four would have
immediately attracted their attention, had it not been otherwise
engaged.
Parsons had made somewhat of a mistake in his calculations. It
had been his intention to keep close to the mountains, and make a
trail running southward. If he could do this, and at the same time
keep out of sight of Tom Rutter and the free trappers, he might
make them believe that Adele was with him, and by drawing off their
attention and forces in this direction, Waving Plume and the Major’s
daughter might possibly have a chance to escape. The nature of the
place was favourable to the plan, and, had it not been for the
Indians, it might have been successful.
Unfortunately they were half a mile closer than he expected them
to be, and as he rode out through the narrow, rocky, bush-sheltered
passage, he fell, as it were, right into their hands. With a loud
whoop, he clapped heels to his horse’s side, and endeavoured to
dodge past them, but in vain. One of those nearest to him, and who
was armed with a rifle, drew sight on the luckless trapper. Without
waiting to ascertain whether the fleeing man was friend or foe, he
pulled the trigger and fired.
Though the ball missed its intended mark, nevertheless it took
fatal effect upon the horse which Jake bestrode, and, with one
prodigious leap, its vital energies were expended. Though it fell so
suddenly, its rider was not to be caught unprepared. Leaping nimbly
aside, he avoided being crushed, and with steady aim covered the
Indian who had fired the shot. He, knowing his almost certain fate,
attempted to throw himself behind his horse, but his motion was not
quick enough. A sharp crack, a whistling bullet, and the steed was
avenged. To turn and rush toward the cover of the woods was his
next move, and, with a score of red-skins, and the four whites to
spur him on, he made the tallest kind of running.
A perfect storm of bullets and arrows was launched at him, but
still was he unharmed. A number of the Blackfeet dismounted, and
closed in upon him; but the hardy white disdained to yield.
Drawing his heavy rifle over his shoulder, he anticipated their
attack by leaping upon them. For a few moments there was a lively
time among the party, but numbers and resolution were too much
for resolution alone, and Jake was finally borne to the ground. Even
then he did not, at once, give in, but made most frantic efforts to
draw his knife. At length, after a most desperate fight, he was
bound, though not without the assistance of Big Dick and Tom
Rutter.
“Thar, darn yer ornary picturs, you’ve got me; but ye had a good
time adoin’ it. See what yer’ll make of me, ye low-lived, red-skinned
devils!”
To this exclamation of Parsons, which showed that his mind was
not under control, if his body was, no immediate attention was paid,
Tom Rutter, all panting with his exertions, exclaiming:
“Whar is the gal—ye?”
CHAPTER VII.
PARSONS AND ARCHER IN THE BLACKFOOT
VILLAGE.
The night wore on. The sighing winds crept slowly around the
wigwam, or sorrowfully wailed up the streets of the Blackfoot village.
The dim, ghostly circle around the moon deepened into blackness;
dim clouds grew in size, looming forebodingly, and a chill, damp
feeling filled the air. Without the wigwam, which served as a prison
for Major Robison and his friends, three dusky warrior sentinels
stalked, their arms well secured under the folds of their close
wrapped blankets. Silence came, like cotton-down, upon the
surrounding village, and all was quiet.
From within came no sound indicative of aught of life; but by the
light of the low-burned, smouldering brand, three persons held a
whispered conversation. It was Waving Plume who first spoke out,
and asked his companions to make, at least, one more desperate
attempt to escape. It was Waving Plume who first spoke of what all
three had before been thinking.
“Time hurries on, Major, and the hour of midnight must be well
past. To remain here is certain death, and that, too, without having
the consolation of knowing that thereby we are in the least
benefitting your daughter. Darkness, without, appears to be thick,
and guards slacking in their vigilance—what say you, then, to a
desperate try for life and liberty?”
“No need to ask me that question, Archer. I have that to nerve me
for the struggle which may come; and much of all one loves, hangs
trembling in the balance. Here are we, with unbound hands, our
lives, and the lives of our friends at stake—the chance of success, to
one of us, at least, tolerable—why then should we delay. Let us
hasten to leave.”
The step of the sentinels without had ceased. A low murmur of
conversation came in from the corner opposite to the door. The men
without had seen Jake Parsons and Archer most thoroughly bound,
and they had not the slightest suspicion but what Major Robison was
in the same predicament. A thought of bad faith from Tom Rutter
never crossed their minds. With such subjects as might beguile their
savage minds, they kept up their conversation, leaving the tight
binding withes which had entwined the wrists of their captives, and
the chance of fortune to take care of the prisoners. Thus, in silence,
and with lips somewhat quivering, and hearts almost silenced in
their beating, the three stole out, all unarmed, save the heavy
hunting-knife which Waving Plume carried in his bosom.
Robison and Parsons crept along side by side; but Charles Archer
followed some half dozen paces in the rear, covering the retreat, and
occupying, as he thought, the post of danger.
A faint sound of pattering feet, following close behind, saluted the
ear of Waving Plume, so that, with knife drawn, and in a crouching
position, he awaited the nearer approach of the object. It proved to
be something which is but rarely met with—a really courageous
Indian dog. With only a single bark, with only a low, deep growl, he
sprang straight at the neck of Archer.
He, however, on his guard, threw up his left arm to ward off the
attack, at the same time striking a powerful blow at the side of the
animal. It proved a fatal one, for, with a sound, the mere repetition
of his growl, he fell lifeless to the ground; while our hero,
withdrawing his steel, turned to follow in the track of his still
advancing friends. They, not perceiving that he had stopped, silently
continued their journey, leaving their rear guard to stand with his
reeking knife firmly clasped in his hand, perplexedly listening in the
endeavour to guess the direction taken by his companions.
In five minutes Archer had extricated himself from the village, had
traversed a distance of a hundred yards due west, and had then,
with a Westerner’s instincts, turned and struck a course almost due
south. To the south were friends: to the south help, freedom. But, if
to the south lay safety, so, to the south lay danger. Outlying pickets
returning bands of warriors, a tangled path—these, and darkness
were before him. But death howled behind him, and forward,
forward through the night, he pressed.
Hastening on, his teeth firm set, his eyes straining to pierce the
darkness, his hand tightly clenching his hunting-knife, there came
suddenly to his ears the sound of a rapidly approaching horseman.
Not far distant was he, either, and though the danger of halting was
almost commensurate with that of proceeding, still he thought it
best to halt, and, if possible, escape the notice of the coming foe.
For not one moment could he suppose that any but a foe might ride
so recklessly in such close proximity to the Indian town.
Halting, then, he threw himself at full length upon the ground,
hoping that good fortune and the darkness of the night might once
again befriend him. At three yards distance he was invisible; it would
be a keen-scented man, indeed, who might detect his presence.
The steed came nearer, the soft ground and tangled prairie grass,
deadening the sounds of his approach.
Onward, and still onward the red-man swept.
Suddenly, from the very ground at his feet, arose a form, shadowy
and spectral, reaching one arm toward the head of his steed, the
other brandished back. Startled, his self-possession most sternly
attacked, almost stunned by this ghostly apparition, his hand bore
hard on the leathern thong of his bridle, and a twitch of the wrist,
tried to turn the horse to one side. But, though the nerves of the
rider were steel, not so with the animal he bestrode; and, though
coming to a halt so suddenly as to be thrown back upon its
haunches, farther than that he refused to do. So, as the hand of the
warrior felt for the ready tomahawk, the phantom form gave a
bound forward, the next moment, with a sweeping, hissing sound,
the knife of Archer went hilt-home to the heart of the red-man.
Possessed, then, of steed and fire-arm, with foes behind and
friends before, careless—reckless—of pursuers and pickets,
straightforward through the gloom, dashed the escaped prisoner.
Somewhat tired was the steed, but the clouds rifted, the wailing
winds sighed more softly, the moon again beamed out bright; and as
hours sped on, and were thrown backward by the flying hoofs, the
bright auroras tinged the eastern clouds, and John Howell, from his
look-out by the foot of a thickly wooded hill, keeping sharp guard
while his companions slept, caught glimpse of a strange figure,
mounted on a foam flecked and weary steed, bearing down full and
hard upon him. So, too, with Antonio, the half-breed, who, with the
Crows following in his footsteps, had pushed on, and had, on the
previous day, overtaken the trappers. He and Howell, together
watching, descried the unknown figure, and, at first were somewhat
ruffled in their minds, but at length, with a joyous clap of the hand
upon his thigh, Howell shouted:
“Waving Plume, by mighty!”
CHAPTER IX.
ATTACK ON THE BLACKFOOT VILLAGE—
RESCUE OF THE PRISONERS.
One evening Adele and Archer stood together, looking through the
dim twilight, out over the far-stretching plains. There was a smile on
her face, both bright and joyous, for Waving Plume held her hand in
his, and whispered into her ear, both low and softly:
“Yes, Adele, I have seen much of the ruder elements of life; I have
drained the cup of danger, and lived in an atmosphere of hardship;
but shall I not have my reward?”
What more he said we know not, but when her answer came, he
printed a kiss upon her ripe, red lips, and then, with his arm twined
around her waist, the two stood in the fast-fading twilight of the
deep embrasure, whispering of hope, and love, and bright days to
come.
THE END.
Printed by James Jackson, and Published by him at his Publishing Office, 2
Red Lion Court, Fleet Street, London, E.C.
Read
Boys of London
and
Boys of New York.
PUBLISHED BY
JAMES JACKSON.
*** END OF THE PROJECT GUTENBERG EBOOK FREE TRAPPER'S
PASS; OR, THE GOLD-SEEKER'S DAUGHTER! ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookultra.com