Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL with two JOINs
Message
 
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01686104
Message ID:
01686151
Views:
35
>>Thank you very much for the detailed explanation. And for the links to your articles. Your descriptions really helps to understand. I already printed one of your articles on Handling Hierarchical Data.
>>As to CTE, I don't know if this will work on my PC since I still use SQL Server 2008 Developers Edition. I may try a simple example just to make sure the CTE works on this version of SQL Server and I can test a different approach.
>
>Dmitry, you already received some good answers, just a few additional items....
>
>At the simplest level (and forgetting about hierarchical queries for a moment), a CTE is nothing more than syntax sugar. It's a way to take a derived table subquery (or more than 1) and place it "above" the main SELECT, for readability.
>
>The following two lines of code do the same thing. (I realize this is so simple an example that you'd never actually write something this way, but just to illustrate the point) . Let's say you wanted to get invoices for 2023 that had a type of "internal".
>
>
>;with tempcte as (select * from Invoices where year(InvoiceDate) = 2023)
>select * from tempcte where invoicetype = 'Internal'
>
>
>versus
>
>select * from ( select * from invoices where year(InvoiceDate) = 2023) t where InvoiceType = 'Internal'
>
>
>Again, in reality, you'd write it as one straight query.....but just to illustrate the point, in the first query, all SQL Server is doing is allowing you to place the "main" query up in a CTE, and reference it. Under the hood, SQL Server is treating it as one query......it's not a case where there's a "temp table" in between the "with" line and the "select line"......it's basically a syntax trick so that you can place the syntax above. Some developers like to use this for readability when writing more complex queries against derived tables.
>
>If you're not querying hierarchical data, I can't think of a reason why a CTE would be REQUIRED.....(I can certainly understand how people prefer them for readability). But if you're not doing anything hierarchical, there's nothing a CTE can do that in-line, derived table subqueries can't do.
>
>It's when you get to hierarchical data that CTEs and recursive queries really shine. For a few years I worked on a cost model project where we had a cost buildup based on steel going through so many different chemical and facility processes....and the list was variable. So being able to recursively query to get cumulative numbers was a major help.

Kevin,
Thank you very much for your explanation. Now I understand CTE fairly well.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform