0

在对两个表进行联合之后,现在它忽略了我的 WHERE 语句的 @Badge 参数。代码如下。

SELECT     TOP (100) PERCENT ID_BADGE, LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) 
                      + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, DATE_TRX, CODE_TRX

FROM omi.DCUTRX_ZERO 

UNION ALL 

SELECT     TOP (100) PERCENT ID_BADGE, LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) 
                      + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, DATE_TRX, CODE_TRX
FROM omi.TAHIST_ZERO 

WHERE     (ID_BADGE = '     ' + @Badge)  AND (DATE_TRX BETWEEN @Date AND @Date2) AND (SEQ_REC IN ('0', '1000')) AND (CODE_TRX IN ('100', '101'))

任何帮助是极大的赞赏!

4

3 回答 3

3

如果您希望将 WHERE 应用于两者,则需要包装语句并将其应用于结果集,如下所示:

SELECT *
FROM (
    SELECT TOP (100) PERCENT ID_BADGE,
        LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime,
        DATE_TRX,
        CODE_TRX
    FROM omi.DCUTRX_ZERO 
    UNION ALL 
    SELECT TOP (100) PERCENT ID_BADGE,
        LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime,
        DATE_TRX,
        CODE_TRX
    FROM omi.TAHIST_ZERO
) q
WHERE (ID_BADGE = '     ' + @Badge)  AND
    (DATE_TRX BETWEEN @Date AND @Date2) AND
    (SEQ_REC IN ('0', '1000')) AND
    (CODE_TRX IN ('100', '101'))
于 2012-12-27T14:40:07.600 回答
2

如果要将 WHERE 子句应用于所有结果,请尝试以下操作:

select * from (
SELECT TOP (100) PERCENT 
    ID_BADGE, 
    LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, 
    DATE_TRX, 
    CODE_TRX
FROM omi.DCUTRX_ZERO 

UNION ALL 

SELECT TOP (100) PERCENT 
    ID_BADGE, 
    LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, 
    DATE_TRX, 
    CODE_TRX
FROM omi.TAHIST_ZERO 
) omi
WHERE (omi.ID_BADGE = '     ' + @Badge)  
    AND (omi.DATE_TRX BETWEEN @Date AND @Date2) 
    AND (omi.SEQ_REC IN ('0', '1000')) 
    AND (omi.CODE_TRX IN ('100', '101'))
于 2012-12-27T14:40:45.283 回答
1

WHERE 过滤器应用于最后一个 UNION SELECT ..

更好的 SQL 如下:

SELECT     TOP (100) PERCENT ID_BADGE, LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) 
+ ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, DATE_TRX, CODE_TRX
FROM omi.DCUTRX_ZERO 
WHERE     (ID_BADGE = '     ' + @Badge)  AND (DATE_TRX BETWEEN @Date AND @Date2) AND (SEQ_REC IN ('0', '1000')) AND (CODE_TRX IN ('100', '101'))

UNION ALL 

SELECT     TOP (100) PERCENT ID_BADGE, LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 2) 
                  + ':' + SUBSTRING(LEFT(RIGHT('000000' + CAST(TIME_TRX AS varchar), 6), 4), 3, 2) AS ClockTime, DATE_TRX, CODE_TRX
FROM omi.TAHIST_ZERO
WHERE     (ID_BADGE = '     ' + @Badge)  AND (DATE_TRX BETWEEN @Date AND @Date2) AND (SEQ_REC IN ('0', '1000')) AND (CODE_TRX IN ('100', '101'))
于 2012-12-27T15:27:27.220 回答