1

如何将这些多行变成一行?N 和 Y 是布尔值。

Id  IsPnt IsPms, IsPdt  
1   N     Y      N  
1   N     Y      N  
1   Y     N      N

进入这个

Id  IsPnt IsPms, IsPdt  
1   Y     Y      N 

编辑:

生成结果集的查询如下所示

select b.id, 
    CASE mpft.PlanIndCd WHEN 'PBMN' THEN 1 ELSE 0 END AS IsPnt,
    CASE mpft.PlanIndCd WHEN 'PBMT' THEN 1 ELSE 0 END AS IsPbt,
    CASE mpft.PlanIndCd WHEN 'PBMS' THEN 1 ELSE 0 END AS IsPms
from vw_D_SomveViewName pb
   -- bunch of joins 
where mpft.PlanIndCd in ('HANR', 'PBMN','PBMT','PBMS','HAWR')
4

4 回答 4

4

MAX()如果这些值是真实YN唯一的,您可以简单地使用它。

SELECT ID, MAX(IsPnt) IsPnt, MAX(IsPms) IsPms, MAX(IsPdt) IsPdt
FROM   tableName
GROUP  BY ID

更新 1

SELECT  b.id, 
        MAX(CASE mpft.PlanIndCd WHEN 'PBMN' THEN 1 ELSE 0 END) AS IsPnt,
        MAX(CASE mpft.PlanIndCd WHEN 'PBMT' THEN 1 ELSE 0 END) AS IsPbt,
        MAX(CASE mpft.PlanIndCd WHEN 'PBMS' THEN 1 ELSE 0 END) AS IsPms
FROM    vw_D_SomveViewName pb
        -- bunch of joins 
WHERE   mpft.PlanIndCd in ('HANR', 'PBMN','PBMT','PBMS','HAWR')
GROUP   BY b.ID
于 2013-04-02T14:16:13.433 回答
1

这行得通吗?

select
  id,
  max(IsPnt),
  max(IsPms),
  max(IsPdt)
from
  table
GROUP BY
  id
于 2013-04-02T14:17:25.957 回答
0

编辑您的问题后,您可以直接使用PIVOT表运算符而不是使用MAX表达式,例如:

SELECT
  Id,
  PBMN AS IsPnt,
  PBMT AS IsPbt,
  PBMS AS IsPms
FROM
(
   SELECT
     id,
     mpft.PlanIndCd, 
     ROW_NUMBER() OVER(PARTITION BY id
                       ORDER BY ( SELECT 1)) AS RN
   from vw_D_SomveViewName pb
   -- bunch of joins 
   where mpft.PlanIndCd in ('HANR', 'PBMN','PBMT','PBMS','HAWR')
) AS t
PIVOt
(
   MAX(RN)
   FOR PlanIndCd IN ([PBMN], [PBMT], [PBMS])
) AS p;

您可以在以下演示示例中看到它的实际效果:

SQL Fiddle 演示

于 2013-04-02T14:32:25.317 回答
-5
select Id, MAX(IPnt), MAX(IsPms), MAX(IsPdt) 
from table etc
于 2013-04-02T14:17:25.407 回答