DB2 使用 SUM 函数导致 SQL 错误 [S1000]: 一般错误
DB2 using SUM function causes SQL Error [S1000]: General error
我正在尝试收集项目的销售数据,但我在主查询中用作 cte 的查询出现问题。
SELECT
CONCAT(
CONCAT(
VARCHAR_FORMAT(INFO.DATE, 'YYYYMMDD'),
CAST(INFO.INVOICE AS VARCHAR)
),
CAST(INFO.STORE AS VARCHAR)
) AS HEADER,
CASE WHEN INFO.CUSTOMER_NUMBER IN (999990, 999991)
THEN 'CASH' ELSE 'CHARGE' END AS CUST_TYPE,
INFO.STORE,
INFO.INVOICE,
INFO.DATE,
INFO.SALES_UNITS,
INFO.SALES
FROM
DATALAKE.SLSINFO INFO
WHERE
INFO.DATE >= '2019-01-01'
AND INFO.STORE = 163
AND INFO.INVOICE = 100349
ORDER BY
HEADER ASC
此查询应该创建一个唯一的 header 然后使用客户编号将发票分类为现金客户或收费客户以及 return 一些相关值。
在这种情况下,我正在查看特定的 date/store/invoice 进行测试,结果是:
HEADER
CUST_TYPE
STORE
INVOICE
DATE
SALES_UNITS
SALES
20190101100349163
CASH
163
100349
2019-01-01
4
9.96
20190101100349163
CASH
163
100349
2019-01-01
1
10.99
20190101100349163
CASH
163
100349
2019-01-01
1
2.99
我试图通过添加 SALES_UNITS 和 SALES 列来为每个唯一 header 创建一行。我尝试使用的查询是这样的:
SELECT
CONCAT(CONCAT(VARCHAR_FORMAT(INFO.DATE, 'YYYYMMDD'), CAST(INFO.INVOICE AS VARCHAR)), CAST(INFO.STORE AS VARCHAR)) AS HEADER,
CASE WHEN INFO.CUSTOMER_NUMBER IN (999990, 999991) THEN 'CASH' ELSE 'CHARGE' END AS CUST_TYPE,
INFO.STORE,
INFO.INVOICE,
INFO.DATE,
SUM(INFO.SALES_UNITS) AS UNITS,
SUM(INFO.SALES) AS SALES
FROM
DATALAKE.SLSINFO INFO
WHERE
INFO.DATE >= '2019-01-01'
AND INFO.STORE = 163
AND INFO.INVOICE = 100349
ORDER BY
HEADER ASC
理想的输出应该是这样的
HEADER
CUST_TYPE
STORE
INVOICE
DATE
UNITS
SALES
20190101100349163
CASH
163
100349
2019-01-01
6
23.94
然而,第二个查询没有 运行,我收到 SQL 错误 [S1000]:一般错误,没有其他信息。
我已经尝试在原始查询和第二个查询中按 header 分组,但它不允许我按任何分组,我还没有弄清楚为什么会这样。
任何见解将不胜感激!
您可以使用 with 子句为您的原始查询命名(我称之为 T,然后只需获取您的总和并按其他列分组。
With T as (
SELECT
CONCAT(
CONCAT(
VARCHAR_FORMAT(INFO.DATE, 'YYYYMMDD'),
CAST(INFO.INVOICE AS VARCHAR)
),
CAST(INFO.STORE AS VARCHAR)
) AS HEADER,
CASE WHEN INFO.CUSTOMER_NUMBER IN (999990, 999991) THEN 'CASH' ELSE 'CHARGE' END AS CUST_TYPE,
INFO.STORE,
INFO.INVOICE,
INFO.DATE,
INFO.SALES_UNITS,
INFO.SALES
FROM
DATALAKE.SLSINFO INFO
WHERE
INFO.DATE >= '2019-01-01'
AND INFO.STORE = 163
AND INFO.INVOICE = 100349
)
select
Header,
cust_type,
store,
invoice,
date,
SUM(SALES_UNITS) AS UNITS,
SUM(SALES) AS SALES
from
t
group by
Header,
cust_type,
store,
invoice,
date
order by
Header
我正在尝试收集项目的销售数据,但我在主查询中用作 cte 的查询出现问题。
SELECT
CONCAT(
CONCAT(
VARCHAR_FORMAT(INFO.DATE, 'YYYYMMDD'),
CAST(INFO.INVOICE AS VARCHAR)
),
CAST(INFO.STORE AS VARCHAR)
) AS HEADER,
CASE WHEN INFO.CUSTOMER_NUMBER IN (999990, 999991)
THEN 'CASH' ELSE 'CHARGE' END AS CUST_TYPE,
INFO.STORE,
INFO.INVOICE,
INFO.DATE,
INFO.SALES_UNITS,
INFO.SALES
FROM
DATALAKE.SLSINFO INFO
WHERE
INFO.DATE >= '2019-01-01'
AND INFO.STORE = 163
AND INFO.INVOICE = 100349
ORDER BY
HEADER ASC
此查询应该创建一个唯一的 header 然后使用客户编号将发票分类为现金客户或收费客户以及 return 一些相关值。
在这种情况下,我正在查看特定的 date/store/invoice 进行测试,结果是:
HEADER | CUST_TYPE | STORE | INVOICE | DATE | SALES_UNITS | SALES |
---|---|---|---|---|---|---|
20190101100349163 | CASH | 163 | 100349 | 2019-01-01 | 4 | 9.96 |
20190101100349163 | CASH | 163 | 100349 | 2019-01-01 | 1 | 10.99 |
20190101100349163 | CASH | 163 | 100349 | 2019-01-01 | 1 | 2.99 |
我试图通过添加 SALES_UNITS 和 SALES 列来为每个唯一 header 创建一行。我尝试使用的查询是这样的:
SELECT
CONCAT(CONCAT(VARCHAR_FORMAT(INFO.DATE, 'YYYYMMDD'), CAST(INFO.INVOICE AS VARCHAR)), CAST(INFO.STORE AS VARCHAR)) AS HEADER,
CASE WHEN INFO.CUSTOMER_NUMBER IN (999990, 999991) THEN 'CASH' ELSE 'CHARGE' END AS CUST_TYPE,
INFO.STORE,
INFO.INVOICE,
INFO.DATE,
SUM(INFO.SALES_UNITS) AS UNITS,
SUM(INFO.SALES) AS SALES
FROM
DATALAKE.SLSINFO INFO
WHERE
INFO.DATE >= '2019-01-01'
AND INFO.STORE = 163
AND INFO.INVOICE = 100349
ORDER BY
HEADER ASC
理想的输出应该是这样的
HEADER | CUST_TYPE | STORE | INVOICE | DATE | UNITS | SALES |
---|---|---|---|---|---|---|
20190101100349163 | CASH | 163 | 100349 | 2019-01-01 | 6 | 23.94 |
然而,第二个查询没有 运行,我收到 SQL 错误 [S1000]:一般错误,没有其他信息。
我已经尝试在原始查询和第二个查询中按 header 分组,但它不允许我按任何分组,我还没有弄清楚为什么会这样。
任何见解将不胜感激!
您可以使用 with 子句为您的原始查询命名(我称之为 T,然后只需获取您的总和并按其他列分组。
With T as (
SELECT
CONCAT(
CONCAT(
VARCHAR_FORMAT(INFO.DATE, 'YYYYMMDD'),
CAST(INFO.INVOICE AS VARCHAR)
),
CAST(INFO.STORE AS VARCHAR)
) AS HEADER,
CASE WHEN INFO.CUSTOMER_NUMBER IN (999990, 999991) THEN 'CASH' ELSE 'CHARGE' END AS CUST_TYPE,
INFO.STORE,
INFO.INVOICE,
INFO.DATE,
INFO.SALES_UNITS,
INFO.SALES
FROM
DATALAKE.SLSINFO INFO
WHERE
INFO.DATE >= '2019-01-01'
AND INFO.STORE = 163
AND INFO.INVOICE = 100349
)
select
Header,
cust_type,
store,
invoice,
date,
SUM(SALES_UNITS) AS UNITS,
SUM(SALES) AS SALES
from
t
group by
Header,
cust_type,
store,
invoice,
date
order by
Header