我想要一个机动运输工具的列表,拖车设备的数量在单独的列中列出。我正在使用case语句对不同的设备类型进行分类,但是我得到了多个退货,而不是每个承运人看到一个退货。
我尝试了ROW OVER()并得到相同的结果。
SELECT C.ECP_CARRIER_IDENT_ID AS ECP_ID, C.CARRIER_NM, C.MC_NB, C.DOT_NB,
(CASE WHEN E.EQUIPMENT_TYPE_CD = 'TR' THEN E.EQUIPMENT_CT END) AS TRACTORS,
(CASE WHEN E.EQUIPMENT_TYPE_CD = 'SV' THEN E.EQUIPMENT_CT END) AS VAN_TRLS,
(CASE WHEN E.EQUIPMENT_TYPE_CD = 'FT' THEN E.EQUIPMENT_CT END) AS FLATBEDS
FROM ECP_CARRIER C
JOIN ECP_CARRIER_EQUIPMENT E ON C.ECP_CARRIER_IDENT_ID = E.ECP_CARRIER_ID
WHERE CERTIFIED_FG = 1
GROUP BY C.ECP_CARRIER_IDENT_ID, C.CARRIER_NM, C.MC_NB, C.DOT_NB,
E.EQUIPMENT_TYPE_CD, E.EQUIPMENT_CT
我希望所有内容都在同一行,但这是我得到的结果
ECP_NB CARRIER_NM MC_NB DOT_NB TRACTORS VAN_TRLS FLATBEDS
1234 Bob's Trucking 606066 66 10
1234 Bob's Trucking 606066 66 12
答案 0 :(得分:0)
使用聚合函数并修复group by
:
SELECT C.ECP_CARRIER_IDENT_ID AS ECP_ID, C.CARRIER_NM, C.MC_NB, C.DOT_NB,
MAX(CASE WHEN E.EQUIPMENT_TYPE_CD = 'TR' THEN E.EQUIPMENT_CT END) AS TRACTORS,
MAX(CASE WHEN E.EQUIPMENT_TYPE_CD = 'SV' THEN E.EQUIPMENT_CT END) AS VAN_TRLS,
MAX(CASE WHEN E.EQUIPMENT_TYPE_CD = 'FT' THEN E.EQUIPMENT_CT END) AS FLATBEDS
FROM ECP_CARRIER C JOIN
ECP_CARRIER_EQUIPMENT E
ON C.ECP_CARRIER_IDENT_ID = E.ECP_CARRIER_ID
WHERE CERTIFIED_FG = 1
GROUP BY C.ECP_CARRIER_IDENT_ID, C.CARRIER_NM, C.MC_NB, C.DOT_NB;
GROUP BY
中的键组合应定义结果集中所需的每一行。例如,您不需要E.EQUIPMENT_TYPE_CD
的单独行,因此它不应位于GROUP BY
中。