SQL PIVOT多列与关键字搜索

时间:2019-06-11 08:52:01

标签: sql sql-server pivot

我有一个单位表,如下所示:

enter image description here

我正在使用以下查询返回搜索结果,但它为alt单位创建了单独的行。

SELECT U.unit_id as primary_unit_id, U.unit_name as primary_unit,  
    alt_id1 = (select top 1 unit_id from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=1),
    alt_unit1 = (select top 1 unit_name from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=1),
    alt_conversion1 = (select top 1 conversion from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=1),
    alt_id2 = (select top 1 unit_id from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=2),
    alt_unit2 = (select top 1 unit_name from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=2),
    alt_conversion2 = (select top 1 conversion from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=2),
    alt_id3 = (select top 1 unit_id from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=3),
    alt_unit3 = (select top 1 unit_name from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=3),
    alt_conversion3 = (select top 1 conversion from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=3),
    alt_id4 = (select top 1 unit_id from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=4),
    alt_unit4 = (select top 1 unit_name from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=4),
    alt_conversion4 = (select top 1 conversion from T_DX_UNITS where alt_of=U.unit_id and alt_sort_no=4)
FROM T_DX_UNITS U
WHERE --U.alt_of IS NULL AND 
(U.unit_name LIKE @keyword + '%' OR U.unit_name LIKE '%' + @keyword + '%')

给出以下输出:(第一行是正确的,它也显示了alt单位,但它还显示了alt单位的单独行)

enter image description here

请求更新 所需结果:(它在没有过滤器的情况下可以正常工作,如下所示) enter image description here

SQL FIDDLE

1 个答案:

答案 0 :(得分:3)

只是一个猜测。可能您还需要一些基本单位,这些单位本身并不满足过滤器的要求,但其中一些替代单位确实可以满足。

DECLARE @keyword nvarchar(10) = 'unit';
WITH alts AS (
   SELECT [unit_id], [unit_name], [conversion],[alt_of], [alt_sort_no] 
   FROM T_DX_UNITS 
   WHERE alt_of IS NOT NULL AND unit_name LIKE '%' + @keyword + '%'
)
SELECT U.unit_id as primary_unit_id, U.unit_name as primary_unit,  
    alt_id1,
    alt_unit1,
    alt_conversion1,
    alt_id2,
    alt_unit2 ,
    alt_conversion2,
    alt_id3,
    alt_unit3,
    alt_conversion3
    alt_id4,
    alt_unit4,
    alt_conversion4
FROM T_DX_UNITS U
CROSS APPLY (
    SELECT 
    alt_id1 = (select top 1 unit_id from alts a where a.alt_of=U.unit_id and a.alt_sort_no=1),
    alt_unit1 =      (select top 1 unit_name from alts a where a.alt_of=U.unit_id and a.alt_sort_no=1),
    alt_conversion1 =(select top 1 conversion from alts a where a.alt_of=U.unit_id and a.alt_sort_no=1),
    alt_id2 =        (select top 1 unit_id from alts a where a.alt_of=U.unit_id and a.alt_sort_no=2),
    alt_unit2 =      (select top 1 unit_name from alts a where a.alt_of=U.unit_id and a.alt_sort_no=2),
    alt_conversion2 =(select top 1 conversion from alts a where a.alt_of=U.unit_id and a.alt_sort_no=2),
    alt_id3 =        (select top 1 unit_id from alts a where a.alt_of=U.unit_id and a.alt_sort_no=3),
    alt_unit3 =      (select top 1 unit_name from alts a where a.alt_of=U.unit_id and a.alt_sort_no=3),
    alt_conversion3 =(select top 1 conversion from alts a where a.alt_of=U.unit_id and a.alt_sort_no=3),
    alt_id4 =        (select top 1 unit_id from alts a where a.alt_of=U.unit_id and a.alt_sort_no=4),
    alt_unit4 =      (select top 1 unit_name from alts a where a.alt_of=U.unit_id and a.alt_sort_no=4),
    alt_conversion4 =(select top 1 conversion from alts a where a.alt_of=U.unit_id and a.alt_sort_no=4)
    ) t
WHERE U.alt_of IS NULL 
AND (U.unit_name LIKE '%' + @keyword + '%' 
    OR coalesce(t.alt_id1, t.alt_id2, t.alt_id3, t.alt_id4) IS NOT NULL)
相关问题