× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



...type, that ORDER BY should have been GROUP BY - apologies!

with STORAGE_SUMMARY as (
select id,
sum(memSize) / 1024 AS Mem_Used_GB,
sum(stgOSUsed) AS Storage_Used_GB

from VMRESMGMT.RESPF

group by id -- <-- HERE
)

select *,
memMax AS Mem_Max_GB,
stgMax AS Storage_Max_GB

from STORAGE_SUMMARY

left join VMRESMGMT.RESKONPF using (id)

where ifnull(ResKonPF.memMax, 0) > 0 and ifnull(ResKonPF.stgMax, 0) > 0 and (
Mem_Used_GB > ResKonPF.memMax OR Storage_Used_GB > ResKonPF.stgMax
)

order by id

Tim.


________________________________
From: MIDRANGE-L <midrange-l-bounces@xxxxxxxxxxxxxxxxxx> on behalf of Tim Fathers <X700-IX2J@xxxxxxxxxxx>
Sent: 22 January 2020 18:13
To: Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxxxxxxxx>
Subject: Re: SQL syntax error with fields and expressions in SELECT

Patrik,

Having had a better look, I think you could refactor your SQL as follows using a common table expression, which avoids the problems with the aggregated columns and the duplicate expressions. Hopefully it works as I haven't tested it!

with STORAGE_SUMMARY as (
select id,
sum(memSize) / 1024 AS Mem_Used_GB,
sum(stgOSUsed) AS Storage_Used_GB

from VMRESMGMT.RESPF

order by id
)

select *,
memMax AS Mem_Max_GB,
stgMax AS Storage_Max_GB

from STORAGE_SUMMARY

left join VMRESMGMT.RESKONPF using (id)

where ifnull(ResKonPF.memMax, 0) > 0 and ifnull(ResKonPF.stgMax, 0) > 0 and (
Mem_Used_GB > ResKonPF.memMax OR Storage_Used_GB > ResKonPF.stgMax
)

Tim.


________________________________
From: MIDRANGE-L <midrange-l-bounces@xxxxxxxxxxxxxxxxxx> on behalf of Patrik Schindler <poc@xxxxxxxxxx>
Sent: 22 January 2020 17:46
To: Midrange Systems Technical Discussion <midrange-l@xxxxxxxxxxxxxxxxxx>
Subject: Re: SQL syntax error with fields and expressions in SELECT

Hello Tim,

Am 22.01.2020 um 17:19 schrieb Tim Fathers <X700-IX2J@xxxxxxxxxxx>:

I thought you must be mistaken, but surprisingly, when I tried it in MariaDB/MySQL it works! However, it seems to pick arbitrary values which is not what you ought to rely on in any case so I think DB2 failing is the correct thing to do.

:-)

The problem is this, if you have a table as follows:

ID MAX_MEM
1 1
1 5
2 2
2 5
2 8
3 7

...and you do

select ID, MAX_MEM from TABLE group by ID

How should SQL know which value from MAX_MEM should be included in the summarised row?

Good point. I'm not sure if your example applies, though, because there's just one row in ResKonPF per ID. Maybe that's the reason why MySQL was doing the right thing besides the SQL being ambiguous.

As I said above, it seems MySQL is indeterminate, which is never a good thing, so you need to either select min(MAX_MEM), max(MAX_MEM) or use some other aggregating function to aggregate/select the value. If there's some other column that dictates which MAX_MEM value has priority then you can use the OLAP functions to order and pick the value you need.

Okay, understood.

I got this one to run properly and show the same values as in MySQL:

SELECT
id,
SUM(memSize)/1024 AS Mem_Used_GB,
memMax AS Mem_Max_GB,
SUM(stgOSUsed) AS Storage_Used_GB,
stgMax AS Storage_Max_GB
FROM vmresmgmt/respf
LEFT JOIN vmresmgmt/ResKonPF USING (id)
GROUP BY id, memMax, stgMax
HAVING (
NOT (IFNULL(ResKonPF.memMax, 0) = 0 OR IFNULL(ResKonPF.stgMax, 0) = 0 )
) AND (
(SUM(memSize)/1024) > ResKonPF.memMax OR (SUM(stgOSUsed)) > ResKonPF.stgMax
)
ORDER BY id

Important to note is that I can't use alias names as defined in "SELECT foo AS bar" within the HAVING clause. I must repeat the exact expression within HAVING as used in the SELECT. Also something where MySQL behaves different (and calculates the expression just once while DB2 may optimise the same calculations to one shot or not).

Thanks for your Help, Tim! Greatly appreciated!

:wq! PoC

PGP-Key: DDD3 4ABF 6413 38DE - https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.pocnet.net%2Fpoc-key.asc&amp;data=02%7C01%7C%7C79daf1e214f84434552b08d79f5e761a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637153100368085067&amp;sdata=GTFTP2eHkebZsV%2FtYZIeXvmdsGM4ppkHIh6IWjZutWk%3D&amp;reserved=0


--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.midrange.com%2Fmailman%2Flistinfo%2Fmidrange-l&amp;data=02%7C01%7C%7C79daf1e214f84434552b08d79f5e761a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637153100368095076&amp;sdata=UnrytPMKtFcHrJAU5HBFMNpCtR1uZmcw6PXlGe%2BeX%2BA%3D&amp;reserved=0
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Farchive.midrange.com%2Fmidrange-l&amp;data=02%7C01%7C%7C79daf1e214f84434552b08d79f5e761a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637153100368095076&amp;sdata=J2OkIOVMDDk4UCHGqPc8%2BZb2Tmni0h8I1ILkTVjIaG8%3D&amp;reserved=0.

Please contact support@xxxxxxxxxxxx for any subscription related questions.

Help support midrange.com by shopping at amazon.com with our affiliate link: https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Famazon.midrange.com&amp;data=02%7C01%7C%7C79daf1e214f84434552b08d79f5e761a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637153100368095076&amp;sdata=ldi0mIBS%2BnF15Q%2FmhEvUpLQQmwfOH3FOoaqjn9B2U38%3D&amp;reserved=0
--
This is the Midrange Systems Technical Discussion (MIDRANGE-L) mailing list
To post a message email: MIDRANGE-L@xxxxxxxxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.midrange.com%2Fmailman%2Flistinfo%2Fmidrange-l&amp;data=02%7C01%7C%7C79daf1e214f84434552b08d79f5e761a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637153100368095076&amp;sdata=UnrytPMKtFcHrJAU5HBFMNpCtR1uZmcw6PXlGe%2BeX%2BA%3D&amp;reserved=0
or email: MIDRANGE-L-request@xxxxxxxxxxxxxxxxxx
Before posting, please take a moment to review the archives
at https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Farchive.midrange.com%2Fmidrange-l&amp;data=02%7C01%7C%7C79daf1e214f84434552b08d79f5e761a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637153100368095076&amp;sdata=J2OkIOVMDDk4UCHGqPc8%2BZb2Tmni0h8I1ILkTVjIaG8%3D&amp;reserved=0.

Please contact support@xxxxxxxxxxxx for any subscription related questions.

Help support midrange.com by shopping at amazon.com with our affiliate link: https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Famazon.midrange.com&amp;data=02%7C01%7C%7C79daf1e214f84434552b08d79f5e761a%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637153100368095076&amp;sdata=ldi0mIBS%2BnF15Q%2FmhEvUpLQQmwfOH3FOoaqjn9B2U38%3D&amp;reserved=0

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.