SQL在连接条件中选择子查询

时间:2016-06-13 10:00:59

标签: sql oracle

虽然我不是高级SQL程序员,但我正在尝试编写SQL select,我需要将子查询放入连接条件以便从Actor表中获取数据。所以我选择了:

SELECT JEI.account_number                                AS JEI_accountNumber,
  JE.branch_code                                         AS JE_branchCode,
  JE.journal_entry_status                                AS JEI_transStatus,
  JEI.trans_amount                                       AS JEI_transAmount,
  JEI.account_currency                                   AS JEI_accountCurrency,
  JEI.other_account_number                               AS JEI_otherAccountNumber,
  JEI.other_bank_number                                  AS JEI_otherBankNumber,
  nts_rep_cheque.getISOStringFromDate(JE.effective_date) AS JE_effectiveDate,
  JE.multiple_trans_id                                   AS JE_multipleTransID,
  A.user_logon_name                                      AS USER_ID,
  A.di_teller_id                                         AS A_DITellerID,
  nts_rep_cheque.getISOStringFromDate(JE.created_date)   AS JE_createdDate,
  JE.operation_id                                        AS JE_operationID,
  JEI.nts_di_trans_number                                AS JEI_transType
FROM nts_journal_entry JE
JOIN nts_journal_entry_item JEI
ON JEI.company_code      = JE.company_code
AND JEI.user_id          = JE.user_id
AND JEI.journal_date     = JE.journal_date
AND JEI.journal_entry_id = JE.journal_entry_id
JOIN actor A
ON A.ACCE_PROC_ID            = JE.COMPANY_CODE
AND A.actor_id               = JE.actor_id
WHERE JE.company_code        = '01'

但是我希望使用此选择从actor表中选择不同的行:

 (SELECT USER_ID
    FROM
      (SELECT USER_ID
      FROM NTS_JOURNAL_ENTRY
      WHERE MULTIPLE_TRANS_ID = JE.MULTIPLE_TRANS_ID
      ORDER BY journal_entry_id
      )
    WHERE rownum =1
    )

我把它放在之前的选择替换中:

AND A.actor_id = JE.actor_id

  AND A.actor_id  = (SELECT USER_ID
    FROM
      (SELECT USER_ID
      FROM NTS_JOURNAL_ENTRY
      WHERE MULTIPLE_TRANS_ID = JE.MULTIPLE_TRANS_ID
      ORDER BY journal_entry_id
      )
    WHERE rownum =1
    )

但是,似乎Oracle对嵌套查询有限制,因此我收到错误:ORA-00904: "JE"."MULTIPLE_TRANS_ID" invalid identifier.

是否有任何解决方案,如何修复该错误或以何种方式重写查询以获得相同的结果?感谢

1 个答案:

答案 0 :(得分:0)

如果您希望第一个用户使用此MULTIPLE_TRANS_ID,则可以使用FIRST_VALUE

NN = MLPClassifier(algorithm='l-bfgs', alpha=1e-05, hidden_layer_sizes=(10, 10,), 
            random_state=1, validation_fraction=0.0)

# For every iteration, shuffle data, train model, test model
for j in xrange(100):   
    # Shuffle data
    X_train, y_train, X_test, y_test = shuffle_data(biz_features, biz_labels)

    # Classify using NN
    NN_clone = clone(NN, safe = True)
    y_pred_NN = NN_clone.fit(X_train, y_train).predict(X_test)

    # Classify using SGD
    SGD = OneVsRestClassifier(
        SGDClassifier(
            random_state=0,
            loss = 'hinge',
            alpha = 0.00001,
            penalty = 'elasticnet')).fit(X_train, y_train)
    y_pred_SGD = SGD.predict(X_test)

    # Evaluate ..

如果您希望最后一位用户使用DESCLAST_VALUE

  AND A.actor_id  = 
      (SELECT DISTINCT FIRST_VALUE(USER_ID) OVER (PARTITION BY MULTIPLE_TRANS_ID ORDER BY journal_entry_id)
      FROM NTS_JOURNAL_ENTRY
      WHERE MULTIPLE_TRANS_ID = JE.MULTIPLE_TRANS_ID
      )