sqlite Sqllite嵌套sum语句为数据库中不重复的记录返回错误的值

irtuqstp  于 2023-01-21  发布在  SQLite
关注(0)|答案(1)|浏览(121)

我试图得到数据库中计数产品的总和,当我使用嵌套求和语句的结果是错误的一个记录是不重复的。我的代码:

INSERT OR IGNORE INTO COUNT_REPORT
                (
                PRODUCT_CODE, 
                BRANCH, 
                WAREHOUSE, 
                PRODUCT_NAME, 
                CURRENT_COUNT, 
                PREVIOUS_COUNT,
                DIFFERENCE,
                STATE,
                DATE,
                PRICE
                )
                    SELECT
                    PROD_CODE,
                    (SELECT UNVAN FROM BRANCHES WHERE SUBE_KODU = BRANCH_CODE), 
                    (SELECT DEPO_ISMI FROM WAREHOUSES WHERE DEPO_KODU = WARE_CODE),
                    (SELECT STOK_ADI FROM PRODUCT_DETAILS WHERE STOK_KODU = PROD_CODE),
                    (SELECT SUM(QUANTITY) FROM FINAL_DATA GROUP BY PROD_CODE),
                    PREVIOUS_COUNT,
                    DIFFERENCE,
                    STATE,
                    TASK_DATE,
                    PRICE
                    FROM FINAL_DATA
                    WHERE TASK_ID = ?

我尝试使用DISTINCT和GROUP BY以及另一个嵌套语句。我期望查询返回重复记录中数量的总和。

jogvjijk

jogvjijk1#

问题是在同一个表中执行两次子查询,因此:

(SELECT SUM(QUANTITY) FROM FINAL_DATA GROUP BY PROD_CODE)

我使用了:

SUM(QUANTITY)

它现在正按预期工作。

相关问题