Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 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

question from:https://stackoverflow.com/questions/65603095/returning-rows-with-where-and-or-clause-not-returning-correct-values

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...