SELECT
co.id,
co.title,
co.description,
co.created_at,
co.updated_at,
COALESCE(
SUM(
CASE
WHEN t.type = 'INCOME' THEN t.amount
ELSE 0
END
) - SUM(
CASE
WHEN t.type = 'EXPENSE' THEN t.amount
ELSE 0
END
),
0.00
) AS total_amount,
COUNT(t),
array_to_json(ARRAY_AGG(DISTINCT to_jsonb(c))) AS categories,
array_to_json(ARRAY_AGG(DISTINCT to_jsonb(t))) AS transactions
FROM
companies co
LEFT JOIN LATERAL (
SELECT
DISTINCT t.id,
t.description,
t.amount,
t.created_at,
t.type
FROM
company_transactions t
WHERE
t.company_id = co.id
) t ON TRUE
LEFT JOIN LATERAL (
SELECT
c.id,
c.title,
COALESCE(
SUM(
CASE
WHEN tr.type = 'INCOME' THEN tr.amount
ELSE 0
END
) - SUM(
CASE
WHEN tr.type = 'EXPENSE' THEN tr.amount
ELSE 0
END
),
0.00
) AS total_amount
FROM
company_categories c
LEFT JOIN company_transactions tr ON c.id = tr.category_id
WHERE
c.company_id = co.id
GROUP BY
c.id
) c ON TRUE
WHERE
co.id = 17
GROUP BY
co.id
export interface ICompanyDetails
extends Pick<
ICompany,
'id' | 'title' | 'description' | 'created_at' | 'updated_at'
> {
total_amount: number;
categories: {
id: number;
title: string;
total_amount: number;
}[];
transactions: Pick<
ICompanyTransaction,
'id' | 'description' | 'amount' | 'created_at' | 'type'
>[];
}
export interface ICompany {
id: number;
owner: IUser;
title: string;
description: string;
created_at: Date;
updated_at: Date;
deleted_at: Date | null;
categories: ICompanyCategory[];
transactions: ICompanyTransaction[];
}
export interface ICompanyCategory {
id: number;
creator: IUser;
company: ICompany;
title: string;
description: string;
created_at: Date;
updated_at: Date;
transactions: ICompanyTransaction[];
}
export enum CompanyTransactionEnum {
INCOME = 'INCOME',
EXPENSE = 'EXPENSE',
}
export interface ICompanyTransaction {
id: number;
creator: IUser;
company: ICompany;
category: ICompanyCategory;
description: string;
amount: number;
type: CompanyTransactionEnum;
maked_at: Date;
created_at: Date;
updated_at: Date;
}
array_to_json(ARRAY_AGG(DISTINCT to_jsonb(t))) AS transactions
если его убрать - будет выводиться 12.