【发布时间】:2023-03-05 23:13:01
【问题描述】:
我很难创建此查询。我有一个包含数千条记录的数据库,我想创建报告。 我的数据库是这样的:
其中一条记录必须是某家公司所有收据和发票的总金额。 所以我试着写这个查询:
SELECT *
FROM (
SELECT r.receipt_total, i.invoice_total, s.store_id, c.company_id
FROM receipts r
INNER JOIN invoices i on r.store_id = i.store_id
INNER JOIN stores s on r.store_id = s.store_id
INNER JOIN companies c on c.company_id=s.company_id ) t1;
这工作正常。它立即向我显示数据。这是结果:
但是当我尝试对 invoice_total 和 receipt_total 进行 SUM 时(因为我想要这家公司在特定时期的营业额),我什么也得不到。这是我写的:
SELECT *, (SUM(t1.invoice_total) + SUM(t1.receipt_total)) AS TOTAL
FROM (
SELECT r.receipt_total, r.receipt_datetime, i.invoice_datetime, i.invoice_total,
s.store_id, c.company_id, c.company_name
FROM receipts r
INNER JOIN invoices i on r.store_id = i.store_id
INNER JOIN stores s on r.store_id = s.store_id
INNER JOIN companies c on c.company_id=s.company_id
WHERE c.company_name= "Cogidoo"
AND DATE(invoice_datetime) between "01-01-2019" and "31-03-2019"
AND DATE(receipt_datetime) between "01-01-2019" and "31-03-2019") t1;
我也试过了,结果还是没有:
SELECT (SUM(receipt_total) + SUM(invoice_total)) AS SUM
FROM (
SELECT r.receipt_total, r.receipt_datetime, i.invoice_total, i.invoice_datetime, s.store_id,
c.company_id, c.company_name
FROM receipts r
INNER JOIN invoices i on r.store_id = i.store_id
INNER JOIN stores s on r.store_id = s.store_id
INNER JOIN companies c on c.company_id=s.company_id ) t1
WHERE t1.company_name = "Jamia"
AND DATE(t1.receipt_datetime) BETWEEN "01-01-2019" AND "31-03-2019"
AND DATE(t1.invoice_datetime) BETWEEN "01-01-2019" AND "31-03-2019";
所以我最后的愿望是得到某家公司在一定时期内的所有发票和收据的总和:)
我希望结果只是一个具有最终值的单元格
PS:添加更多信息 这就是我在 Java 中创建表的方式。卡片详细信息和客户表与想要的结果完全无关。我插入数据也没有问题。
dsl.createTableIfNotExists("companies")
.column("company_id",SQLDataType.BIGINT.nullable(false))
.column("company_uuid", SQLDataType.INTEGER.nullable(false))
.column("company_name", SQLDataType.VARCHAR(30).nullable(false))
.column("company_address", SQLDataType.VARCHAR(30).nullable(false))
.constraint(
primaryKey("company_id")
)
.execute();
dsl.createTableIfNotExists("stores")
.column("store_id", SQLDataType.BIGINT.nullable(false))
.column("store_name", SQLDataType.VARCHAR(30).nullable(false))
.column("store_address", SQLDataType.VARCHAR(30).nullable(false))
.column("company_id", SQLDataType.BIGINT.nullable(true))
.constraints(
primaryKey("store_id"),
foreignKey("company_id").references("companies", "company_id"),
unique("store_name")
)
.execute();
dsl.createTableIfNotExists("customers")
.column("customer_id", SQLDataType.BIGINT.nullable(false))
.column("customer_uuid", SQLDataType.INTEGER.nullable(false))
.column("customer_name", SQLDataType.VARCHAR(30).nullable(false))
.column("customer_address", SQLDataType.VARCHAR(30).nullable(false))
.constraints(
primaryKey("customer_id"),
unique ("customer_name")
)
.execute();
dsl.createTableIfNotExists("carddetails")
.column("card_id", SQLDataType.BIGINT.nullable(false))
.column("card_number", SQLDataType.VARCHAR(30).nullable(false))
.column("card_type", SQLDataType.VARCHAR(30).nullable(false))
.column("card_contactless", SQLDataType.BOOLEAN.nullable(false))
.constraints(
primaryKey("card_id"),
unique ("card_number")
)
.execute();
dsl.createTableIfNotExists("receipts")
.column("receipt_id", SQLDataType.BIGINT.nullable(false))
.column("receipt_total", SQLDataType.DOUBLE.nullable(false))
.column("receipt_datetime", SQLDataType.TIMESTAMP(0).nullable(false))
.column("receipt_payment", SQLDataType.VARCHAR(4).nullable(false))
.column("store_id", SQLDataType.BIGINT)
.column("card_id", SQLDataType.BIGINT.nullable(true))
.constraints(
primaryKey("receipt_id"),
foreignKey("store_id").references("stores", "store_id"),
foreignKey("card_id").references("carddetails", "card_id")
)
.execute();
dsl.createTableIfNotExists("invoices")
.column("invoice_id", SQLDataType.BIGINT.nullable(false))
.column("invoice_total", SQLDataType.DOUBLE.nullable(false))
.column("invoice_datetime", SQLDataType.TIMESTAMP(0).nullable(false))
.column("invoice_payment", SQLDataType.VARCHAR(4).nullable(false))
.column("customer_id", SQLDataType.BIGINT)
.column("store_id", SQLDataType.BIGINT)
.column("card_id", SQLDataType.BIGINT)
.constraints(
primaryKey("invoice_id"),
foreignKey("store_id").references("stores", "store_id"),
foreignKey("card_id").references("carddetails", "card_id"),
foreignKey("customer_id").references("customers", "customer_id")
)
.execute();
【问题讨论】:
-
请以表格文本的形式向我们展示您当前的结果和您期望的结果。
-
“结果仍然没有”不会帮助我们帮助您。什么都没有???没有答案?错误的答案?空值?最重要的是。正确答案是什么。
-
这里是如何提问的指南。 stackoverflow.com/help/how-to-ask
-
对不起:/ 第一次在这里发帖,我发现我的解释不是很好。我的意思是结果是NULL。我只想得到一个单列单行,其中包含每张收据和发票的总和。
-
请阅读meta.stackoverflow.com/questions/333952/…并编辑您的问题以提供更多信息