0

我知道这个错误

DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=((select * from ( select rownumber() over(;BEGIN-OF-STATEMENT;<delete>, DRIVER=3.50.152

当我运行以下查询时:

((SELECT ORGANIZATION.ORGNAME AS ORGNAME
        ,SUBSCRIPTION.ID AS SUBSCRIPTIONID
        ,SUBSCRIPTION.NUMBEROFAVAILABLESEATS AS AVAILABLESEATS
    FROM SUBSCRIPTION SUBSCRIPTION
        ,ORGANIZATION ORGANIZATION
        ,CUSTOMER CUSTOMER
        ,CUSTOMERACCOUNT CUSTOMERACCOUNT
    WHERE ORGANIZATION.ISDELETED=0
      AND SUBSCRIPTION.DELETED=0
      AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID
      AND CUSTOMER.ISDELETED=0
      AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID
      AND CUSTOMER.PARTYID=ORGANIZATION.ID
      AND ISLIMITEDSEAT=1 
UNION ALL 
  SELECT ORGANIZATION.ORGNAME
        ,SUBSCRIPTION.ID AS SUBSCRIPTIONID
        ,-1 AS AVAILABLESEATS
    FROM SUBSCRIPTION SUBSCRIPTION
        ,ORGANIZATION ORGANIZATION
        ,CUSTOMER CUSTOMER
        ,CUSTOMERACCOUNT CUSTOMERACCOUNT
    WHERE ORGANIZATION.ISDELETED=0
      AND SUBSCRIPTION.DELETED=0
      AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID
      AND CUSTOMER.ISDELETED=0
      AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID
      AND CUSTOMER.PARTYID=ORGANIZATION.ID
      AND ISLIMITEDSEAT=0
 ) as results
  order by ORGNAME
) with ur

Hibrnate 解码查询:

"((select * from ( select rownumber() over(order by ORGNAME)with ur) as rownumber_, ORGANIZATION.ORGNAME AS ORGNAME,SUBSCRIPTION.ID AS SUBSCRIPTIONID,SUBSCRIPTION.NUMBEROFAVAILABLESEATS AS AVAILABLESEATS FROM SUBSCRIPTION SUBSCRIPTION,ORGANIZATION ORGANIZATION,CUSTOMER CUSTOMER,CUSTOMERACCOUNT CUSTOMERACCOUNT WHERE ORGANIZATION.ISDELETED=0 AND SUBSCRIPTION.DELETED=0 AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID AND CUSTOMER.ISDELETED=0 AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID AND CUSTOMER.PARTYID=ORGANIZATION.ID AND ISLIMITEDSEAT=1 UNION ALL SELECT ORGANIZATION.ORGNAME,SUBSCRIPTION.ID AS SUBSCRIPTIONID,-1 AS AVAILABLESEATS FROM SUBSCRIPTION SUBSCRIPTION,ORGANIZATION ORGANIZATION,CUSTOMER CUSTOMER,CUSTOMERACCOUNT CUSTOMERACCOUNT WHERE ORGANIZATION.ISDELETED=0 AND SUBSCRIPTION.DELETED=0 AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID AND  CUSTOMER.ISDELETED=0 AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID AND CUSTOMER.PARTYID= ORGANIZATION.ID AND ISLIMITEDSEAT=0) results order by ORGNAME)with ur ) as temp_ where rownumber_ <= ?"
4

3 回答 3

0

Your original problem may have been due to the placement of the parentheses, but it became difficult to unravel due to the length and complexity of what was going on.

I'll put several suggestions together, giving you a much shorter query, which should also be significantly faster, and easier for the next person to understand.

Let's look at what's going on. You've got two nearly identical SELECTs being glued together. Suppose we mesh them back into a single SELECT.

A little bit of logic, in the form of a CASE expression will be a lot less work overall than two SELECTS that get merged and sorted back together.

Unless you are creating an abbreviation, there's no point repeating every table name:

  FROM SUBSCRIPTION SUBSCRIPTION,

It is often deemed clearer to the next person if you specify the join conditions in the FROM clause, rather than the WHERE clause.

SELECT ORGANIZATION.ORGNAME,
       SUBSCRIPTION.ID         AS SUBSCRIPTIONID,
       CASE WHEN SUBSCRIPTION.ISLIMITEDSEAT = 1
            THEN SUBSCRIPTION.NUMBEROFAVAILABLESEATS
            ELSE -1 
        END                    AS AVAILABLESEATS
  FROM ORGANIZATION
  JOIN CUSTOMER
    ON  CUSTOMER.PARTYID = ORGANIZATION.ID
  JOIN CUSTOMERACCOUNT
    ON  CUSTOMERACCOUNT.CUSTOMERID = CUSTOMER.ID
  JOIN SUBSCRIPTION
    AND SUBSCRIPTION.CUSTOMERACCOUNTID = CUSTOMERACCOUNT.ID
  WHERE ORGANIZATION.ISDELETED=0
    AND SUBSCRIPTION.DELETED=0
    AND CUSTOMER.ISDELETED=0
    AND SUBSCRIPTION.ISLIMITEDSEAT IN (0,1) 

If SUBSCRIPTION.ISLIMITEDSEAT is a flag that can only be 0 or 1, then drop that last line.

于 2013-10-23T00:02:24.500 回答
0
the following thing worked for me :

SELECT RESULTS.ORGNAME,
       RESULTS.SUBSCRIPTIONID,
       RESULTS.AVAILABLESEATS FROM(
                                     (SELECT ORGANIZATION.ORGNAME ,SUBSCRIPTION.ID AS SUBSCRIPTIONID,-1 AS AVAILABLESEATS
                                      FROM SUBSCRIPTION SUBSCRIPTION,ORG` v`ANIZATION ORGANIZATION,CUSTOMER CUSTOMER,CUSTOMERACCOUNT CUSTOMERACCOUNT
                                      WHERE ORGANIZATION.ISDELETED=0`enter code here`
                                        AND SUBSCRIPTION.DELETED=0
                                        AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID
                                        AND CUSTOMER.ISDELETED=0
                                        AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID
                                        AND CUSTOMER.PARTYID=ORGANIZATION.ID
                                        AND SUBSCRIPTION.ISLIMITEDSEAT=0)
                                   UNION ALL
                                     (SELECT ORGANIZATION.ORGNAME ,SUBSCRIPTION.ID AS SUBSCRIPTIONID, SUBSCRIPTION.NUMBEROFAVAILABLESEATS AS AVAILABLESEATS
                                      FROM SUBSCRIPTION SUBSCRIPTION,ORGANIZATION ORGANIZATION,CUSTOMER CUSTOMER,CUSTOMERACCOUNT CUSTOMERACCOUNT
                                      WHERE ORGANIZATION.ISDELETED=0
                                        AND SUBSCRIPTION.DELETED=0
                                        AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID
                                        AND CUSTOMER.ISDELETED=0
                                        AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID
                                        AND CUSTOMER.PARTYID=ORGANIZATION.ID
                                        AND SUBSCRIPTION.ISLIMITEDSEAT=1))AS RESULTS
于 2013-10-22T05:35:29.223 回答
0

我稍微格式化了查询。我不确定语法是否正确。order by看起来有点孤独(没有匹配的选择),我不确定应with该做什么。

((SELECT ORGANIZATION.ORGNAME AS ORGNAME, 
    SUBSCRIPTION.ID AS SUBSCRIPTIONID,
    SUBSCRIPTION.NUMBEROFAVAILABLESEATS AS AVAILABLESEATS 
  FROM SUBSCRIPTION SUBSCRIPTION,
       ORGANIZATION ORGANIZATION,
       CUSTOMER CUSTOMER,
       CUSTOMERACCOUNT CUSTOMERACCOUNT 
  WHERE ORGANIZATION.ISDELETED=0 AND SUBSCRIPTION.DELETED=0 
    AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID 
    AND CUSTOMER.ISDELETED=0 
    AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID
    AND CUSTOMER.PARTYID=ORGANIZATION.ID 
    AND ISLIMITEDSEAT=1 
UNION ALL 
  SELECT ORGANIZATION.ORGNAME,
         SUBSCRIPTION.ID AS SUBSCRIPTIONID,
         -1 AS AVAILABLESEATS 
  FROM SUBSCRIPTION SUBSCRIPTION,
       ORGANIZATION ORGANIZATION,
       CUSTOMER CUSTOMER,
       CUSTOMERACCOUNT CUSTOMERACCOUNT 
  WHERE ORGANIZATION.ISDELETED=0 
    AND SUBSCRIPTION.DELETED=0 
    AND CUSTOMERACCOUNT.ID=SUBSCRIPTION.CUSTOMERACCOUNTID 
    AND  CUSTOMER.ISDELETED=0 
    AND CUSTOMERACCOUNT.CUSTOMERID=CUSTOMER.ID 
    AND CUSTOMER.PARTYID=ORGANIZATION.ID 
    AND ISLIMITEDSEAT=0
 ) results order by ORGNAME
)with ur
于 2013-10-21T15:16:32.500 回答