Need Help With SQL Method

Questions, tips and tricks and techniques for scripting in Servoy

Need Help With SQL Method

Postby tommygill » Wed Jun 30, 2010 5:15 pm

Hi All

As I am trying to update from 3.5.x to 5.1.3 I keep running against subtle differences.
In 3.5 is the following getDataByQuery query.
Code: Select all
var query_pk = "SELECT donation_detail_id FROM donation_detail WHERE donation_detail_id IN " + "(SELECT (donation_detail_id) FROM donation_detail WHERE detail_date = ? GROUP BY detail_query_totals)" + "ORDER BY detail_query_totals asc";

In 5.1.3 it throws the following error,
SQL Anywhere Error -149: Function or column reference to 'donation_detail_id' must also appear in a GROUP BY

Thanks in advance for all your assistance.
Tom

Mac OS X 10.6.7
Servoy version 5.2.0 -build 997
Java Version 1.6.0_20
User avatar
tommygill
 
Posts: 110
Joined: Thu Jan 27, 2005 1:38 pm
Location: Columbus, Indiana, USA

Re: Need Help With SQL Method

Postby martinh » Wed Jun 30, 2010 5:49 pm

What would you like to do with this query?

It is quite normal that this query will fail, so please explain what you intended to do with this query.
Martin
------------------------------------------------
Servoy Developer
Version 5.2.10/5.2.13
Java version 1.6 update 31
Database SQL Server 2008 R2
martinh
 
Posts: 857
Joined: Wed May 09, 2007 5:34 pm
Location: Belgium

Re: Need Help With SQL Method

Postby Joas » Wed Jun 30, 2010 9:11 pm

If you use GROUP BY in your query, you need to specify how you want to group the fields that you select. By doing SUM, AVG, MIN, MAX, FIRST, LAST, ...

For example for your query:
Code: Select all
var query_pk = "SELECT donation_detail_id FROM donation_detail WHERE donation_detail_id IN " + "(SELECT FIRST(donation_detail_id) FROM donation_detail WHERE detail_date = ? GROUP BY detail_query_totals)" + "ORDER BY detail_query_totals asc";
Joas de Haan
Yield Software Development
Need help on your project? yieldsd.com
User avatar
Joas
Site Admin
 
Posts: 842
Joined: Mon Mar 20, 2006 4:07 pm
Location: Leusden, NL

Re: Need Help With SQL Method

Postby tommygill » Thu Jul 01, 2010 4:20 am

Thanks Joas,

If you use GROUP BY in your query, you need to specify how you want to group the fields that you select. By doing SUM, AVG, MIN, MAX, FIRST, LAST, ...

The key word I was missing was "MIN". I rechecked my 3.5 method and it has "MIN".

MY BAD!!

Thanks a lot.
Tom

Mac OS X 10.6.7
Servoy version 5.2.0 -build 997
Java Version 1.6.0_20
User avatar
tommygill
 
Posts: 110
Joined: Thu Jan 27, 2005 1:38 pm
Location: Columbus, Indiana, USA

Re: Need Help With SQL Method

Postby hansayton » Thu Jul 01, 2010 9:08 am

It would seen very expensive to use a subquery here, certainly if this is is a large table.
If you need more complex stuff to happen in the subquery you could try:
-------------------------
with tmp as (
select distinct dd.donation_detail_id
from
donation_detail as dd
--some more (left outer) join(s)
where
dd.detail_date = ?
-- some more clauses
)
select
donation_detail_id
from
donation_detail
join dd on (
dd.donation_detail_id = donation_detail.donation_detail_id
)
order by
detail_query_totals asc
-----------------------

this will perform a lot better than using subqueries
(works on sybase 9+ and MS-sql 2005+, other backends I don't know...)

Hope this helps,
Hans Nieuwenhuijs
Hans Nieuwenhuijs
Ayton bv
Netherlands
hansayton
 
Posts: 11
Joined: Fri Nov 09, 2007 11:03 am

Re: Need Help With SQL Method

Postby martinh » Thu Jul 01, 2010 9:50 am

tommygill wrote:Thanks Joas,

If you use GROUP BY in your query, you need to specify how you want to group the fields that you select. By doing SUM, AVG, MIN, MAX, FIRST, LAST, ...

The key word I was missing was "MIN". I rechecked my 3.5 method and it has "MIN".

MY BAD!!

Thanks a lot.


I'm not sure this solves your problem. As far as I can see the query still has a problem.
There is GROUP BY detail_query_totals, but detail_query_totals is not in your result.

Please can you explain English what this query should do, so that I can see if I can propose a good query for you.
Martin
------------------------------------------------
Servoy Developer
Version 5.2.10/5.2.13
Java version 1.6 update 31
Database SQL Server 2008 R2
martinh
 
Posts: 857
Joined: Wed May 09, 2007 5:34 pm
Location: Belgium

Re: Need Help With SQL Method

Postby ROCLASI » Thu Jul 01, 2010 11:31 am

Hi Tom,

You don't need a sub-select at all:

Code: Select all
SELECT MIN(donation_detail_id)
FROM donation_detail
WHERE detail_date = ?
GROUP BY detail_query_totals
ORDER BY detail_query_totals ASC


This does exactly the same and it's MUCH more efficient.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium


Return to Methods

Who is online

Users browsing this forum: No registered users and 7 guests

cron