我需要在SalesOrderLine上拥有SalesOrderLine.Shipped<>的资产数量。 ' C&#39 ;.如果SalesOrderLine中没有行,或者SalesExrderLine.Shipped =' C'中的所有行,则以下SQL查询不会检索任何行。 你能告诉我在哪里错了吗?
更具体一点:
如果我删除
AND SalesOrderLine.Shipped <> ''C''
从WHERE部分,它检索一些行。当然,这不是我需要的。
以下是一个包含140个资产的SO的示例,SalesOrderLine.Shipped = 'C'
:
Order No Customer Name Customer PO Asset Count SO Status Released Raised Date Order Type Address Deliveries (Connotes)
O30540 ITPG Group IAL4445, IAL3062 140 Cancelled NO 11/01/2015 Sold NYIAL5554, ITPL6032 NULL
以下是查询:
('SELECT SalesOrderHeader.OrderNo AS [Order No], SalesOrderHeader.CustomerName AS [Customer Name], SalesOrderHeader.Reference1 AS [Customer PO], COUNT(SalesOrderLine.Barcode) AS [Asset Count],
LookUpData.CategoryLink AS [SO Status], (CASE WHEN isReleased = ''1'' THEN ''YES'' ELSE ''NO'' END) AS Released, SalesOrderHeader.RaisedDate AS [Raised Date], SalesOrderHeader.Ordertype AS [Order Type],
SalesOrderHeader.DeliverySuburb + SalesOrderHeader.DeliveryAddress1 AS [Address], (CASE WHEN LEN(pre_trimmed.DeliveryNo) < 1 THEN pre_trimmed.DeliveryNo ELSE LEFT(pre_trimmed.DeliveryNo , LEN(pre_trimmed.DeliveryNo)-1) END) AS [Deliveries (Connotes)]
FROM SalesOrderHeader
INNER JOIN LookUpData ON SalesOrderHeader.Status = LookUpData.CategoryName
INNER JOIN SalesOrderLine ON SalesOrderHeader.OrderNo = SalesOrderLine.OrderNo
LEFT JOIN DeliveryHeader ON SalesOrderHeader.OrderNo = DeliveryHeader.OrderNo
CROSS APPLY
(
SELECT DeliveryNo + '' ('' + Connote + ''), ''
FROM DeliveryHeader AS intern
WHERE DeliveryHeader.OrderNo = intern.OrderNo
FOR XML PATH('''')
) pre_trimmed (DeliveryNo)
WHERE
' + @FieldName + ' Like ''%' + @FieldValue + '%''
AND (LookUpData.Category = ''SOStatus'' AND SalesOrderLine.Shipped <> ''C'')
GROUP BY SalesOrderHeader.OrderNo , SalesOrderHeader.CustomerName, SalesOrderHeader.Reference1 ,
LookUpData.CategoryLink, SalesOrderHeader.RaisedDate, SalesOrderHeader.Ordertype,
SalesOrderHeader.DeliverySuburb, SalesOrderHeader.DeliveryAddress1, pre_trimmed.DeliveryNo, SalesOrderHeader.isReleased
ORDER BY SalesOrderHeader.CustomerName, SalesOrderHeader.OrderNo')
答案 0 :(得分:0)
我同意史莱克和佐哈尔你应该加入更多内容 信息。它会鼓励有人帮助你。
随着说。在您花费太多时间之前,这是一个简单的尝试。 替换您怀疑的行
AND SalesOrderLine.Shipped <> ''C''
以下行
AND UPPER(LTRIM(RTRIM(SalesOrderLine.Shipped))) <> ''C''
以下是您方便参考的说明。
Shipped
表的SalesOrderLine
列的数据类型设为VARCHAR
而不是NVARCHAR
。所以我尝试LTRIM
和RTRIM
来摆脱周围的任何空间。 (PS - TRIM
如果适用于您的版本,或者只是RTRIM
可以完成此任务) @mike - 看看这是否有帮助。