输入多个SKU的循环功能

时间:2019-06-12 18:08:30

标签: sql sql-server

我正在尝试从一些不同的表中查询大约150个SKU的列表中的信息。到目前为止,我已经尝试执行Select,Where和From语句,但不想继续执行OR语句150次。是否可以通过以下方式循环代码并获取信息:

SKU   ProductName  Introduction Date   Suggested Retail Price   Units Sold  
1234    Cream             6/12/2019               $5.47              50

数据库如下

SKU-是表dbo.v_Nav_PurchasePriceCurrent中的商品编号

产品名称-是表[NAV-WH]中的描述。[dbo]。[阈值企业$项目]

介绍日期-是表dbo.v_NAV_PurchasePriceCurrent中的开始日期

建议零售价-是表dbo.v_NAV_PurchasePriceCurrent中的建议零售价

售出单位和售出金额-我使用以下代码获取:

DECLARE @SDate date
SET @SDate = '12/31/2018'
DECLARE @EDate date
SET @EDate = '5/30/2019'


SELECT        SUM(Amount) AS EXPR1
FROM            dbo.[Threshold Enterprises$Sales Invoice Line]
WHERE        ([Shipment Date] BETWEEN @SDate AND @EDate) AND (No_ = 
N'SN1580')

SELECT        SUM(Quantity) AS EXPR1
FROM            dbo.[Threshold Enterprises$Sales Invoice Line]
WHERE        ([Shipment Date] BETWEEN @SDate AND @EDate) AND (No_ = 
N'SN1580')

代码下面是我获得信息的截止日期,但我希望将所有信息与我上面发布的“已售金额”代码一并获得

/****** Script for SelectTopNRows command from SSMS  ******/
SELECT        TOP (1000) [Item No_], [Suggested Retail Price], [Starting 
Date], [Ending Date], [Price Source]
FROM            dbo.v_NAV_PurchasePriceCurrent
WHERE        ([Item No_] = N'SN1793') OR
                     ([Item No_] = N'SN1794') OR
                     ([Item No_] = N'SN1795') OR
                     ([Item No_] = N'SN1201') OR
                     ([Item No_] = N'SN1202') OR
                     ([Item No_] = N'SN2636') OR
                     ([Item No_] = N'SN2637') OR
                     ([Item No_] = N'SN2185') OR
                     ([Item No_] = N'SN2186') OR
                     ([Item No_] = N'SN1213') OR
                     ([Item No_] = N'SN1214') OR
                     ([Item No_] = N'SN1732') OR
                     ([Item No_] = N'SN1945') OR
                     ([Item No_] = N'SN1946')OR
                     ([Item No_] = N'SN1947')OR
                     ([Item No_] = N'SN1766')OR
                     ([Item No_] = N'SN1767')OR
                     ([Item No_] = N'SN1768')OR
                     ([Item No_] = N'SN2138')OR
                     ([Item No_] = N'SN2139')or
                     ([Item No_] = N'SN1201')or
                     ([Item No_] = N'SN1201')

我的预期结果发布在下面,并在其中增加了一个列,其中包括已售金额

SKU   ProductName  Introduction Date   Suggested Retail Price   Units Sold  
1234    Cream             6/12/2019               $5.47              50
4565    Pill              1/1/2019                $4.55              45

1 个答案:

答案 0 :(得分:0)

您最简单的解决方案将使用IN语句,如下所示:

/****** Script for SelectTopNRows command from SSMS  ******/
SELECT        TOP (1000) [Item No_], [Suggested Retail Price], [Starting Date], [Ending Date], [Price Source]
FROM          dbo.v_NAV_PurchasePriceCurrent
WHERE         [Item No_] IN (N'SN1793', N'SN1794', N'SN1795', N'SN1201', N'SN1202', N'SN2636', N'SN2637', 
                             N'SN2185', N'SN2186', N'SN1213', N'SN1214', N'SN1732', N'SN1945', N'SN1946', 
                             N'SN1947', N'SN1766', N'SN1767', N'SN1768', N'SN2138', N'SN2139', N'SN1201', 
                             N'SN1201')