2

我在SQL Server 2008 R2Microsoft Dynamics AX ERP数据库中有一个,它有一些行,表中有一个名为 qty 的字段,一些行的数量为负数,一些行的数量为正数。还有另一个字段 TransactionID,现在我想选择所有那些具有负数量的 TransactionID,以及那些具有正 QTy 和负数量的行的 TransactionID。

TransactionID 是主表的外键字段。

到目前为止,我在下面编写了不起作用的代码。

select * from RBOTRANSACTIONSALESTRANS main where main.qty < 0 
                                               and main.DATAAREAID = 'DAT'
and exists 
(
select 1 from RBOTRANSACTIONSALESTRANS where QTY > 0 
and RBOTRANSACTIONSALESTRANS.DATAAREAID = main.DATAAREAID
and RBOTRANSACTIONSALESTRANS.STORE = main.STORE 
and RBOTRANSACTIONSALESTRANS.TERMINALID = main.TERMINALID 
)

order by main.TRANSACTIONID
4

4 回答 4

2

回顾一下,您要选择至少存在一个具有相同 TransactionID 且数量为负数的行的所有行?

select * 
from RBOTRANSACTIONSALESTRANS main 
where main.DATAAREAID = 'DAT'
 and exists 
  (
    select 1 
    from RBOTRANSACTIONSALESTRANS sub 
    where sub.QTY < 0 
      and sub.DATAAREAID = main.DATAAREAID
      and sub.STORE = main.STORE 
      and sub.TERMINALID = main.TERMINALID
      and sub.TRANSACTIONID = main.TRANSACTIONID
  )
order by main.TRANSACTIONID

主要区别是,你有一张main.QTY < 0支票。我补充说sub.TRANSACTIONID = main.TRANSACTIONID

于 2013-01-10T10:55:40.503 回答
1

或多或少的查询应该是这样的

select * from 
(
select tansactionID, sum(qty) as q, sum(abs(qty)) as q2
from table2 T
group by tansactionID ) T
where q<> q2

发布架构,以便我可以调整它

于 2013-01-10T10:56:01.690 回答
1

JOINed 查询TRANSACTIONID根据您的条件定义(请参阅HAVING条件)。所以在 JOIN 之后你过滤你的表只留下这些TRANSACTIONID

select t.* from RBOTRANSACTIONSALESTRANS t
join
(
select TRANSACTIONID,min(main.qty),max(main.qty) 
    from RBOTRANSACTIONSALESTRANS main 
GROUP BY TRANSACTIONID
HAVING (max(main.qty) < 0) 
         or 
       ((min(main.qty) < 0) and (max(main.qty) > 0))
) t1 on (t.TRANSACTIONID=t1.TRANSACTIONID)

UPD在您对问题发表评论后,我认为您只需要在内部查询中使用此条件:

HAVING not (min(main.qty) < 0)            
于 2013-01-10T11:09:21.587 回答
1

在我看来,您可能使问题复杂化了。如果我正确理解标准,您希望所有行

  • 数量 < 0

或者

  • 数量 < 0并且存在具有相同交易 ID 且数量为负的行

所以开始

SELECT  *
FROM    RBOTRANSACTIONSALESTRANS tr
WHERE   tr.DATAAREAID = 'DAT'
AND     tr.Qty < 0

将满足第一个标准。以下将做第二个。

SELECT  *
FROM    RBOTRANSACTIONSALESTRANS tr
WHERE   tr.DATAAREAID = 'DAT'
AND     tr.Qty > 0
AND     EXISTS
        (   SELECT  1
            FROM    RBOTRANSACTIONSALESTRANS neg
            WHERE   neg.TransactionID = tr.TransactionID
            AND     neg.DATAAREAID = 'DAT'
            AND     neg.Quantity < 0
        )

这可以组合为

SELECT  *
FROM    RBOTRANSACTIONSALESTRANS tr
WHERE   tr.DATAAREAID = 'DAT'
AND (   tr.Qty < 0
    OR  EXISTS
        (   SELECT  1
            FROM    RBOTRANSACTIONSALESTRANS neg
            WHERE   neg.TransactionID = tr.TransactionID
            AND     neg.DATAAREAID = 'DAT'
            AND     neg.Quantity < 0
        )
    )

< 0但是,第一个子句是多余的,因为该子句也将满足数量所在的任何行EXISTS。所以这可以简化为:

SELECT  *
FROM    RBOTRANSACTIONSALESTRANS tr
WHERE   tr.DATAAREAID = 'DAT'
AND     EXISTS
        (   SELECT  1
            FROM    RBOTRANSACTIONSALESTRANS neg
            WHERE   neg.TransactionID = tr.TransactionID
            AND     neg.DATAAREAID = 'DAT'
            AND     neg.Quantity < 0
        )
于 2013-01-10T11:23:37.720 回答