Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

sql - Returning rows with where and or clause not returning correct values

I have the following query, which is supposed to return [issued date] and [Date Made] according to the same where and or criteria.

In other words if I have a date of 04/01/2020 for [Date Made] and a date of 04/01/2020 for [Issued Date], the data returned should be 04/01/2020 for all records. Currently the [Date Made] is correct, but the [Date Issued] is returning additional dates.

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;

I am thinking it is the way I have implemented the or clause ? (See data below)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...