如何设计 select 价格的查询,其中 sum(tax) 小于 price in to different table (tableA,tableB)

how to design query for select price where sum(tax) less than price in to different table (tableA,tableB)

如何为 select price 设计查询,其中 sum(tax) 小于 price 到不同的 table (tableA, tableB)

请检查以下SQL Select 声明

;with cte as (
    select 
        a.id, a.tax, a.idprice, b.price, 
        sum(tax) over (partition by a.idprice) sum_tax
    from tableA a
    inner join tableB b
    on a.idprice = b.idprice
)
select * from cte
where sum_tax < price

首先我使用了SQL CTE (Common Table Expression) query 我使用 CTE 是因为我在 WHERE 子句中使用了求和列。 您还可以将相同的 SELECT 语句放在子查询中,而不是 SQL CTE 表达式

关于上面的查询一个有趣的事情,也许你是第一次看到它;我将 SUM() 聚合函数与 Partition By 子句一起使用 就像 SQL Count() Over (Partition By) 子句一样,分区依据使您能够对共享相同值的行应用聚合函数