0
    select
        b.security_type,
        b.symbol,
        b.security_description,
        b.trade_date_qty as 'axys_qty',
        c.trade_date_qty as 'fidelity_qty',
        c.trade_date_qty - b.trade_date_qty as 'qty_diff',
        b.cost_basis as 'axys_cost',
        c.cost_basis as 'fidelity_cost',
        c.cost_basis - b.cost_basis as 'cost_diff'
    from
        account a
        inner join advent_position b on a.fixed_account_number = b.account_number
        inner join fidelity_position c on a.fixed_account_number = c.account_number and b.symbol = c.symbol
    where
        b.account_number = '636296651'

基本上,我有ff。域:帐户、AdventPosition、FidelityPosition。我还没有设置关系。我只是想知道是否有一种方法可以使用 Criteria 或 HQL 来复制上述逻辑。原谅我,我还是 Grails 的新手。

感谢您对此提供任何线索。

4

2 回答 2

0

这将是接近这个的东西:

String hql = '''
select
   b.securityType,
   b.symbol,
   b.securityDescription,
   b.tradeDateQty,
   c.tradeDateQty,
   c.tradeDateQty - b.tradeDateQty,
   b.costBasis,
   c.costBasis,
   c.costBasis - b.costBasis
from
   Account a, AdventPosition b, FidelityPosition c
where
   a.fixedAccountNumber = b.accountNumber
   and a.fixedAccountNumber = c.accountNumber
   and b.symbol = c.symbol
   and b.accountNumber = :accountNumber
'''

def accountNumber = '636296651'
def results = Account.executeQuery(hql, [accountNumber: accountNumber])

结果将是 Object[] 的 ArrayList,因此您可以使用类似的内容对其进行迭代

for (row in results) {
   def securityType = row[0]
   def symbol = row[1]
   def securityDescription = row[2]
   def axys_qty = row[3]
   def fidelity_qty = row[4]
   def qty_diff = row[5]
   def axys_cost = row[6]
   def fidelity_cost = row[7]
   def cost_diff = row[8]
}

我用命名参数替换了硬编码的帐号;你可以用普通的吗?就像在 SQL 中一样,如果您喜欢并运行“def results = Account.executeQuery(hql, [accountNumber])”,当然如果您打算对其进行硬编码,则恢复它并且不要传入第二个参数,只需运行'def results = Account.executeQuery(hql)'

于 2010-02-15T07:17:03.703 回答
0

只是分享我提出的解决方案(在等待答案时:P),但请注意,之前的答案更好更快:

def acc = Account.findByFixedAccountNumber('636296651')
List advPos = AdventPosition.findAllByAccountNumber('636296651')
List fidPos = advPos.collect {
    FidelityPosition.findAllByAccountNumberAndSymbol('636296651', it.symbol)
}

def item = [:]
def res = []

def limit = advPos.size() - 1
for(i in 0..limit){
    item.security_type = advPos[i].securityType
    item.symbol = advPos[i].symbol
    item.security_description = advPos[i].securityDescription
    item.axys_qty = advPos[i].tradeDateQty
    item.fidelity_qty = fidPos[i].tradeDateQty
    item.qty_diff = item.fidelity_qty - item.axys_qty
    item.axys_cost = advPos[i].costBasis
    item.fidelity_cost = fidPos[i].costBasis
    item.cost_diff = item.fidelity_cost - item.axys_cost 
    res.add(item)
}
于 2010-02-15T07:35:06.923 回答