我有一个返回3列的查询,如下所示:
Second ref Code Description
10/14/0094 ZW01008 Side Support Bracket PM1662TAA
10/14/0095 ZW01008 Rear Hook LH PM1664TAA
10/14/0095 ZW01008 Rear Hook LH PM1664TAA
10/14/0095 ZW01008 Rear Hook RH PM1663TAA
10/14/0095 ZW01008 Rear Hook RH PM1663TAA
10/14/0096 ZW01008 Sunblind Brackets PM1665TAA
10/14/0098 ZW01008 NEW TOOL TRIAL 300 HALFS 600 WEDGES
10/14/0100 ZW01008 NEW TOOL TRIAL WATER RESERVOIR
如果有多个行具有相同的SecondRef
我希望将这些行组合在一起,并且只显示Description
个值中的一个,因为它们将是彼此的变体,或者组合一条记录中所有4条记录的值。
以下是我用来获取示例的查询:
SELECT AllocationBalance.SecondRef, StockItem.Code, BomRecord.Description
FROM AllocationBalance AS AllocationBalance
INNER JOIN StockItem AS StockItem ON AllocationBalance.ItemID = StockItem.ItemID
INNER JOIN BomAllocation ON AllocationBalance.Reference = BomAllocation.AllocationNumber
INNER JOIN WopOrder ON BomAllocation.BomAllocationID = WopOrder.BomAllocationID
INNER JOIN BomRecord ON WopOrder.BomRecordID = BomRecord.BomRecordID
WHERE (AllocationBalance.RecipientName = 'BOM') AND (StockItem.Code LIKE '%ZW01008%')
答案 0 :(得分:3)
只需使用GROUP BY
和MAX
或MIN
:
SELECT AllocationBalance.SecondRef,
StockItem.Code,
MAX(dbo.BomRecord.Description) as Description
FROM [YOUR_TABLES] -- including joins
WHERE (AllocationBalance.RecipientName = 'BOM') AND (StockItem.Code LIKE '%ZW01008%')
GROUP BY AllocationBalance.SecondRef, StockItem.Code
因此,按前两列进行分组并获取描述列中的最后一个/最高值。
使用MAX
代替MIN
会覆盖您,因为@Gordon可能会在评论中提及空白描述。
答案 1 :(得分:0)
尝试DISTINCT。 如果我理解正确 - 这就是你需要的。