SQL通过3个单独的表连接和分组

时间:2013-02-21 14:10:49

标签: sql sql-server-2008

我在这里有两个单独的查询,我需要在一个查询中进行,我会发布查询,然后尝试解释我正在尝试做什么。

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的前三个字符相关...我没有设计此设置。我现在只想尝试使用它。

如何将这两个陈述加入一个单一的陈述中,该陈述将为我提供供应商名称,料品描述,计量单位(销售M的M)以及料品描述唯一的料品数量?

3 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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()将返回最大值。如果一切都相同,那么这是获得“任意”值的好方法。

答案 2 :(得分: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')