SQL加入条件

时间:2016-09-06 11:23:38

标签: sql if-statement where sql-server-2016

我正在尝试编写一个查询,如果该特定事务满足某个特定要求(在这种情况下它是总数量!= 0),它将对事务进行一些计算。

IF EXISTS(
   SELECT DISTINCT
        T0.[ItemCode],
        T0.[ItemName],
        T0.[DocDate],
        CAST(CAST(T0.[CreateTime] / 100 AS varchar) + ':' + CAST(T0.[CreateTime] % 100 AS varchar) AS time) AS [CreateTime],
        T0.[DocType],
        T0.[DocEntry],
        T0.[DocNum],
        T0.[LocCode],
        T1.[MdAbsEntry],
        T4.[AbsEntry],
        T4.[DistNumber] AS [BatchNumber],
        T1.[Quantity],
        T5.[CalcPrice] AS [Price],
        T3.[U_ORC_BE_Variance]


    FROM OITL T0 
        INNER JOIN ITL1 T1 ON T1.[LogEntry] = T0.[LogEntry]
        INNER JOIN OITM T2 ON T2.[ItemCode] = T0.[ItemCode]
        INNER JOIN IGE1 T3 ON T3.[DocEntry] = T0.[DocEntry]
        INNER JOIN OBTN T4 ON T1.[MdAbsEntry] = T4.[AbsEntry]
        INNER JOIN OINM T5 ON T0.[ItemCode] = T5.[ItemCode] 
            AND T0.[LocCode] = T5.[Warehouse]  
            AND T0.[AppDocNum] = T5.[BASE_REF]

    WHERE T0.[ItemCode] LIKE 'BB%' -- BBT items only
        AND T3.[U_ORC_BE_Variance] = 'M3'-- Code issued upon BBT empty
        AND CAST(CAST(T0.[CreateTime] / 100 AS varchar) + ':' + CAST(T0.[CreateTime] % 100 AS varchar) AS time)
            BETWEEN CAST(DATEADD(MINUTE,-11,GETDATE())AS TIME) AND CONVERT(varchar(10), GETDATE(), 108) -- Last 10 minutes
        AND T0.[DocDate] = CAST(LEFT(GETDATE(), 11) AS DATETIME) -- Today
   )

BEGIN

SELECT DISTINCT
       'BBT ALERT - ' + convert(varchar(10), GETDATE(), 108) + ' - Check for errors',
        'Difference HL:',
       SUM(T1.[Quantity]),
       'Percentage Difference:',
       ((((SUM(ABS(T1.[Quantity])) - MAX(T1.[Quantity])) / MAX(T1.[Quantity])) * 100) - 100)

FROM OITL T0 
    INNER JOIN ITL1 T1 ON T1.[LogEntry] = T0.[LogEntry]  
    INNER JOIN OITM T2 ON T2.[ItemCode] = T0.[ItemCode]
    INNER JOIN IGE1 T3 ON T3.[DocEntry] = T0.[DocEntry]
    INNER JOIN OBTN T4 ON T1.[MdAbsEntry] = T4.[AbsEntry]    
    INNER JOIN OINM T5 ON T0.[ItemCode] = T5.[ItemCode] 
        AND T0.[LocCode] = T5.[Warehouse] 
        AND T0.[AppDocNum] = T5.[BASE_REF] 

WHERE
        (SELECT SUM([Quantity]) FROM ITL1 WHERE
                (SELECT DISTINCT
                    T4.[AbsEntry]
                 FROM OITL T0 
                    INNER JOIN ITL1 T1 ON T1.[LogEntry] = T0.[LogEntry]  
                    INNER JOIN OITM T2 ON T2.[ItemCode] = T0.[ItemCode]
                    INNER JOIN IGE1 T3 ON T3.[DocEntry] = T0.[DocEntry]
                    INNER JOIN OBTN T4 ON T1.[MdAbsEntry] = T4.[AbsEntry]    
                    INNER JOIN OINM T5 ON T0.[ItemCode] = T5.[ItemCode] 
                        AND T0.[LocCode] = T5.[Warehouse] 
                        AND T0.[AppDocNum] = T5.[BASE_REF] 

                WHERE T0.[ItemCode] LIKE 'BB%' -- BBT items only
                        AND T3.[U_ORC_BE_Variance] = 'M3'-- Code issued upon BBT empty
                        AND CAST(CAST(T0.[CreateTime] / 100 AS varchar) + ':' + CAST(T0.[CreateTime] % 100 AS varchar) AS time)
                            BETWEEN CAST(DATEADD(MINUTE,-11,GETDATE())AS TIME) AND CONVERT(varchar(10), GETDATE(), 108) -- Last 10 minutes
                                AND T0.[DocDate] = CAST(LEFT(GETDATE(), 11) AS DATETIME) -- Today
                ) = [MdAbsEntry]
            ) <> 0

SELECT DISTINCT
       T1.[MdAbsEntry],
       T0.[DocNum],
       T1.[Quantity]

FROM OITL T0 
    INNER JOIN ITL1 T1 ON T1.[LogEntry] = T0.[LogEntry]  
    INNER JOIN OITM T2 ON T2.[ItemCode] = T0.[ItemCode]
    INNER JOIN IGE1 T3 ON T3.[DocEntry] = T0.[DocEntry]
    INNER JOIN OBTN T4 ON T1.[MdAbsEntry] = T4.[AbsEntry]    
    INNER JOIN OINM T5 ON T0.[ItemCode] = T5.[ItemCode] AND T0.[LocCode] = T5.[Warehouse] 
        AND T0.[AppDocNum] = T5.[BASE_REF] 

WHERE
        (SELECT SUM([Quantity]) FROM ITL1 WHERE
                (SELECT DISTINCT
                    T4.[AbsEntry]
                 FROM OITL T0 
                    INNER JOIN ITL1 T1 ON T1.[LogEntry] = T0.[LogEntry]  
                    INNER JOIN OITM T2 ON T2.[ItemCode] = T0.[ItemCode]
                    INNER JOIN IGE1 T3 ON T3.[DocEntry] = T0.[DocEntry]
                    INNER JOIN OBTN T4 ON T1.[MdAbsEntry] = T4.[AbsEntry]    
                    INNER JOIN OINM T5 ON T0.[ItemCode] = T5.[ItemCode] 
                        AND T0.[LocCode] = T5.[Warehouse] 
                        AND T0.[AppDocNum] = T5.[BASE_REF] 

                  WHERE T0.[ItemCode] LIKE 'BB%' -- BBT items only
                        AND T3.[U_ORC_BE_Variance] = 'M3'-- Code issued upon BBT empty
                        AND CAST(CAST(T0.[CreateTime] / 100 AS varchar) + ':' + CAST(T0.[CreateTime] % 100 AS varchar) AS time)
                            BETWEEN CAST(DATEADD(MINUTE,-11,GETDATE())AS TIME) AND CONVERT(varchar(10), GETDATE(), 108) -- Last 10 minutes
                                AND T0.[DocDate] = CAST(LEFT(GETDATE(), 11) AS DATETIME) -- Today
                ) = [MdAbsEntry]
            ) <> 0

END

如果我单独运行第一部分,我会得到以下内容(这是正确的): previous transaction

但是,如果我完整运行查询,我会得到以下内容: query result

我希望它只能在MdAbsEntry中列出一个值的结果(在本例中为5618)。

在我看来,它不像我想要的那样匹配MdAbsEntry值,因此拉取所有数据的结果,而不仅仅是针对这一个特定事务。

有人可以给我任何关于它为什么这样做的建议吗?

0 个答案:

没有答案