首页 > 解决方案 > 返回带有 where 和 or 子句的行没有返回正确的值

问题描述

我有以下查询,它应该根据相同的 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 子句的方式吗?(见下面的数据)

在此处输入图像描述

标签: sql

解决方案


推荐阅读