1

这已被多次询问,但我没有找到适合我的解决方案。我有一个类似于下面的视图。我需要运行一个返回所有列的查询,但对于任何唯一的 GLASS_ID 只返回一次。我还需要一个 where 子句WHERE GLASS_ID LIKE '%' + @PartialGlassId + '%'

VEH_ID GLASS_ID OPENING_SEQ PART_NUM PREFIX_CD ANT_FLAG BLK_SIZE1
26975 DB00201 1 201 DB Y 14.00
26864 DB00375 1 375 DB N 16.00
26865 DB00375 1 375 DB N 16.00
26866 DB00375 1 375 DB N 16.00
38929 DB00408 1 408 DB N 12.00
38930 DB00408 1 408 DB N 12.00
38931 DB00408 1 408 DB N 12.00
38932 DB00408 1 408 DB N 12.00
38933 DB00408 1 408 DB N 12.00
38990 DB00408 1 408 DB N 12.00
38991 DW01015 1 1015 DB N 12.00

结果应该是

VEH_ID NAGS_GLASS_ID OPENING_SEQ PART_NUM PREFIX_CD ANT_FLAG BLK_SIZE1
26975 DB00201 1 201 DB Y 14.00
26864 DB00375 1 375 DB N 16.00
38929 DB00408 1 408 DB N 12.00
对于 WHERE GLASS_ID LIKE '%DB00%'

4

2 回答 2

1
SELECT b.* 
FROM yourTable b
INNER JOIN
  (SELECT MIN(VEH_ID) as minV,  GLASS_ID as g
   FROM yourTable
   GROUP BY GLASS_ID) as d
  ON b.Glass_ID = d.g and b.VEH_ID = d.minV
WHERE b.GLASS_ID LIKE  '%DB00%'

注意:在这种情况下,WHERE 子句也可以在 INNER JOIN 的子查询中。

于 2013-02-04T23:05:34.547 回答
0
Select distinct max(veh_ID), glass_ID, Opening_SEQ, part_Num, PRefix_CD, Ant_Flag, BLK_Size1
FROM yourTable
WHERE GLASS_ID LIKE '%' + @PartialGlassId + '%'
Group by glass_ID, Opening_SEQ, part_Num, PRefix_CD, Ant_Flag, BLK_Size1

其他选项

Select distinct group_Concat(veh_ID, ', '), glass_ID, Opening_SEQ, part_Num, PRefix_CD, Ant_Flag, BLK_Size1
FROM yourTable
WHERE GLASS_ID LIKE '%' + @PartialGlassId + '%'
Group by glass_ID, Opening_SEQ, part_Num, PRefix_CD, Ant_Flag, BLK_Size1
于 2013-02-04T23:08:18.087 回答