使用 where 和 or 子句返回行未返回正确值

时间:2021-01-06 20:40:50

标签: sql

我有以下查询,它应该根据相同的 where 和 or 条件返回 [issued date] 和 [Date Made]。

换句话说,如果 [Date Made] 的日期为 04/01/2020,[Issued Date] 的日期为 04/01/2020,则返回的所有记录的数据应为 04/01/2020 .目前 [Date Made] 是正确的,但 [Date Issued] 正在返回其他日期。

DECLARE 
    @Compound VARCHAR(30) = 'All',
    @SkidID VARCHAR(100) = 'All'

DECLARE @SkidCount INT;
    
SELECT 
    Reprint AS 'Reprint',
    comp.ID,
    SkidID AS 'Skid ID',
    PartNumber AS 'Compound',
    Quantity AS 'Qty',
    CONVERT(VARCHAR(12), DateMade, 101) AS 'Date Made',
    WorkOrderNumber AS 'E1 Work Order Number',
    WorkOrderDesc AS 'Work Order Desc',
    IssuedDate AS 'Issued Date',
    (SELECT TOP 1 cn.Name
     FROM dbo.ClockNum cn
     WHERE cn.ClockNum = comp.PrintedByClockNumber) AS 'PrintedBy',
    PrintedDateTime AS 'DateTimePrinted',
    (SELECT TOP 1 cn.Name
     FROM dbo.ClockNum cn
     WHERE cn.ClockNum = comp.IssuedByClockNumber) AS 'IssuedBy',
    CurrentLocation AS 'Current Location',
    PreviousLocation AS 'Previous Location'
FROM 
    dbo.Components comp WITH (NOLOCK)
WHERE 
    DateMade >= '2021-01-04 00:00:00.000'
    AND DateMade <= '2021-01-04 23:59:59.000'
    OR comp.IssuedDate >= '2021-01-04 00:00:00.000'
    AND comp.IssuedDate <= '2021-01-04 23:59:59.000'
    AND (PartNumber = 'All' OR @Compound = 'All')
    AND (SkidID LIKE '%' + @SkidID + '%' OR @SkidID = 'All')
    AND (ReversedBy IS NULL AND ReversalDate IS NULL)
    AND (CurrentLocation = 'Jackson')
ORDER BY 
    [Date Made] DESC;

我认为这是我实施 or 子句的方式? (见下方数据)

enter image description here

0 个答案:

没有答案
相关问题