sql - 返回带有 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 子句的方式吗?(见下面的数据)
解决方案
推荐阅读
- reactjs - 如何在 Gatsby 中将 js 添加到 React 组件中?
- bixby - 如何使用模拟器在 Bixby Studio SDK 上编译代码?
- c - fgets 在 Linux 和 Windows 中从同一个文件中写入不同的字符串
- scala - 除了 Apache Kafka 之外,还有哪些事件溯源替代方案?
- python - 按法线旋转 vtkDiskSource/vtkPolyDataMapper
- firebase - 如何限制用户可以在 Firebase 中发送的查询数量
- r - 使用对不同数据框中的另一个变量的条件创建一个新变量
- ios - 对 JSON 进行排序以查找字符串不同的每个实例
- html - 如何用 bulma 使部分居中
- r - 使用 purrr 的地图时出错,可能