How can I remove distinct from subquery?

Hello, I'll expose my problem, I don't know if may be is a bug.
Suppose We have
Table A:
field A1 primary key
field A2
field A3
Table B
field B1 primary key
field B2 foreig key to A(A1)
field B3
I use reverse tool to build objects from DB schema.
Everything runs ok but when I want to recover A objects
with sum(B3) from B,it doesn't runs fine.
To achieve this I've made a class C with fields of class A and a double
for sum. The code I've use is:
KodoQuery query = (KodoQuery)getPersistenceManager()
.newQuery(A.class, "this == b.b2");
query.declareVariables("B b");
query.declareImports("x.x.B");
query.setResultClass(C.class);
query.setResult("this as a, sum(b.b3) as s");
query.setGrouping("a");
Collection c = query.execute();
I use PostGreSQL 7.4, and the sql that Kodo generates is like this:
SELECT s.A1, SUM(s.C) AS c
FROM ( SELECT DISTINCT a.A1 AS A1, s.B3 AS C
FROM PUBLIC.A a INNER JOIN PUBLIC.B b ON (a.A1 = b.B2) ) s
GROUP BY s.A1
But the problem is 'DISTINCT' keyword. If I have records on B
with same B3 value then:
B1=1,B2=1,B3=25
B1=2,B2=1,B3=25
The query returns SUM(B3)=25 which is not valid!!!, it must be 50.
It fails with repeated B3 values because distinct keyword.
I bypass this adding a third field to class C that I will not use, and
introduce it into query:
KodoQuery query = (KodoQuery)getPersistenceManager()
.newQuery(A.class, "this == b.b2");
query.declareVariables("B b");
query.declareImports("x.x.B");
query.setResultClass(C.class);
query.setResult("this as a, sum(b.b3) as s, sum(b.b1) as t");
query.setGrouping("a");
Collection c = query.execute();
With this, I force to put primary key into subquery, and disables
DISTINCT, but I never will use this field :(. SQL generated by KODO:
SELECT s.A1, SUM(s.C) AS c, SUM(s.T) as t
FROM ( SELECT DISTINCT a.A1 AS A1, s.B3 AS C, s.B1 as T
FROM PUBLIC.A a INNER JOIN PUBLIC.B b ON (a.A1 = b.B2) ) s
GROUP BY s.A1
Questions:
1.- What may I doing wrong?
2.- How can I disable Distinct from subquerys?
And, no, I don't want to select by class B and then search A objects.
I want a C object with A object plus SUM of values from B.
I am achieving this with this patch.
I know that when there aren't B objects I don't receive results.
Is there any way to build an OUTER JOIN with JDOQL ?
In order to bypass this, I execute the query another time but reseting to
0, but I will prefer to be able to build an OUTER JOIN ;)
PD: Little class descriptions:
class A{ int a1, int a2, int a3} (autogenerated by kodo)
class B{ int b1, int b2, double b3} (autogenerated by kodo)
class C{ A a, double c, int t} (generated by me)
Sorry for my english :(
thanks.

I'll address your outer join question first, since it's the easiest. Kodo will
create outer joins automatically as needed to satisfy your JDOQL. In this case,
your JDOQL does not warrant an outer join. Your filter criteria on the query is
"this == b.b2". That means the query cannot and should not return any values
when there are no A's that match that join. Why not try this equivalent but
simpler query?
Query q = pm.newQuery (B.class);
q.setResult ("b2 as a, sum(b3) as s");
q.setGrouping ("b2");
q.setResultClass (C.class);
That will return results grouped by the A objects from the b2 field, and the sum
of the b3 field for all B's in each group. It should use an outer join.
About DISTINCT:
The pattern of a distinct subselect is caused by two conditions:
1. Your result clause is non-distinct. That is, you did:
q.setResult("this as a, sum(b3) as s")
rather than:
q.setResult("distinct this as a, sum(b3) as s")
This means that you want to allow duplicates in the projected values you get back.
2. You use an unbound variable ("b") in a way that could lead to duplicates
caused by relational joins. JDO always eliminates duplicates caused by database
joins.
So in order to eliminate the possible duplicates caused by #2, Kodo issues the
query as a DISTINCT subselect. Then in order to allow duplicates of the
projected results (#1), Kodo uses a non-distinct outer select.
The simplest way to eliminate the DISTINCT subselect is to use the alternate
query on B.class I mentioned above. That will result in no DISTINCTs at all.
Another way is to use the "distinct" keyword in your setResult() call, as I
showed in #1. That will result in a DISTINCT select without any subselect.
Without reverting to a SQL query (which you can still execute through JDO APIs,
as described in the documentation), I believe those are the options available.

Similar Messages

  • How can i remove pictures from my droid turbo without having to use the touch screen on the phone at all?

    how can i remove pictures from my droid turbo without having to use the touch screen on the phone at all?  my screen is cracked and i am waiting on a replacement phone.  as far as i can tell in order to access the phone storage through my computer, i have to use the touchscreen to enable it so that the computer will see it as a camera.  I can't do that as the top half of the touschscreen is unresponsive.  any suggestions?  teaches me to use the backup services offered in the future, but i still need a short term solution for this time.

        I'm sorry to hear your screen is cracked spence425. I know its difficult to use the phone while its damaged. I'm happy you are receiving a replacement device. Have you saved any of your pictures and other data to the cloud? Here is a link about the cloud http://vz.to/1mF0GIx . Unfortunately, you will be unable to remove any data from the phone if the screen is unresponsive but if you are looking to transfer data to your new phone, it would have needed to be saved via the cloud first. Keep us posted.
    Kinquana_VZW
    Follow us on Twitter @vzwsupport

  • How can i remove Ubuntu from my laptop

    yes im an idiot, i installed ubuntu over windows when my computer died and How can i remove Ubuntu from my laptop cause i tried to reformat but kept giving me the blue screen error and its realy annoying me,Ubuntu just isnt right for me please help me  :L 

    aquos wrote:
    i have an h.p. g60 535 dx note book,windows was wiped out so i installed ubuntu and don't like it  is there a sequence of key strokes that will erase the pc back to factory? ubuntu  is useless ...thanks  i forgot what ever name i made up to register.  just use m.h.Not if you wipe the Factory recovery partition then you will have to order the Factory REcovery media from hp. Obtaining HP Recovery Discs or an HP USB Recovery Drive
    Read this before ordering your Recovery Discs! (US and Canada Only)

  • How can I remove something from my iTunes download list permanently?

    Same as title: how can I remove something from my download list FOREVA! Why would I want to do this? Because it's a piece of poo free sampler from Best Buy that is effing huge, and terrible and I don't want it, ever (it's been stuck to my list since 08, and it must die). Every time I buy something I have to remember to go and remove that from my list, otherwise iTunes gets caught up downloading that thing, which is many times larger than any video/cd/app that I have ever downloaded on iTunes before. It wouldn't be as much an issue if I had a good ISP but I don't, so any non-apple employee people giving some smarmy "get better internets" advice need not apply.

    Once an item enters the download queue, the user cannot remove it. Either let it complete the download (easiest) or contact the iTunes Store customer support department through the Contact page and ask them if they'll remove the item(s) from your queue for you.

  • How can I remove Growl from my Mac Pro

    Hi there!
    How can i remove Growl from my Mac Pro?

    David ..
    Instructions here >  Growl - Removing Growl

  • How can I remove pictures from my ipod touch that were downloaded from my computer?

    Hi... How can I remove pictures from my IPod  (4th generation) that were downloaded from my

    If the pictures were synced from a computer to the iOS device then they are removed the same way.  Connect the device to the computer.  Tell iTunes what to sync and sync.

  • How can i remove them from my purchased(no hidden them)and my account for ever?

    hello
    I got a few free app before and did not download them completely and dont want to do this,
    but they stay in my purchased and start automatly when i connect to itunes store and going to crazy me
    How can i remove them from my purchased(no hidden them)and my account for ever?
    (I want to delete all free apps and Books from my purchases.)
    please help me
    thank you.

    There is no way to do it. Others have asked for the very same thing that you are asking for, but currently it just cannot be done. You can't remover purchases from your Purchase History - you can hide them - but you can't delete them from the history.
    You can delete the Apps from the iPad and you can delete them from iTunes on the computer. Delete them from iPad, delete them fromiTunes so that when you sync there is no chance that they will come back. Then hide the purchases. That's the best that you can do.
    On the iPad, tap and hold down on the app icons until the X pops up. Tap the X to delete the apps.
    In iTunes, clcik on Apps under the Library heading on the left. Right click on the apps that you want to delete and follow the prompts.

  • How can I remove music from my iPhone that's not on a playlist, or checked off in the Sync window in any of the possible categories (Playlist, genre, artist, etc)? HELP! It's taking up SO much space on my phone

    How can I remove music from my iPhone that's not on a playlist, or checked off in the Sync window in any of the possible categories (Playlist, genre, artist, etc)? HELP! It's taking up SO much space on my phone but I cannot see a way to remove any of it

    If it's slow on startup it would be extensions loading or LaunchDaemons starting up. 
    You should have a look in:
    /Library/LaunchDaemons
    /Library/Extensions
    You can count out anything in your home folder and it shouldn't put anything in /System as that's reserved for Apple. 

  • How can i remove cydia from my phone

    How can i remove cydia from my iPhone 4

    restore it to factory settings from itunes on a computer where the phone is connected to by usb cable

  • How can I remove updates from my I-pad

    How can I remove updates from my I pad

    What updates ?
    If you mean from the Updates tab in the App Store app then you can't - any updates that you've downloaded will disappear off that last in 30 days. If you don't want to install an app update then as far as I know they can't be removed from the Updates tab.
    If you want to got back to a previous version of an app then do you have a copy of that previous version anywhere ? If you do then you can delete the app from your iPad (which will also delete its content) and sync back the previous version.
    If you mean an iOS update that you've installed then you can't, downgrading the iOS version isn't supported by Apple.

  • How can i remove password from my I Pod

    how can i remove password from my I Pod

    If you mean you can't get past the screen lock then:
    Place the iOS device in Recovery Mode and then connect to your computer and restore via iTunes. The iPod will be erased.
    iOS: Wrong passcode results in red disabled screen                         
    If recovery mode does not work try DFU mode.                        
    How to put iPod touch / iPhone into DFU mode « Karthik's scribblings        
    For how to restore:
    iTunes: Restoring iOS software
    To restore from backup see:
    iOS: How to back up     
    If you restore from iCloud backup the apps will be automatically downloaded. If you restore from iTunes backup the apps and music have to be in the iTunes library since synced media like apps and music are not included in the backup of the iOS device that iTunes makes.
    You can redownload most iTunes purchases by:
      Downloading past purchases from the App Store, iBookstore, and iTunes Store

  • How can I remove songs from my iPod without deleting them from my iTunes library?, how can I remove songs from my iPod without deleting them from my iTunes library?

    How can I remove files from my iPod without deleting them from my iTunes library? I do not see a functionailty enabling this i the iPod>

    Do not select the songs to sync, then sync.

  • How can I remove Skype from my iMac?

    How can i remove Skype from my iMac?
    Thanks

    https://support.skype.com/en/faq/FA12073/how-can-i-completely-uninstall-and-then -reinstall-skype-for-mac-os-x?frompage=search&q=uninstall+skype+mac&fromSearchFi rstPage=false
    That should assist with getting rid of skype, Just don't worry about the reinstalling part

  • How can I remove pictures from my IPad

    How can I remove photos from my IPad

    Depends on where these photos came from. If they were synced from a computer, you'll need to sync them off by unchecking the folder the pictures are in from  iTunes on the computer.
    If they were taken by the iPad or downloaded you should have a little trashcan icon at the top of the screen  you can use when viewing the image.

  • How can I remove computers from my itunes account when I no longer have the computers?

    How can I remove computers from my itunes account when I no longer have the computers?

    De-authorise all, then authorise the one you still have.

Maybe you are looking for