我有3个数据库(下面分开的实际查询)我一起查询了联合。我试图在3个基于MFG PART NO的3个中获得最低成本。 一个常见的是MFG PART NO,所以我希望这样,以及所有3个供应商的最低价格。 我创建了一个联合查询,如下所示:
SELECT INGRAM.[MFG NAME], INGRAM.[MFG PART NO], INGRAM.[DESCRIPT 1] AS DESCRIPTION, INGRAM.Cost, INGRAM.MLP AS [LIST PRICE], INGRAM.[INGRAM PARTNO] AS [VENDOR SKU], INGRAM.LGTH AS COO
FROM INGRAM
UNION ALL
SELECT [SYNNEX CURRENT].[MFG NAME], [SYNNEX CURRENT].[MFG PART NO], [SYNNEX CURRENT].[Part Description] AS DESCRIPTION, [SYNNEX CURRENT].Cost, [SYNNEX CURRENT].MSRP AS [LIST PRICE], [SYNNEX CURRENT].[SYNNEX SKU] AS [VENDOR SKU], [SYNNEX CURRENT].[Country of Origin] AS COO
FROM [SYNNEX CURRENT]
UNION ALL
SELECT [Digitek Product Catalog].Manufacturer AS [MFG NAME], [Digitek Product Catalog].[Part No] AS [MFG PART NO], [Digitek Product Catalog].Description AS DESCRIPTION, [Digitek Product Catalog].[Unit Price] AS Cost, [Digitek Product Catalog].[List Price], [Digitek Product Catalog].[Part No] AS [VENDOR SKU], [Digitek Product Catalog].[Country Of Origin] AS COO
FROM [Digitek Product Catalog]
ORDER BY [MFG PART NO];
这将所有目录合并为一个。
然后我运行另一个查询尝试从成本列中获取最小值,但由于MFG NAME列包含同一制造商的不同名称(取决于供应商),因此VENDOR SKU也不同。
我想要一个显示我所有信息的结果,但只返回相同MFG PART NO常用项目中的最佳费用。
答案 0 :(得分:0)
您可以使用前1个查询来完成此操作。
Select TOP 1 * FROM <yourunionquery> ORDER BY Cost
如果有多个价格相同,您将获得多行。在这种情况下,只需在排序中添加另一个字段即可。例如。按成本排序,[MFG名称]