选择具有特定逻辑的语句

时间:2013-01-10 10:47:44

标签: sql sql-server-2008 tsql

我在 Microsoft Dynamics AX ERP SQL Server 2008 R2 数据库中有一个,其中有一些行,有一个名为的字段表中的数量,某些行具有负数量,而某些行具有正数量。还有另一个字段TransactionID,现在我想选择所有具有负数量的TransactionID,以及那些具有正QTys和负qtys的行的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 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分: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)            

答案 3 :(得分:1)

在我看来,你可能会使问题复杂化。如果我正确理解标准,您需要所有行

  • 数量<&lt; 0

  • 数量<&lt; 0 AND 存在具有相同事务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
        )