0

我在这里有两个单独的查询,我需要将它们合并为一个查询,我将发布查询,然后尝试解释我正在尝试做的事情。

SELECT Distinct I.ITMCDE, V.VNDRCDE, V.VNAME
  FROM (SELECT RIGHT(Items.[Item Number], 3) as ITMCDE FROM Items) I, 
       (SELECT LEFT(Vendors.[Vendor ID], 3) as VNDRCDE, 
               Vendors.[Vendor Name] as VNAME
          FROM Vendors) V
 WHERE I.ITMCDE = V.VNDRCDE

在第一个中,我只是将供应商代码与项目代码进行匹配,以获取生产该项目的供应商名称。

SELECT DISTINCT (Items.[Item Description]), ItemQuantities.[QTY Available],
       Items.[Selling U Of M], Items.[Item Number] 
  FROM ItemQuantities 
 INNER JOIN Items ON ItemQuantities.[Item Number] = Items.[Item Number] 
 WHERE Items.[Item Number] LIKE 'WH%'
   AND Items.[Item Number] NOT LIKE '%RMW'

在第二个中,我选择项目描述,两个单独表格中的可用数量(可用数量在不同的表格中,使用项目编号匹配它们)

如您所见,三个表之间的唯一关联是项目编号,甚至在供应商表中也没有。项目编号的最后三个字符与供应商 ID 的前三个字符相关......我没有设计这个设置。我现在只是在尝试使用它。

如何将这两个语句合并为一个语句,该语句将为我提供供应商名称、项目描述、计量单位(销售 U 的 M)和项目描述唯一的项目数量?

4

3 回答 3

0

我认为这应该有效:

SELECT DISTINCT Items.[Item Description], 
       ItemQuantities.[QTY Available],
       Items.[Selling U Of M], 
       Items.[Item Number], 
       V.VNAME
FROM ItemQuantities 
   INNER JOIN Items ON ItemQuantities.[Item Number] = Items.[Item Number] 
   INNER JOIN Vendors ON 
       RIGHT(Items.[Item Number], 3) = LEFT(Vendors.[Vendor ID], 3)
WHERE Items.[Item Number] LIKE 'WH%'
       AND Items.[Item Number] NOT LIKE '%RMW'
于 2013-02-21T14:21:08.533 回答
0

你可以加入它:

SELECT DISTINCT (i.[Item Description]), iq.[QTY Available],
       i.[Selling U Of M], i.[Item Number],
       V.VNDRCDE, V.VNAME
FROM ItemQuantities iq INNER JOIN
     Items i
     ON iq.[Item Number] = i.[Item Number] left outer join
     Vendors v
     on LEFT(v.[Vendor ID], 3) = RIGHT(i.[Item Number], 3) 
WHERE i.[Item Number] LIKE 'WH%' and
      i.[Item Number] NOT LIKE '%RMW'

我不确定你是否打算用括号来做任何特别的事情i.[Item Description]distinct关键字适用于整行。

如果您distinct只想要描述,那么您需要使用group by. 就像是:

SELECT i.[Item Description],
       max(iq.[QTY Available]),
       max(i.[Selling U Of M]), max(i.[Item Number]),
       max(V.VNDRCDE), max(V.VNAME)
FROM ItemQuantities iq INNER JOIN
     Items i
     ON iq.[Item Number] = i.[Item Number] left outer join
     Vendors v
     on LEFT(v.[Vendor ID], 3) = RIGHT(i.[Item Number], 3) 
WHERE i.[Item Number] LIKE 'WH%' and
      i.[Item Number] NOT LIKE '%RMW'
group by i.[Item Description])

max()返回最大值。如果一切都相同,那么这是获得“任意”值的好方法。

于 2013-02-21T14:21:19.503 回答
0

@sgeddes 谢谢。

这就是我想出的

SELECT DISTINCT
            Item.ITEMNMBR AS [Item Number], 
            Item.ITEMDESC AS [Item Description], 
            Item.ITMGEDSC AS [Item Category], 
            Item.SELNGUOM AS [Unit of Measure], 
            (SELECT VENDNAME FROM PM00200 WHERE (VENDORID = IV00103.VENDORID)) AS [Vendor Name], 
            (CASE WHEN Quan.QTYONHND > 0 THEN 'In Stock' ELSE 'Out of Stock' END) AS [Stock Status]
FROM       IV00101 AS Item INNER JOIN
                      IV00102 AS Quan ON Item.ITEMNMBR = Quan.ITEMNMBR INNER JOIN
                      IV00103 ON Item.ITEMNMBR = IV00103.ITEMNMBR AND Quan.ITEMNMBR = IV00103.ITEMNMBR
WHERE     (Item.ITEMNMBR LIKE 'WH%') AND (IV00103.VENDORID NOT LIKE '%MIL')

于 2013-02-22T00:23:40.927 回答