SQL 查询 - 表达式 > 0

SQL query - having expression > 0

我正在使用 Microsoft SQL Server 2014,我有以下 SQL 有效的查询:

SELECT 
    h.entidade, h.datadoc, h.tipodoc, h.numdoc,
    (SELECT valortotal -
         (SELECT COALESCE(SUM(l.valorrec), 0) 
          FROM LinhasLiq as l
          INNER JOIN cabliq ON l.IdCabLiq = CabLiq.id
          INNER JOIN Historico ON L.IdHistorico = Historico.id
          WHERE cabliq.DataDoc < '01/05/2015'
            AND historico.id = h.id)) as 'valor pendente',  
    coalesce(documentosCCT.Descricao,'')
+' '+ CASE WHEN h.modulo<>'V' THEN coalesce(documentosVenda.Descricao,'') else'' END 
+' '+  coalesce(h.descricao,'') AS descricaogeral   
FROM 
    Historico h 
LEFT JOIN 
    documentosCCT ON h.TipoDoc = documentosCCT.Documento  
LEFT JOIN 
    documentosVenda ON h.Tipodoc = documentosVenda.Documento
WHERE
    h.entidade = 'ta0141' AND
    (h.tipoentidade = 'C' OR h.tipoentidade = 'F') 
ORDER BY 
    datadoc ASC

和这个特定的表达式

(select valortotal - (SELECT COALESCE(SUM(l.valorrec),0) from LinhasLiq as l
    inner join cabliq on l.IdCabLiq = CabLiq.id
    inner join Historico on L.IdHistorico = Historico.id
    where cabliq.DataDoc < '01/05/2015' and historico.id = h.id)) as 'valor pendente'

returns 很多 0 值,所以我如何将整个表达式放在 having X > 0 子句中,或者任何其他方式,只要具有此表达式 = 0 的行不显示?

非常感谢。

我不知道你的情况如何 尝试 在单个 select 语句而不是多个语句中进行查询 不要像 valor pendente 这样的别名应该是 valorpendente

现在您可以使用 CTE

;WITH CTE AS
(
-- YOUR QUERY
)

SELECT * FROM CTE
WHERE valorpendente > 0