如何根据字段值组合行

时间:2014-04-25 10:53:59

标签: sql sql-server-2008

我设计了以下查询

SELECT 
v.visitid,
CASE 
   WHEN vd.DocType = 1 THEN 'y' ELSE 'n'
END as 'FinalReportAttached'
,CASE 
   WHEN vd.DocType = 13 THEN 'y' ELSE 'n'
END as 'InspectorReportAttached'
,CASE 
   WHEN vd.DocType = 2 THEN 'y' ELSE 'n'
END as 'Co-ordReportAttached'

FROM Visits v
INNER JOIN VisitDocs vd on vd.VisitID = v.VisitID  
WHERE v.VisitID  = 79118

enter image description here

我喜欢在一行中显示结果。如果报告在那里那么'y',如果没有那么'n'。

有一个关于manay关系的b / w访问和visitdoc表。 Visitdoc可以为一次访问提供许多不同的文档。我需要检查visitdoc是否有每次访问的文件类型1,3或12然后说是,否则没有。

visitID |FinalReport |InspectorReport |Co-ordReport 
------------------------------------------------
79118   |n           |y               |y

6 个答案:

答案 0 :(得分:1)

要获得一行,请使用聚合函数:

SELECT vd.visitid,
       MAX(CASE WHEN vd.DocType = 1 THEN 'y' ELSE 'n' END) as FinalReportAttached,
       MAX(CASE WHEN vd.DocType = 13 THEN 'y' ELSE 'n' END) as InspectorReportAttached,
       MAX(CASE WHEN vd.DocType = 2 THEN 'y' ELSE 'n' END) as [Co-ordReportAttached]
FROM VisitDocs vd
WHERE vd.VisitID  = 79118;

请注意,这可行,因为'y'> 'n'(至少在我熟悉的角色排序中)。

我还以其他两种方式更改了查询。我从列别名中删除了单引号。仅对字符串和日期常量使用单引号。对标识符使用单引号可能会导致混淆。此外,似乎不需要连接,因为原始查询使用内部联接而不使用v中的其他字段。

答案 1 :(得分:0)

请尝试:

select 
    visitid, 
    MAX(FinalReportAttached)  FinalReportAttached, 
    MAX(InspectorReportAttached) InspectorReportAttached, 
    MAX([Co-ordReportAttached]) [Co-ordReportAttached]
from(
    SELECT 
    v.visitid,
    CASE 
       WHEN vd.DocType = 1 THEN 'y' ELSE 'n'
    END as 'FinalReportAttached'
    ,CASE 
       WHEN vd.DocType = 13 THEN 'y' ELSE 'n'
    END as 'InspectorReportAttached'
    ,CASE 
       WHEN vd.DocType = 2 THEN 'y' ELSE 'n'
    END as 'Co-ordReportAttached'

    FROM Visits v
    INNER JOIN VisitDocs vd on vd.VisitID = v.VisitID  
    WHERE v.VisitID  = 79118
)x
group by visitid

答案 2 :(得分:0)

你可以试试这个:

SELECT 
v.visitid,
CASE 
   WHEN SUM(CASE WHEN vd.DocType = 1 THEN 1 ELSE 0 END)>0 THEN 'y' ELSE 'n'
END as 'FinalReportAttached'
,CASE 
   WHEN SUM(CASE WHEN vd.DocType = 13 THEN 1 ELSE 0 END)>0 THEN 'y' ELSE 'n'
END as 'InspectorReportAttached'
,CASE 
   WHEN SUM(CASE WHEN vd.DocType = 2 THEN 1 ELSE 0 END)>0 THEN 'y' ELSE 'n'
END as 'Co-ordReportAttached'
FROM Visits v
INNER JOIN VisitDocs vd on vd.VisitID = v.VisitID  
WHERE v.VisitID  = 79118
GROUP BY v.VisitID

答案 3 :(得分:0)

这将为您解决问题

SELECT 
v.visitid,
CASE 
   WHEN sum(case when vd.DocType = 1  THEN 1 ELSE 0 END) = 1 THEN 'y' ELSE 'n'
END as 'FinalReportAttached'
,CASE 
   WHEN sum(case when vd.DocType = 13  THEN 1 ELSE 0 END) = 1 THEN 'y' ELSE 'n'
END as 'InspectorReportAttached'
,CASE 
   WHEN sum(case when vd.DocType = 2  THEN 1 ELSE 0 END) = 1 THEN THEN 'y' ELSE 'n'
END as 'Co-ordReportAttached'

FROM Visits v
INNER JOIN VisitDocs vd on vd.VisitID = v.VisitID  
WHERE v.VisitID  = 79118
GROUP BY v.visitId

答案 4 :(得分:0)

SELECT 
    v.visitid,
MAX(CASE 
        WHEN vd.DocType = 1 THEN 'y' ELSE 'n'
    END) as 'FinalReportAttached'
,MAX(CASE 
        WHEN vd.DocType = 13 THEN 'y' ELSE 'n'
    END) as 'InspectorReportAttached'
,MAX(CASE 
        WHEN vd.DocType = 2 THEN 'y' ELSE 'n'
    END) as 'Co-ordReportAttached'
FROM Visits v
INNER JOIN VisitDocs vd 
    on vd.VisitID = v.VisitID  
WHERE v.VisitID  = 79118
group by v.VisitID

答案 5 :(得分:0)

为什么没有人使用EXISTS。好的,它有点长。

SELECT 
v.visitid,
CASE 
   WHEN EXISTS (SELECT * FROM VisitDocs vd WHERE vd.VisitID = v.VisitID AND vd.DocType = 1) THEN 'y' ELSE 'n'
END AS 'FinalReportAttached'
,CASE 
   WHEN EXISTS (SELECT * FROM VisitDocs vd WHERE vd.VisitID = v.VisitID AND vd.DocType = 13) THEN 'y' ELSE 'n'
END AS 'InspectorReportAttached'
,CASE 
   WHEN EXISTS (SELECT * FROM VisitDocs vd WHERE vd.VisitID = v.VisitID AND vd.DocType = 2) THEN 'y' ELSE 'n'
END AS 'Co-ordReportAttached'
FROM Visits v
WHERE v.VisitID  = 79118