1

I had a large explanation (in my mind it was an explanation, in reality it was more 10 pages of rambling) and then my browser crashed. So, I'll just give a quick overview of the problem as I'm not sure exactly what information is needed.

I have the following query model. The one on top is what currently happens and the one below is what I need to happen

                DetailsTable
                      |            
                 ObjectTable1
                      |            
              OtherDetailsTable

                DetailsTable
               /            \
         ObjectTable1  ObjectTable2
               \            /
              OtherDetailsTable

ObjectTable1 and ObjectTable2 store different objects and are both children of one parent object that contains no data.

They have foreign keys which link them to the other tables.

The query I need is generated by code and seems to like using uniontables (I can't really control this unless I hardcode the query which isn't really an option.

The current Query generated is the following:

SELECT UNIONTABLE.objectID, 
       UNIONTABLE.unqiueID, 
       UNIONTABLE.f, 
       UNIONTABLE.updatedate, 
       UNIONTABLE.version, 
       UNIONTABLE.objectID_updatedby, 
       UNIONTABLE.unqiueID_updatedby, 
       UNIONTABLE.amount, 
       UNIONTABLE.currency, 
       UNIONTABLE.name, 
       UNIONTABLE.number, 
       UNIONTABLE.funds, 
       UNIONTABLE.print, 
       UNIONTABLE.paydate, 
       UNIONTABLE.recover, 
       UNIONTABLE.address, 
       UNIONTABLE.payee, 
       UNIONTABLE.objectID_nominated, 
       UNIONTABLE.unqiueID_nominated, 
       UNIONTABLE.objectID_override, 
       UNIONTABLE.unqiueID_override, 
       UNIONTABLE.objectID_paymntspayee, 
       UNIONTABLE.unqiueID_paymntspayee, 
       UNIONTABLE.objectID_paymentspay, 
       UNIONTABLE.unqiueID_paymentspay 
FROM   (SELECT DISTINCT DT.objectID, 
                    DT.UniqueID, 
                    DT.f, 
                    DT.updatedate, 
                    DT.version, 
                    DT.objectID_updatedby, 
                    DT.unqiueID_updatedby, 
                    DT.amount, 
                    DT.currency, 
                    DT.name, 
                    DT.number, 
                    DT.funds, 
                    DT.print, 
                    DT.paydate, 
                    DT.recover, 
                    DT.address, 
                    DT.payee, 
                    DT.objectID_nominated, 
                    DT.uniqueID_nominated, 
                    DT.objectID_override, 
                    DT.uniqueID_override, 
                    DT.objectID_paymntspayee, 
                    DT.uniqueID_paymntspayee, 
                    DT.objectID_paymentspay, 
                    DT.uniqueID_paymentspay 
    FROM   DetailsTable DT, 
           ObjectTable1 OT1, 
           OtherDetailsTable ODT 
    WHERE  DT.uniqueID = OT1.uniqueID_ForeignKey1 
           AND OT1.uniqueID_ForeignKey2 = ODT.uniqueiD 
           AND Upper(ODT.Name) = 'STOCK1' 
           ) UNIONTABLE 
ORDER  BY UNIONTABLE.objectID, 
          UNIONTABLE.uniqueID 

This would work correctly if there was only one ObjectTable. In order to get the correct results for the required model, I have the query

SELECT UNIONTABLE.objectID, 
       UNIONTABLE.unqiueID, 
       UNIONTABLE.f, 
       UNIONTABLE.updatedate, 
       UNIONTABLE.version, 
       UNIONTABLE.objectID_updatedby, 
       UNIONTABLE.unqiueID_updatedby, 
       UNIONTABLE.amount, 
       UNIONTABLE.currency, 
       UNIONTABLE.name, 
       UNIONTABLE.number, 
       UNIONTABLE.funds, 
       UNIONTABLE.print, 
       UNIONTABLE.paydate, 
       UNIONTABLE.recover, 
       UNIONTABLE.address, 
       UNIONTABLE.payee, 
       UNIONTABLE.objectID_nominated, 
       UNIONTABLE.unqiueID_nominated, 
       UNIONTABLE.objectID_override, 
       UNIONTABLE.unqiueID_override, 
       UNIONTABLE.objectID_paymntspayee, 
       UNIONTABLE.unqiueID_paymntspayee, 
       UNIONTABLE.objectID_paymentspay, 
       UNIONTABLE.unqiueID_paymentspay 
FROM   (SELECT DISTINCT DT.objectID, 
                    DT.UniqueID, 
                    DT.f, 
                    DT.updatedate, 
                    DT.version, 
                    DT.objectID_updatedby, 
                    DT.unqiueID_updatedby, 
                    DT.amount, 
                    DT.currency, 
                    DT.name, 
                    DT.number, 
                    DT.funds, 
                    DT.print, 
                    DT.paydate, 
                    DT.recover, 
                    DT.address, 
                    DT.payee, 
                    DT.objectID_nominated, 
                    DT.uniqueID_nominated, 
                    DT.objectID_override, 
                    DT.uniqueID_override, 
                    DT.objectID_paymntspayee, 
                    DT.uniqueID_paymntspayee, 
                    DT.objectID_paymentspay, 
                    DT.uniqueID_paymentspay 
    FROM   DetailsTable DT, 
           ObjectTable1 OT1, 
           OtherDetailsTable ODT 
    WHERE  DT.uniqueID = OT1.uniqueID_ForeignKey1 
           AND OT1.uniqueID_ForeignKey2 = ODT.uniqueiD 
           AND Upper(ODT.Name) = 'STOCK1' 
           ) UNIONTABLE 
    UNION
SELECT UNIONTABLE.objectID, 
       UNIONTABLE.unqiueID, 
       UNIONTABLE.f, 
       UNIONTABLE.updatedate, 
       UNIONTABLE.version, 
       UNIONTABLE.objectID_updatedby, 
       UNIONTABLE.unqiueID_updatedby, 
       UNIONTABLE.amount, 
       UNIONTABLE.currency, 
       UNIONTABLE.name, 
       UNIONTABLE.number, 
       UNIONTABLE.funds, 
       UNIONTABLE.print, 
       UNIONTABLE.paydate, 
       UNIONTABLE.recover, 
       UNIONTABLE.address, 
       UNIONTABLE.payee, 
       UNIONTABLE.objectID_nominated, 
       UNIONTABLE.unqiueID_nominated, 
       UNIONTABLE.objectID_override, 
       UNIONTABLE.unqiueID_override, 
       UNIONTABLE.objectID_paymntspayee, 
       UNIONTABLE.unqiueID_paymntspayee, 
       UNIONTABLE.objectID_paymentspay, 
       UNIONTABLE.unqiueID_paymentspay 
FROM   (SELECT DISTINCT DT.objectID, 
                    DT.UniqueID, 
                    DT.f, 
                    DT.updatedate, 
                    DT.version, 
                    DT.objectID_updatedby, 
                    DT.unqiueID_updatedby, 
                    DT.amount, 
                    DT.currency, 
                    DT.name, 
                    DT.number, 
                    DT.funds, 
                    DT.print, 
                    DT.paydate, 
                    DT.recover, 
                    DT.address, 
                    DT.payee, 
                    DT.objectID_nominated, 
                    DT.uniqueID_nominated, 
                    DT.objectID_override, 
                    DT.uniqueID_override, 
                    DT.objectID_paymntspayee, 
                    DT.uniqueID_paymntspayee, 
                    DT.objectID_paymentspay, 
                    DT.uniqueID_paymentspay 
    FROM   DetailsTable DT, 
           ObjectTable1 OT2, 
           OtherDetailsTable ODT 
    WHERE  DT.uniqueID = OT2.uniqueID_ForeignKey1 
           AND OT2.uniqueID_ForeignKey2 = ODT.uniqueiD 
           AND Upper(ODT.Name) = 'STOCK1' 
           ) UNIONTABLE 
    Order by objectID, uniqueID

This will get the right number of results, however, I really need to simplify this as doing this through the current code generators will be... tricky.

Also, I had to change the orderby from

ORDER  BY UNIONTABLE.objectID, 
          UNIONTABLE.uniqueID 

To

Order by objectID, uniqueID

because it was giving out.

So yea, is there an easier way to do this? Its using Oracle

Thanks.

4

1 回答 1

1

有没有更简单的方法来做到这一点?

好吧,你可以ObjectTable1 OT1

(SELECT * FROM ObjectTable1 UNION ALL
 SELECT * FROM ObjectTable2) OT1

它“容易”,但我不知道你失去了多少性能。

于 2013-10-31T14:04:01.623 回答