值太多错误

时间:2014-01-19 14:35:21

标签: sql oracle

SELECT t.trade_id, t.share_holder_id, t.broker_id
  FROM trades t
 INNER JOIN share_holders sh ON t.share_holder_id = sh.share_holder_id
 INNER JOIN share_holder_shares shs ON shs.share_holder_id = sh.share_holder_id
 WHERE (t.share_amount > 5000 AND shs.amount < 0 OR t.transaction_type = 2)
 GROUP BY t.trade_id, t.share_holder_id, t.broker_id
HAVING t.share_amount = (SELECT
                            SUM(DECODE(transaction_type, 1, share_amount)),
                            SUM(DECODE(transaction_type, 2, 0 - share_amount))
                           FROM trades t1
                          WHERE t.share_id = t1.share_id
                            AND t.share_holder_id = t1.share_holder_id
                            AND t.transaction_time < t1.transaction_time
                          GROUP BY t.share_amount)
ORDER BY t.trade_id, t.share_holder_id, t.broker_id;

我想写一个查询,显示所有股东每笔交易买入或卖出超过50,000股或出售他们在出售时没有的股票。我从子查询中得到太多的值错误。我如何解决他的问题?

2 个答案:

答案 0 :(得分:0)

您正从子查询返回2个值。确保子查询仅返回1个值。它现在的方式与写作相似:

having t.share_amount = 50000 , -100

这没有任何意义

答案 1 :(得分:0)

SELECT
t.trade_id,
t.share_holder_id,
t.broker_id
 FROM
trades t
INNER JOIN
share_holders sh
ON
t.share_holder_id = sh.share_holder_id
     INNER JOIN
    share_holder_shares shs
  ON
 shs.share_holder_id = sh.share_holder_id
WHERE
  (t.share_amount >5000 AND shs.amount <0 OR t.transaction_type = 2) 
GROUP BY
 t.trade_id,
 t.share_holder_id,
  t.broker_id
  having
  t.share_amount = 
 (
  SELECT
    SUM(DECODE(transaction_type,1,share_amount))
     FROM
      trades t1
       WHERE
     t.share_id = t1.share_id AND
      t.share_holder_id = t1.share_holder_id AND
        t.transaction_time < t1.transaction_time
     GROUP BY
     t.share_amount)
or t.share_amount =
(
  SELECT
    SUM(DECODE(transaction_type,2,0-share_amount))
     FROM
      trades t1
       WHERE
     t.share_id = t1.share_id AND
      t.share_holder_id = t1.share_holder_id AND
        t.transaction_time < t1.transaction_time
     GROUP BY
     t.share_amount)
 ORDER BY
 t.trade_id,
 t.share_holder_id,
 t.broker_id
  ;

SELECT t.trade_id, t.share_holder_id, t.broker_id FROM trades t INNER JOIN share_holders sh ON t.share_holder_id = sh.share_holder_id INNER JOIN share_holder_shares shs ON shs.share_holder_id = sh.share_holder_id WHERE (t.share_amount >5000 AND shs.amount <0 OR t.transaction_type = 2) GROUP BY t.trade_id, t.share_holder_id, t.broker_id having t.share_amount = ( SELECT SUM(DECODE(transaction_type,1,share_amount)) FROM trades t1 WHERE t.share_id = t1.share_id AND t.share_holder_id = t1.share_holder_id AND t.transaction_time < t1.transaction_time GROUP BY t.share_amount) or t.share_amount = ( SELECT SUM(DECODE(transaction_type,2,0-share_amount)) FROM trades t1 WHERE t.share_id = t1.share_id AND t.share_holder_id = t1.share_holder_id AND t.transaction_time < t1.transaction_time GROUP BY t.share_amount) ORDER BY t.trade_id, t.share_holder_id, t.broker_id ;