与选择列表中不同数量的列联合?
我有这个疑问,但我需要为所有人建立一个联盟。但是每个查询在选择列表中都有不同数量的列,这给了我错误:
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于查询每个部分的列数,而不是结果数。顶部有 5 列
,底部有 4 列。
要将这些返回到一个结果集中,必须有相同数量的列,并且相同序数位置的列需要具有兼容的数据类型。
目前尚不清楚您想要的结果应该是什么样子。如果在第二个查询中没有合适的列要添加,那么您可以添加常量表达式来代替其中一列。在这种情况下,您可能需要
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
and 4 in the bottom.
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 thanUNION
(the difference between the two being thatUNION
adds an additional duplicate removal step) or perhaps they should just be brought back as two separate results to your application.UNION 每一侧的列数必须相同。您始终可以添加虚拟列来到达那里 - 并用 NULL 或您选择的“不适用”值填充它们。
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.