消除sql查询中的一些记录

时间:2018-03-17 17:34:05

标签: sql sql-server sql-server-2012

我从各种表中编写了以下查询:

SELECT DISTINCT 0 AS IID, 
       pd.IID AS PurchaseOrerDetailsId, 
       i.[Description] AS ITEM, 
       '' AS BatchNo, 
       s.[Description] AS Unit,
       '' AS MfgDt, 
       0 AS QtyReceived, 
       '' AS ExpiryDate, 
       '' AS PackSize, 
       0 AS QtyOrdered, 
       0 AS MRP, 
       0 AS PTR,
       0 AS PTS, 
       0 AS PurchaseRate, 
       0 AS CGST, 
       0 AS SGST, 
       0 AS IGST, 
       0 AS DiscPer, 
       0 AS DiscVal, 
       0 AS PurchaseValue, 
       0 AS CGSTAmt,
       0 AS SGSTAmt, 
       i.IID AS ItemId
FROM   PurchaseOrderDetails pd
LEFT JOIN StockDetails sd ON pd.ItemId = sd.ItemId
INNER JOIN Items i ON i.IID = pd.ItemId
INNER JOIN Strings s ON s.IID = i.Unit
WHERE pd.PurchaseOrderId = 1

我得到以下结果:

enter image description here

现在我要删除前两条记录(PurchaseOrerDetailsId为1和2)。 任何线索。

由于 帕塔

2 个答案:

答案 0 :(得分:1)

我怀疑它还有更多。但根据你提供的内容:

 and pd.IID > 2

您不能使用StockDetails sd。离开加入是一种浪费。

问题不需要所有这些硬编码值。

答案 1 :(得分:0)

您可以在CTE中使用row_number()作为[rn],之后您可以选择[rn]> 2。