2014-02-27 110 views
0

我需要将空行添加到sql结果中,以便始终有3行。如果我的查询返回0行,我需要做一个空行的3个联合,如果查询返回1行,我需要UNION 2空行。如果查询返回2行,我需要UNION只有一个空行,并且如果查询返回3行,请添加空行。不同的select sql在case语句中

我新手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

非约束性建议:如果可能的话,做SQL外部这样的事情。 – Ashalynd

回答

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 
+0

感谢在ms访问中为我工作,使用TOP 3而不是LIMIT 3 – Egidi

+0

您没有说出您正在使用的数据库。我的答案在MySQL中起作用。 – Barmar