|
SQL Puzzlers, by Joe Celko
Joe Celko is an independent consultant in Austin, Texas, and the author of SQL Puzzles and Answers (2006), Joe Celko's SQL for Smarties: Advanced SQL Programming (2005), and Joe Celko's Trees and Hierarchies in SQL for Smarties (2004). See More by Joe Celko An Old Celko Puzzle
If you go over to http://www.dbdebunk.com/page/page/666711.htm, you will find a letter to the editor from someone named PV about an old column of mine. Here is the jist of it: Back in June of 1996, Jack Wells submitted this SQL problem to my SQL FOR SMARTIES column. His situation is pretty typical for SQL programmers who work with 3GL people. Jack spoke with Fabian Pascal, the week he was working on this problem, and Mr. Pascal replied that this query could not be done. He said, 'In a truly relational language it could be done, but since SQL is not relational it isn't possible, not even with SQL-92.' Sounds like a challenge to me! Oh, I forgot to mention an addition constraint; the answer had to be in 1996 Oracle, which had no proper outer joins, no general scalar subexpressions, and so on), so your query had to run under the old SQL-86 or SQL-89 rules back then. Assume that you have this test data: CREATE TABLE Salaries INSERT INTO Salaries VALUES ('Tom', '1996-06-20', 500.00); Tom has had two promotions, Dick is a new hire, and Harry has had one promotion. My old solution was: SELECT S0.emp_id, S0.sal_date, S0.sal_amt, +-------+------------+--------+------------+--------+ Fabian Pascal comments that "This was a very long time ago and I do not recall the exact circumstances, and whether my reply was properly represented or understood (particularly coming from Celko). My guess is that it had something to do with inability to resolve such problems without a precise definition of the tables to which the query is to be applied, the business rules in effect for the tables, and the query at issue. I will let Chris Date to respond to PV’s solution." Chris Date responed: "Regarding whether Celko’s solution is correct or not, I neither know, nor care." Then he posted an incorrect answer in his Tutorial D language that he cleaned in a second posting to this: WITH (Salaries RENAME (sal_date AS prev_date, The bad news is that this does not help the poor Oracle programmer. And it is still wrong because Chris Date's version of the Relational Model disagrees with Dr. Codd's rules about having NULLs. By having '1900-01-01' as a "magical token" (remember in Date's world, we have no missing values) we just made poor Dick into the oldest emplyee in the company, and the least appreciated. All this answer did was change the programming language, change the specs and change the calendar. Today the solution is easy enough with an outer join. Here is one way: SELECT S0.emp_id, S0.sal_date AS curr_sal_date, S0.sal_amt AS curr_sal_amt, This can be done using newer operators or with scalar subquery expression in the SELECT list. Anyone else got a Standard SQL solution? I am pretty sure this can be made simpler and if you have the SQL-99 OVER operators it should be a snap. The first entry gets a copy of my SQL PROGRAMING STYLE when it comes out or a copy of my TREES & HIERARCHIES IN SQL.
E-MAIL | SLASHDOT | DIGG This is a public forum. CMP Technology and its affiliates are not responsible for and do not control what is posted herein. CMP Technology makes no warranties or guarantees concerning any advice dispensed by its staff members or readers. Community standards in this comment area do not permit hate language, excessive profanity, or other patently offensive language. Please be aware that all information posted to this comment area becomes the property of CMP Media LLC and may be edited and republished in print or electronic format as outlined in CMP Technology's Terms of Service. Important Note: This comment area is NOT intended for commercial messages or solicitations of business.
|
Blog Channels
Cindi Howson on Business Intelligence The Brain Food Blogger Tony Byrne on Content Management SQL Puzzlers by Joe Celko Rajan Chandras on IT & Information Management Seth Grimes on Analytics In Context by Doug Henschen Phil Kemelor on Web Analytics Sandy Kemsley's Column Two Nelson King on Enterprise App Development David Linthicum on Software as a Service Natural Insight, By Mark Madsen Alan Pelz-Sharpe on Content Management Mark Smith on Performance Management Neil Raden on Business Intelligence Bruce Silver on Business Process Management Product Maven Subscribe to RSS Archives
|
|
|












