早上好人。
刚刚开始使用SQL,我还处于学习阶段。
我对我认为简单的“CASE WHEN”声明感到非常沮丧。
我目前正在尝试将1个位置的所有不同属性类型拉为“是”或“否”(空白字段)格式。
附加了SQL Diagram Pane,Criteria Pane,SQL Pane和Results的屏幕截图。
SQL CODE:
SELECT DISTINCT
TOP (100) PERCENT dbo.ih_location.place_ref, dbo.co_place.address##1, dbo.co_place.address##2, dbo.ih_location.location_type,
CASE WHEN (dbo.ih_loc_attr.attrib_code = '10%T') THEN 'Yes' ELSE '' END AS [10%T],
CASE WHEN (dbo.ih_loc_attr.attrib_code = 'EPCB') THEN 'Yes' ELSE '' END AS EPCB, CASE WHEN (dbo.ih_loc_attr.attrib_code = 'GR1') THEN 'Yes' ELSE '' END AS GR1,
CASE WHEN (dbo.ih_loc_attr.attrib_code = 'SAP') THEN 'Yes' ELSE '' END AS SAP, CASE WHEN (dbo.ih_loc_attr.attrib_code = 'SOCH') THEN 'Yes' ELSE '' END AS SOCH, dbo.ih_loc_attr.attrib_code
FROM
dbo.co_place LEFT OUTER JOIN
dbo.ih_loc_attr ON dbo.co_place.place_ref = dbo.ih_loc_attr.place_ref RIGHT OUTER JOIN
dbo.ih_location ON dbo.co_place.place_ref = dbo.ih_location.place_ref
WHERE
(dbo.ih_location.place_ref = '00010001')
ORDER BY
dbo.ih_location.place_ref
正如您将看到我出现在6个不同的行上。理想情况下,我希望在1行显示,但在不同的列标题中显示。
我希望我所说的有道理。
对此的任何帮助都会很棒。
非常感谢 斯图尔特
答案 0 :(得分:2)
使用聚合:
n
注意:
SELECT l.place_ref, p.address##1, p.address##2, l.location_type,
MAX(CASE WHEN la.attrib_code = '10%T' THEN 'Yes' END) AS [10%T],
MAX(CASE WHEN la.attrib_code = 'EPCB' THEN 'Yes' END) AS EPCB,
MAX(CASE WHEN la.attrib_code = 'GR1' THEN 'Yes' END) AS GR1,
MAX(CASE WHEN la.attrib_code = 'SAP' THEN 'Yes' END) AS SAP,
MAX(CASE WHEN la.attrib_code = 'SOCH' THEN 'Yes' END) AS SOCH
FROM dbo.co_place p LEFT OUTER JOIN
dbo.ih_loc_attr la
ON p.place_ref =ila.place_ref RIGHT OUTER JOIN
dbo.ih_location l
ON p.place_ref = l.place_ref
WHERE l.place_ref = '00010001'
GROUP BY l.place_ref, p.address##1, p.address##2, l.location_type;
,但我没有修复该部分查询。RIGHT OUTER JOIN
是不必要的。只需使用ELSE ''
值,让聚合处理逻辑。NULL
。