【问题标题】:Different select sql in case statementcase语句中不同的select sql
【发布时间】:2014-02-27 10:29:24
【问题描述】:

我需要在 sql 结果中添加空白行,以便始终拥有 3 行。如果我的查询返回 0 行,我需要创建 3 个空白行的联合,如果查询返回 1 行,我需要联合 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;

【问题讨论】:

  • 一个不具约束力的建议:如果可能,在 SQL 之外做这些事情。

标签: sql select case


【解决方案1】:
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

【讨论】:

  • 感谢使用 TOP 3 而不是 LIMIT 3 在 ms 访问中为我工作
  • 你没有说你使用的是什么数据库。我的答案在 MySQL 中有效。
猜你喜欢
  • 2014-12-05
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2012-05-18
相关资源
最近更新 更多