I was just messing around trying to learn things and attempted the
following:
select brch_nbr
, sum(case when post_flag != 'P' then amount else 0 end) as sum_amount
from film.film_trans actions
group by brch_nbr
having sum_amount 0
order by sum_amount desc
;
This didn't work because sum_amount is not yet defined (or whatever) at the
time that the having clause is evaluated. So then I did this:
Example 1)
select brch_nbr
, sum(case when post_flag != 'P' then amount else 0 end) as sum_amount
from film.film_trans actions
group by brch_nbr
having sum(case when post_flag != 'P' then amount else 0 end) 0
order by sum_amount desc
;
This worked, but I didn't like it because if I changed how sum_amount is
calculated I'd have to make a change in two places.
So then I changed it to use a nested table expression thusly:
Example 2)
select brch_nbr
, sum_amount
from (
select brch_nbr
, sum(case when post_flag = 'P' then 0 else amount end) as
sum_amount
from film.film_trans actions
group by brch_nbr
) as ft
where sum_amount 0
order by sum_amount desc
;
This worked fine and in fact gave me the same 'cost' as Example 1.
Then I thought I might make it more readible, to me, and changed it to use a
common table expression instead:
Example 3)
with
ft as (
select brch_nbr
, sum(case when post_flag = 'P' then 0 else amount end) as
sum_amount
from film.film_trans actions
group by brch_nbr
)
select brch_nbr
, sum_amount
from ft
where sum_amount 0
order by sum_amount desc
;
This also gave me the same 'cost' as the other two.
Other than the 'readibility' factor, which I'm assuming is just a matter of
personal preference, is there any other reason I'd chose a CTE over an NTE,
or NTE over CTE?
I like the CTE, personally, because it better segregates the definition of
the temporary table itself and the select of the data from that table.
Thanks,
Frank
---
Frank Swarbrick
Senior Developer/Analyst - Mainframe Applications
FirstBank Data Corporation - Lakewood, CO USA 3 9758
On Mar 6, 5:05 pm, "Frank Swarbrick" <Frank.Swarbr.. .@efirstbank.co m>
wrote:
I was just messing around trying to learn things and attempted the
following:
select brch_nbr
, sum(case when post_flag != 'P' then amount else 0 end) as sum_amount
from film.film_trans actions
group by brch_nbr
having sum_amount 0
order by sum_amount desc
;
This didn't work because sum_amount is not yet defined (or whatever) at the
time that the having clause is evaluated. So then I did this:
Example 1)
select brch_nbr
, sum(case when post_flag != 'P' then amount else 0 end) as sum_amount
from film.film_trans actions
group by brch_nbr
having sum(case when post_flag != 'P' then amount else 0 end) 0
order by sum_amount desc
;
This worked, but I didn't like it because if I changed how sum_amount is
calculated I'd have to make a change in two places.
So then I changed it to use a nested table expression thusly:
Example 2)
select brch_nbr
, sum_amount
from (
select brch_nbr
, sum(case when post_flag = 'P' then 0 else amount end) as
sum_amount
from film.film_trans actions
group by brch_nbr
) as ft
where sum_amount 0
order by sum_amount desc
;
This worked fine and in fact gave me the same 'cost' as Example 1.
Then I thought I might make it more readible, to me, and changed it to use a
common table expression instead:
Example 3)
with
ft as (
select brch_nbr
, sum(case when post_flag = 'P' then 0 else amount end) as
sum_amount
from film.film_trans actions
group by brch_nbr
)
select brch_nbr
, sum_amount
from ft
where sum_amount 0
order by sum_amount desc
;
This also gave me the same 'cost' as the other two.
Other than the 'readibility' factor, which I'm assuming is just a matter of
personal preference, is there any other reason I'd chose a CTE over an NTE,
or NTE over CTE?
I like the CTE, personally, because it better segregates the definition of
the temporary table itself and the select of the data from that table.
Thanks,
Frank
---
Frank Swarbrick
Senior Developer/Analyst - Mainframe Applications
FirstBank Data Corporation - Lakewood, CO USA
Hi, Frank
Yes--I think the readability, modularity, reusability, and reduction
in query size afforded by the use of CTEs is compelling. AFAIK, if you
don't reuse your CTE in the query, DB2 treats it just like a nested
table (which is probably why your numbers are consistent), whereas if
you refer to it more than once, through, e.g., an alias, DB2 may
create a TEMP table for it.
Regards,
--Jeff
jefftyzzer wrote:
On Mar 6, 5:05 pm, "Frank Swarbrick" <Frank.Swarbr.. .@efirstbank.co m>
wrote:
>I was just messing around trying to learn things and attempted the following:
select brch_nbr , sum(case when post_flag != 'P' then amount else 0 end) as sum_amount from film.film_trans actions group by brch_nbr having sum_amount 0 order by sum_amount desc ;
This didn't work because sum_amount is not yet defined (or whatever) at the time that the having clause is evaluated. So then I did this:
Example 1) select brch_nbr , sum(case when post_flag != 'P' then amount else 0 end) as sum_amount from film.film_trans actions group by brch_nbr having sum(case when post_flag != 'P' then amount else 0 end) 0 order by sum_amount desc ;
This worked, but I didn't like it because if I changed how sum_amount is calculated I'd have to make a change in two places. So then I changed it to use a nested table expression thusly:
Example 2) select brch_nbr , sum_amount from ( select brch_nbr , sum(case when post_flag = 'P' then 0 else amount end) as sum_amount from film.film_trans actions group by brch_nbr ) as ft where sum_amount 0 order by sum_amount desc ;
This worked fine and in fact gave me the same 'cost' as Example 1. Then I thought I might make it more readible, to me, and changed it to use a common table expression instead:
Example 3) with ft as ( select brch_nbr , sum(case when post_flag = 'P' then 0 else amount end) as sum_amount from film.film_trans actions group by brch_nbr ) select brch_nbr , sum_amount from ft where sum_amount 0 order by sum_amount desc ;
This also gave me the same 'cost' as the other two. Other than the 'readibility' factor, which I'm assuming is just a matter of personal preference, is there any other reason I'd chose a CTE over an NTE, or NTE over CTE? I like the CTE, personally, because it better segregates the definition of the temporary table itself and the select of the data from that table.
Thanks, Frank
--- Frank Swarbrick Senior Developer/Analyst - Mainframe Applications FirstBank Data Corporation - Lakewood, CO USA
Hi, Frank
Yes--I think the readability, modularity, reusability, and reduction
in query size afforded by the use of CTEs is compelling. AFAIK, if you
don't reuse your CTE in the query, DB2 treats it just like a nested
table (which is probably why your numbers are consistent), whereas if
you refer to it more than once, through, e.g., an alias, DB2 may
create a TEMP table for it.
Correct. In fact if you use it more than once DB2 will start of with a
temp and the optimizer may later decide to "break the CSE" if it deems
it semantically safe.
Interesting side-note:
A HAVING clause is nothing else than a short form for a WHERE clause
over the GROUP BY of a nested query. So all three queries are identical.
Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
On 3/6/2007 at 8:52 PM, in message <55************ *@mid.individua l.net>,
Serge Rielau<sr*****@ ca.ibm.comwrote :
jefftyzzer wrote:
>On Mar 6, 5:05 pm, "Frank Swarbrick" <Frank.Swarbr.. .@efirstbank.co m> wrote:
>>I was just messing around trying to learn things and attempted the following:
select brch_nbr , sum(case when post_flag != 'P' then amount else 0 end) as
sum_amount
>> from film.film_trans actions group by brch_nbr having sum_amount 0 order by sum_amount desc ;
This didn't work because sum_amount is not yet defined (or whatever) at
the
>>time that the having clause is evaluated. So then I did this:
Example 1) select brch_nbr , sum(case when post_flag != 'P' then amount else 0 end) as
sum_amount
>> from film.film_trans actions group by brch_nbr having sum(case when post_flag != 'P' then amount else 0 end) 0 order by sum_amount desc ;
This worked, but I didn't like it because if I changed how sum_amount
is
>>calculated I'd have to make a change in two places. So then I changed it to use a nested table expression thusly:
Example 2) select brch_nbr , sum_amount from ( select brch_nbr , sum(case when post_flag = 'P' then 0 else amount end) as sum_amount from film.film_trans actions group by brch_nbr ) as ft where sum_amount 0 order by sum_amount desc ;
This worked fine and in fact gave me the same 'cost' as Example 1. Then I thought I might make it more readible, to me, and changed it to
use a
>>common table expression instead:
Example 3) with ft as ( select brch_nbr , sum(case when post_flag = 'P' then 0 else amount end) as sum_amount from film.film_trans actions group by brch_nbr ) select brch_nbr , sum_amount from ft where sum_amount 0 order by sum_amount desc ;
This also gave me the same 'cost' as the other two. Other than the 'readibility' factor, which I'm assuming is just a matter
of
>>personal preference, is there any other reason I'd chose a CTE over an
NTE,
>>or NTE over CTE? I like the CTE, personally, because it better segregates the definition
of
>>the temporary table itself and the select of the data from that table. Hi, Frank
Yes--I think the readability, modularity, reusability, and reduction in query size afforded by the use of CTEs is compelling. AFAIK, if you don't reuse your CTE in the query, DB2 treats it just like a nested table (which is probably why your numbers are consistent), whereas if you refer to it more than once, through, e.g., an alias, DB2 may create a TEMP table for it.
Correct. In fact if you use it more than once DB2 will start of with a
temp and the optimizer may later decide to "break the CSE" if it deems
it semantically safe.
Interesting side-note:
A HAVING clause is nothing else than a short form for a WHERE clause
over the GROUP BY of a nested query. So all three queries are identical.
Cool. I'm glad. Thanks for the information!
Frank
---
Frank Swarbrick
Senior Developer/Analyst - Mainframe Applications
FirstBank Data Corporation - Lakewood, CO USA This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Robert Kern |
last post by:
I'm in the middle of writing a scientific program with a number of very, very
long formulae. You can see a typical one below. What I would like to do is to
parse this code to yield an AST, identify identical subtrees, replace them in
the AST with a dummy variable to which I will assign the common subexpression.
v21c = ((1-2*nu)*((2*(1-nu)ctB*ctB-nu)*log(Ry3bar) - \
(2*(1-nu)*ctB*ctB+1-2*nu)*cB*log(Rz3bar)) - \...
|
by: Michael Hobbs |
last post by:
Yeah, yeah, another X-Oriented paradigm, but please hear me out.
I have recently been studying up on EJB's in order to extend my resume.
I have looked at J2EE several times over the past years, but have never
been impressed by it. Now that I've studied it in detail, I know why. A
strongly typed language such as Java is poorly suited to model tables
and relations.
I got to thinking that a more dynamically typed language, such as...
|
by: Xah Lee |
last post by:
Here's the belated Java solution.
import java.util.List;
import java.util.ArrayList;
import java.lang.Math;
class math {
public static List range(double n) {
return range(1,n,1);
}
|
by: randi_clausen |
last post by:
Using SQL against a DB2 table the 'with' key word is used to
dynamically create a temporary table with an SQL statement that is
retained for the duration of that SQL statement.
What is the equivalent to the SQL 'with' using TSQL? If there is not
one, what is the TSQL solution to creating a temporary table that is
associated with an SQL statement? Examples would be appreciated.
Thank you!!
|
by: Endif |
last post by:
I am tring to execute the following SQL statements through the Iseries
Navigator for DB2/V8.2, But i come up with an error saying recursion is
not allowed in common table expression. This is a example i picked up
from SQL cook book. I am not sure where i am wrong.
Any help is appreciated
WITH TEMP ( SUPV_ID,EMPID, FIRSTNAME) AS
(
SELECT TV.SUPV_ID,TV.EMPID, TV.FIRSTNAME
| |
by: Matt T. |
last post by:
I am trying to replace the nested table tag in the follow string
using a regular expression, but I am not having any success. I am new
at using regular expressions, so I am sure I am just overlooking
something simple.
I thought <table.*>.*<table.*>.*</table> or some variation of that
would work to create a match, but it does not. Anyone have an idea as
to what would work?
|
by: a |
last post by:
I need to write a regular expression to match a quoted string in which the
double quote character itself is represented by 2 double quotes. For
example:
"beginning ""nested quoted string"" end"
Any idea how to write this in boost::xpressive or boost::regex.
Thanks,
|
by: jpatchak |
last post by:
I have a query that uses a person's state and other demographic information to look up rate factors. The rate factors are stored in tables. Each state has its own table (each state will have at least 15,000 records). I can get the rate factors to pull up correctly if I use a bunch of nested IIF statements. For example;
IIF(tblPerson.State = "AL", (Select from where ...), IIF(tblPerson.State = "AZ", (Select where...), IIF(...
you get...
|
by: =?iso-8859-1?q?C=E9dric_Lucantis?= |
last post by:
Le Thursday 26 June 2008 15:53:06 oyster, vous avez écrit :
The construct does not match a whole word but only one char, so
means "any char which is not t, a, b, l or e".
Anyway the inside table word won't match your pattern, as there are '<'
and '>' in it, and these chars have to be escaped when used as simple text.
So this should work:
re.compile(r'<table(|.*)>.*</table>')
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |