SELECT * FROM (SELECT N, KJQJ, CODE_ORG, ZZNAME, GYSNAME, GYSBM, DJLX, NVL(BAL_ORI, LAG(BAL_ORI IGNORE NULLS) OVER(ORDER BY 1)) BAL_ORI, NVL(BAL_LOC, LAG(BAL_LOC IGNORE NULLS) OVER(ORDER BY 1)) BAL_LOC FROM (SELECT N, KJQJ, CODE_ORG, ZZNAME, GYSNAME, GYSBM, DJLX, CASE WHEN N = 1 AND BAL_ORI IS NULL THEN 1234567890 ELSE BAL_ORI END BAL_ORI, CASE WHEN N = 1 AND BAL_LOC IS NULL THEN 1234567890 ELSE BAL_LOC END BAL_LOC FROM T_PAYABLE_1_2 T)) TT WHERE TT.BAL_ORI != 1234567890 AND TT.BAL_LOC != 1234567890;
时间: 2024-02-10 12:24:29 浏览: 70
离散数学课后题答案+sdut往年试卷+复习提纲资料
在MySQL中,你可以使用IFNULL函数和子查询来模拟LAG函数和IGNORE NULLS选项的行为。以下是将NVL、LAG和IGNORE NULLS转换为MySQL的示例:
```sql
SELECT *
FROM (
SELECT N,
KJQJ,
CODE_ORG,
ZZNAME,
GYSNAME,
GYSBM,
DJLX,
IF(BAL_ORI = 1234567890,
(SELECT BAL_ORI FROM T_PAYABLE_1_2 t2
WHERE t2.N < T.N AND t2.BAL_ORI != 1234567890
ORDER BY t2.N DESC LIMIT 1),
BAL_ORI) AS BAL_ORI,
IF(BAL_LOC = 1234567890,
(SELECT BAL_LOC FROM T_PAYABLE_1_2 t2
WHERE t2.N < T.N AND t2.BAL_LOC != 1234567890
ORDER BY t2.N DESC LIMIT 1),
BAL_LOC) AS BAL_LOC
FROM T_PAYABLE_1_2 T
) TT
WHERE TT.BAL_ORI != 1234567890
AND TT.BAL_LOC != 1234567890;
```
在上述查询中,我们使用子查询来获取前一个非空值。子查询从同一表中选择符合条件的最大值,即小于当前行值的最大值,并且该值不等于1234567890。然后,我们使用IF函数来检查列的值是否为1234567890,并根据需要返回前一个非空值或原始值。
请注意,上述示例中的"T_PAYABLE_1_2"应替换为您实际使用的表名。同时,这里假设你的表中有一个"N"列用于排序,你可以根据实际情况调整ORDER BY子句和LIMIT子句来适应你的需求。
阅读全文