LOOP AT GT_ZSDT0039_A INTO GT_ZSDT0039_C. COLLECT GT_ZSDT0039_A INTO GT_ZSDT0039_C WHERE CODE = GT_ZSDT0039_C-CODE AGGREGATE DJ = SUM ( GT_ZSDT0039_C-DJ ) . ENDLOOP. WHERE 报错
时间: 2023-08-09 15:12:15 浏览: 89
在 ABAP 中,`WHERE` 关键字只能在 `SELECT` 语句中使用,不能在 `LOOP` 语句中使用。如果您想要筛选出符合条件的数据,可以使用 `IF` 语句进行判断和处理。例如:
```
LOOP AT gt_zsdt0039_a INTO gs_zsdt0039_a.
IF gs_zsdt0039_a-code = gs_zsdt0039_c-code.
gs_zsdt0039_c-dj = gs_zsdt0039_c-dj + gs_zsdt0039_a-dj.
COLLECT gs_zsdt0039_a INTO gt_zsdt0039_c.
ENDIF.
ENDLOOP.
```
这样就可以将 `gt_zsdt0039_a` 中 `CODE` 字段与 `gt_zsdt0039_c` 中相同的数据行进行求和并存入 `DJ` 字段,然后将符合条件的数据行加入到 `gt_zsdt0039_c` 中。
相关问题
dwm_dws_cbsjbz 小表,dwm_sap_zsdt066大表 SELECT ZYEAR,ZMONTH,ZZCPXLT,SUM(VVD02) VVD02 FROM( select a.ZYEAR, a.ZMONTH, a.KAUFN, a.KDPOS, a.MATNR, a.AUART, a.VVD02, c.ZZCPXLT from dwm_dws_cbsjbz a inner join dwm_sap_zsdt066 c on a.KAUFN = c.VBELN and a.KDPOS = c.POSNR where a.AUART = 'OR1' AND a.ZYEAR = '2023' and a.ZMONTH = '04' ) AS SFDF GROUP BY ZYEAR,ZMONTH,ZZCPXLT 优化这个语句
这个语句的优化可以从以下几个方面入手:
1. 添加索引:在dwm_dws_cbsjbz表的KAUFN和KDPOS列上添加联合索引,以及在dwm_sap_zsdt066表的VBELN和POSNR列上添加联合索引,可以提高连接查询的效率。
2. 使用子查询:将连接查询的结果作为子查询,避免了大表和小表的直接连接,减少了计算量。
优化后的SQL语句如下:
```
SELECT ZYEAR, ZMONTH, ZZCPXLT, SUM(VVD02) VVD02
FROM(
SELECT a.ZYEAR, a.ZMONTH, a.VVD02, c.ZZCPXLT
FROM dwm_dws_cbsjbz a
INNER JOIN (
SELECT VBELN, POSNR, ZZCPXLT
FROM dwm_sap_zsdt066
WHERE VBELN IN (
SELECT KAUFN
FROM dwm_dws_cbsjbz
WHERE AUART = 'OR1' AND ZYEAR = '2023' AND ZMONTH = '04'
)
) c ON a.KAUFN = c.VBELN AND a.KDPOS = c.POSNR
WHERE a.AUART = 'OR1' AND a.ZYEAR = '2023' AND a.ZMONTH = '04'
) AS SFDF
GROUP BY ZYEAR, ZMONTH, ZZCPXLT;
```
以上是一种可能的优化方式,具体实现还需要根据你的实际情况进行调整。
SELECT ZYEAR,ZMONTH,ZZCPXLT,SUM(VVD02) VVD02 FROM( SELECT a.ZYEAR, a.ZMONTH, a.KAUFN, a.KDPOS, a.MATNR, a.AUART, a.VVD02, c.ZZCPXLT FROM dwm_dws_cbsjbz a left JOIN ( SELECT VBELN, POSNR, ZZCPXLT FROM dwm_sap_zsdt066 WHERE VBELN IN ( SELECT KAUFN FROM dwm_dws_cbsjbz WHERE AUART = 'OR1' AND ZYEAR = '2023' AND ZMONTH = '04' ) ) c ON a.KAUFN = c.VBELN AND a.KDPOS = c.POSNR WHERE a.AUART = 'OR1' AND a.ZYEAR = '2023' AND a.ZMONTH = '04' ) AS SFDF GROUP BY ZYEAR,ZMONTH,ZZCPXLT 为关键字ZZCPXLT 是NULL的赋值为无系列
您可以使用如下SQL语句将关键字ZZCPXLT是NULL的赋值为“无系列”:
```
SELECT
ZYEAR,
ZMONTH,
NVL(ZZCPXLT,'无系列') AS ZZCPXLT,
SUM(VVD02) VVD02
FROM(
SELECT
a.ZYEAR,
a.ZMONTH,
a.KAUFN,
a.KDPOS,
a.MATNR,
a.AUART,
a.VVD02,
c.ZZCPXLT
FROM
dwm_dws_cbsjbz a
LEFT JOIN (
SELECT VBELN, POSNR, ZZCPXLT
FROM dwm_sap_zsdt066
WHERE VBELN IN (
SELECT KAUFN
FROM dwm_dws_cbsjbz
WHERE AUART = 'OR1'
AND ZYEAR = '2023'
AND ZMONTH = '04'
)
) c ON a.KAUFN = c.VBELN AND a.KDPOS = c.POSNR
WHERE
a.AUART = 'OR1'
AND a.ZYEAR = '2023'
AND a.ZMONTH = '04'
) AS SFDF
GROUP BY ZYEAR, ZMONTH, ZZCPXLT;
```
在查询结果中,关键字ZZCPXLT为NULL的记录将会被替换为“无系列”并显示出来。
阅读全文