Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Common Table Expressions
09-30-2009, 06:29 PM
Post: #1
Common Table Expressions
Anyone who does database programming beyond the simplest of queries should probably have a mild understanding of Common Table Expressions. I've only discovered them recently and they are extremely helpful to simplify complex queries. Here is a very, very simple example:

    SQL Code
  1. WITH x AS (
  2. SELECT id, Name, Cost
  3. FROM Meals
  4. )
  5. SELECT AVERAGE(Cost)
  6. FROM x



Obviously you could do this with a single query very easily but this is simply to show you how the structure of it works.

Edit: I did intend to link to this thread so google would have some search terms to build from. Also, CTEs aren't available in all SQL databases. SQL Server 2000 doesn't support them but SQL Server 2005 does.
Find all posts by this user
Quote this message in a reply
10-06-2009, 06:40 PM
Post: #2
RE: Common Table Expressions
I really do only perform the simplest of queries in databases. I've never really thought to look into more advanced stuff too much because the basics I know always manage to handle what I need it to do.

Also, I don't know if it was intended or not, but the link in your post goes to this thread.

Yilduz Network
I am the Einstein of PHP.
$E = $m * pow($c,2);
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: