Мне трудно создать этот запрос. У меня есть база данных с тысячами записей, и я хочу создавать отчеты. Моя база данных такова:
Одна из записей должна получить общую сумму всех квитанций и счетов в определенной компании. Поэтому я попытался написать этот запрос:
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;
И это работает нормально. Он показывает мне данные immidiately.this это результат:
Но когда я пытаюсь сделать СУММУ для invoice_total и receive_total (потому что я хочу, чтобы оборот этой компании за определенный период c Я ничего не получаю. Вот что я пишу:
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";
Так что мой последний w sh - получить СУММУ всех счета-фактуры и квитанции определенной компании за определенный период:)
I wi sh в результате получится всего одна ячейка с конечным значением
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();