与选择列表中不同数量的列联合?

发布于 2024-12-09 06:02:07 字数 1292 浏览 1 评论 0原文

我有这个疑问,但我需要为所有人建立一个联盟。但是每个查询在选择列表中都有不同数量的列,这给了我错误:

使用 UNION、INTERSECT 或 EXCEPT 运算符组合的所有查询在其目标列表中必须具有相同数量的表达式。

我该如何解决这个问题以避免错误?

     Select 'Pregunta:(8-9-10)Totales No Coinciden' as Descripcion_Error, c_Fk_IdBoleta as Boleta,
       f_TotalAreaExtensionFinca as Extension_Total, f_TotalAreaDedicadaFinca 
       as Area_Dedicada_Finca, f_TotalAreaTenenciaFinca as Tenencia_Finca
      from Fnc_TenenciaUsoTierra
      where (f_TotalAreaExtensionFinca <> f_TotalAreaDedicadaFinca OR f_TotalAreaExtensionFinca <> f_TotalAreaTenenciaFinca) 
      AND  Fnc_TenenciaUsoTierra.c_Fk_IdBoleta = @id_Boleta


UNION 

SELECT 'Pregunta (12) El área sembrada es mayor al área dedicada a cultivos' as Descripcion_Error,
   c_Fk_IdBoleta as Boleta,
   (SELECT SUM(f_AreaDedicadaCultivos)
      FROM Fnc_TenenciaUsoTierra
      WHERE c_Fk_IdBoleta = sembrado.c_Fk_IdBoleta)
   AS AreaDedicadaCultivos,
   SUM(sembrado.f_AreaSiembra) as AreaSembrada
FROM
   Clt_Sembrado as sembrado
WHERE
   sembrado.c_Fk_IdBoleta = 45550711
GROUP BY sembrado.c_Fk_IdBoleta
HAVING SUM(sembrado.f_AreaSiembra) > (SELECT SUM(f_AreaDedicadaCultivos)
      FROM Fnc_TenenciaUsoTierra
      WHERE c_Fk_IdBoleta = sembrado.c_Fk_IdBoleta)

I have this queries, but I need to make a Union for all. But each query has a different quantity of columns in the select-list, which gives me the error:

All queries combined using a UNION, INTERSECT or EXCEPT operator must have the same number of expressions in their target lists.

How can I fix this to avoid the error?

     Select 'Pregunta:(8-9-10)Totales No Coinciden' as Descripcion_Error, c_Fk_IdBoleta as Boleta,
       f_TotalAreaExtensionFinca as Extension_Total, f_TotalAreaDedicadaFinca 
       as Area_Dedicada_Finca, f_TotalAreaTenenciaFinca as Tenencia_Finca
      from Fnc_TenenciaUsoTierra
      where (f_TotalAreaExtensionFinca <> f_TotalAreaDedicadaFinca OR f_TotalAreaExtensionFinca <> f_TotalAreaTenenciaFinca) 
      AND  Fnc_TenenciaUsoTierra.c_Fk_IdBoleta = @id_Boleta


UNION 

SELECT 'Pregunta (12) El área sembrada es mayor al área dedicada a cultivos' as Descripcion_Error,
   c_Fk_IdBoleta as Boleta,
   (SELECT SUM(f_AreaDedicadaCultivos)
      FROM Fnc_TenenciaUsoTierra
      WHERE c_Fk_IdBoleta = sembrado.c_Fk_IdBoleta)
   AS AreaDedicadaCultivos,
   SUM(sembrado.f_AreaSiembra) as AreaSembrada
FROM
   Clt_Sembrado as sembrado
WHERE
   sembrado.c_Fk_IdBoleta = 45550711
GROUP BY sembrado.c_Fk_IdBoleta
HAVING SUM(sembrado.f_AreaSiembra) > (SELECT SUM(f_AreaDedicadaCultivos)
      FROM Fnc_TenenciaUsoTierra
      WHERE c_Fk_IdBoleta = sembrado.c_Fk_IdBoleta)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空宴 2024-12-16 06:02:07

问题在于查询每个部分的列数,而不是结果数。顶部有 5 列

  1. Desripcion_Error、
  2. Boleta
  3. Extension_Total、
  4. Area_Dedicada_Finca、
  5. Tenencia_Finca

,底部有 4 列。

  1. Desripcion_Error、
  2. Boleta、
  3. AreaDedicadaCultivos、
  4. AreaSembrada

要将这些返回到一个结果集中,必须有相同数量的列,并且相同序数位置的列需要具有兼容的数据类型。

目前尚不清楚您想要的结果应该是什么样子。如果在第二个查询中没有合适的列要添加,那么您可以添加常量表达式来代替其中一列。在这种情况下,您可能需要 UNION ALL 而不是 UNION (两者之间的区别在于 UNION 添加了额外的重复删除步骤)或也许它们应该作为两个单独的结果返回到您的应用程序中。

The problem is the number of columns in each part of the query, not the number of results. You have 5 columns in the top part

  1. Descripcion_Error,
  2. Boleta
  3. Extension_Total,
  4. Area_Dedicada_Finca,
  5. Tenencia_Finca

and 4 in the bottom.

  1. Descripcion_Error,
  2. Boleta,
  3. AreaDedicadaCultivos,
  4. AreaSembrada

To bring these back in one result set there must be the same number of the columns and the columns in the same ordinal positions need to have compatible datatypes.

It is not clear what your desired results should look like. If there is no suitable column to add in the second query then you could add a constant expression in place of one of the columns. In that case you would probably want UNION ALL rather than UNION (the difference between the two being that UNION adds an additional duplicate removal step) or perhaps they should just be brought back as two separate results to your application.

输什么也不输骨气 2024-12-16 06:02:07

UNION 每一侧的列数必须相同。您始终可以添加虚拟列来到达那里 - 并用 NULL 或您选择的“不适用”值填充它们。

SELECT 'Pregunta (12) El área sembrada es mayor al área dedicada a cultivos' as Descripcion_Error,
   c_Fk_IdBoleta as Boleta,
   (SELECT SUM(f_AreaDedicadaCultivos)
      FROM Fnc_TenenciaUsoTierra
      WHERE c_Fk_IdBoleta = sembrado.c_Fk_IdBoleta)
   AS AreaDedicadaCultivos,
   SUM(sembrado.f_AreaSiembra) as AreaSembrada, 
   NULL as Tenencia_Finca
FROM Clt_Sembrado as sembrado

You have to have the same number of columns on each side of the UNION. You can always add dummy columns to get there - and fill them with NULLs or the "not applicable" value of your choice.

SELECT 'Pregunta (12) El área sembrada es mayor al área dedicada a cultivos' as Descripcion_Error,
   c_Fk_IdBoleta as Boleta,
   (SELECT SUM(f_AreaDedicadaCultivos)
      FROM Fnc_TenenciaUsoTierra
      WHERE c_Fk_IdBoleta = sembrado.c_Fk_IdBoleta)
   AS AreaDedicadaCultivos,
   SUM(sembrado.f_AreaSiembra) as AreaSembrada, 
   NULL as Tenencia_Finca
FROM Clt_Sembrado as sembrado
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文