我们应该如何重新处理这个查询,我没有找到删除每个组的第二行的解决方案。是否有一个函数或其他东西只能为带有 rolllup 的组返回不同的值(这里不能使用不同的值)
SELECT
-- ROW_NUMBER() over (ORDER BY FF_DATE asc) as "numSituation"
FF_NUMERO as "numFacture"
, FF_DATE as "dateFacture"
, FF_DATEREG as "dateReglement"
, SUM(FF_D_HT) as "montantHT"
, SUM(FF_D_TTC) as "montantTTC"
FROM F_FACTURESFOU
INNER JOIN F_LIGNEFACFOU ON LFF_FFPTR = FF_NUMERO
INNER JOIN F_LOT ON LFF_LT_IDENT = LT_IDENT
WHERE LFF_CODE_AFF = '14031-00' AND LT_CODE IN ('S83') AND FF_DATEVALID is not null
GROUP BY FF_NUMERO , FF_DATE, FF_DATEREG WITH ROLLUP
HAVING ( ( FF_NUMERO is not null and FF_DATE IS NOT NULL ) OR ( FF_NUMERO is null and FF_DATE is null ))
ORDER BY GROUPING(FF_NUMERO ), FF_NUMERO, GROUPING ( FF_DATE), FF_DATE , GROUPING ( FF_DATEREG), FF_DATEREG
这是结果:
15E1500062 2015-07-24 00:00:00.000 NULL 3000.00 3600.00
15E1500062 2015-07-24 00:00:00.000 NULL 3000.00 3600.00
15E1500063 2015-07-31 00:00:00.000 NULL 6000.00 7200.00
15E1500063 2015-07-31 00:00:00.000 NULL 6000.00 7200.00
15E1600423 2015-09-11 00:00:00.000 NULL 3000.00 3600.00
15E1600423 2015-09-11 00:00:00.000 NULL 3000.00 3600.00
NULL NULL NULL 12000.00 14400.00
您可以将 SQL Row_Number() 函数与 Partition By 子句一起使用 您可以对由 Partition By 子句定义的每个组内的结果集中的行进行编号
然后您可以排除 row_number 值大于或等于 2 的所有行
请查看 SQL 教程 Row_Number() Over Partition By 以删除 SQL Server 中的重复行
ROW_NUMBER() over (Partition By FF_NUMERO, FF_DATE ORDER BY
您可以使用
SQL CTE表达式如下
;with cte as (
SELECT
ROW_NUMBER() over (Partition By FF_NUMERO, FF_DATE ORDER BY .... DESC) as rn,
....
FROM ....
)
SELECT * from cte where rn = 1