在case语句中选择不同的sql

时间:2014-02-27 10:29:24

标签: sql select case

我需要在sql结果中添加空行,以便总是有3行。如果我的查询返回0行,我需要创建一个空白行的3个联合,如果查询返回1行,我需要UNION 2空白行。如果查询返回2行,我需要UNION只有一个空行,如果查询返回3行,则不添加空行。

我是SQL的新手,我正在尝试这样的事情:

SELECT CASE (COUNT(*) FROM public_getxo_alumbrado_puntos_luz_soportes_demo where id_punto_luz=CInt(Reports![TODO_horizontal]!id_punto_luz))
WHEN 0 THEN (SELECT * FROM public_getxo_alumbrado_puntos_luz_soportes_demo where id_punto_luz=CInt(Reports![TODO_horizontal]!id_punto_luz) 
UNION ALL Select DISTINCT '' as tipo_soporte , '' as subtipo_soporte, '' as soporte_ppal_forma, '' as material, '' as soporte_ppal_altura, '' as longitud, '' as soporte_aux_angulo, '' as modelo, '' as marca, '' as estado, '' as fecha_instalacion, '' as fecha_intervencion, '' as fecha_baja, '' as observaciones, '' as tipo_intervencion , CInt(Reports![TODO_horizontal]!id_punto_luz) as id_punto_luz, '' as id_soporte, '' as id_modelo  from public_getxo_alumbrado_puntos_luz_soportes_demo 
UNION ALL Select DISTINCT '' as tipo_soporte , '' as subtipo_soporte, '' as soporte_ppal_forma, '' as material, '' as soporte_ppal_altura, '' as longitud, '' as soporte_aux_angulo, '' as modelo, '' as marca, '' as estado, '' as fecha_instalacion, '' as fecha_intervencion, '' as fecha_baja, '' as observaciones, '' as tipo_intervencion , CInt(Reports![TODO_horizontal]!id_punto_luz) as id_punto_luz, '' as id_soporte, '' as id_modelo  from public_getxo_alumbrado_puntos_luz_soportes_demo 
UNION ALL Select DISTINCT '' as tipo_soporte , '' as subtipo_soporte, '' as soporte_ppal_forma, '' as material, '' as soporte_ppal_altura, '' as longitud, '' as soporte_aux_angulo, '' as modelo, '' as marca, '' as estado, '' as fecha_instalacion, '' as fecha_intervencion, '' as fecha_baja, '' as observaciones, '' as tipo_intervencion , CInt(Reports![TODO_horizontal]!id_punto_luz) as id_punto_luz, '' as id_soporte, '' as id_modelo  from public_getxo_alumbrado_puntos_luz_soportes_demo)

WHEN 1 THEN (SELECT * FROM public_getxo_alumbrado_puntos_luz_soportes_demo where id_punto_luz=CInt(Reports![TODO_horizontal]!id_punto_luz)
UNION ALL Select DISTINCT '' as tipo_soporte , '' as subtipo_soporte, '' as soporte_ppal_forma, '' as material, '' as soporte_ppal_altura, '' as longitud, '' as soporte_aux_angulo, '' as modelo, '' as marca, '' as estado, '' as fecha_instalacion, '' as fecha_intervencion, '' as fecha_baja, '' as observaciones, '' as tipo_intervencion , CInt(Reports![TODO_horizontal]!id_punto_luz) as id_punto_luz, '' as id_soporte, '' as id_modelo  from public_getxo_alumbrado_puntos_luz_soportes_demo
UNION ALL Select DISTINCT '' as tipo_soporte , '' as subtipo_soporte, '' as soporte_ppal_forma, '' as material, '' as soporte_ppal_altura, '' as longitud, '' as soporte_aux_angulo, '' as modelo, '' as marca, '' as estado, '' as fecha_instalacion, '' as fecha_intervencion, '' as fecha_baja, '' as observaciones, '' as tipo_intervencion , CInt(Reports![TODO_horizontal]!id_punto_luz) as id_punto_luz, '' as id_soporte, '' as id_modelo  from public_getxo_alumbrado_puntos_luz_soportes_demo)

WHEN 2 THEN (SELECT * FROM public_getxo_alumbrado_puntos_luz_soportes_demo where id_punto_luz=CInt(Reports![TODO_horizontal]!id_punto_luz)
UNION ALL Select DISTINCT '' as tipo_soporte , '' as subtipo_soporte, '' as soporte_ppal_forma, '' as material, '' as soporte_ppal_altura, '' as longitud, '' as soporte_aux_angulo, '' as modelo, '' as marca, '' as estado, '' as fecha_instalacion, '' as fecha_intervencion, '' as fecha_baja, '' as observaciones, '' as tipo_intervencion , CInt(Reports![TODO_horizontal]!id_punto_luz) as id_punto_luz, '' as id_soporte, '' as id_modelo  from public_getxo_alumbrado_puntos_luz_soportes_demo)

WHEN 3 THEN (SELECT * FROM public_getxo_alumbrado_puntos_luz_soportes_demo where id_punto_luz=CInt(Reports![TODO_horizontal]!id_punto_luz))
END
FROM public_getxo_alumbrado_puntos_luz_soportes_demo;

1 个答案:

答案 0 :(得分:2)

SELECT *
FROM (
    SELECT 1 AS precedence, * FROM FROM public_getxo_alumbrado_puntos_luz_soportes_demo where id_punto_luz=CInt(Reports![TODO_horizontal]!id_punto_luz
    UNION
    SELECT 2 AS precedence, '' as tipo_soporte , '' as subtipo_soporte, '' as soporte_ppal_forma, '' as material, '' as soporte_ppal_altura, '' as longitud, '' as soporte_aux_angulo, '' as modelo, '' as marca, '' as estado, '' as fecha_instalacion, '' as fecha_intervencion, '' as fecha_baja, '' as observaciones, '' as tipo_intervencion , CInt(Reports![TODO_horizontal]!id_punto_luz) as id_punto_luz, '' as id_soporte, '' as id_modelo  from public_getxo_alumbrado_puntos_luz_soportes_demo
    UNION 
    SELECT 3 AS precedence, '' as tipo_soporte , '' as subtipo_soporte, '' as soporte_ppal_forma, '' as material, '' as soporte_ppal_altura, '' as longitud, '' as soporte_aux_angulo, '' as modelo, '' as marca, '' as estado, '' as fecha_instalacion, '' as fecha_intervencion, '' as fecha_baja, '' as observaciones, '' as tipo_intervencion , CInt(Reports![TODO_horizontal]!id_punto_luz) as id_punto_luz, '' as id_soporte, '' as id_modelo  from public_getxo_alumbrado_puntos_luz_soportes_demo
    )
ORDER BY precedence
LIMIT 3