1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-09 15:35:22 +02:00

fix sql formatting

This commit is contained in:
Tyler Myracle 2024-01-19 19:43:48 -06:00
parent 25cdf0c1be
commit c1f9664c5e

View file

@ -117,32 +117,32 @@ export class AccountQueryService implements IAccountQueryService {
} }
>( >(
sql` sql`
SELECT SELECT
h.id, h.id,
h.security_id, h.security_id,
s.name, s.name,
s.symbol, s.symbol,
s.shares_per_contract, s.shares_per_contract,
he.quantity, he.quantity,
he.value, he.value,
he.cost_basis, he.cost_basis,
h.cost_basis_user, h.cost_basis_user,
h.cost_basis_provider, h.cost_basis_provider,
he.cost_basis_per_share, he.cost_basis_per_share,
he.price, he.price,
he.price_prev, he.price_prev,
he.excluded he.excluded
FROM FROM
holdings_enriched he holdings_enriched he
INNER JOIN security s ON s.id = he.security_id INNER JOIN security s ON s.id = he.security_id
INNER JOIN holding h ON h.id = he.id INNER JOIN holding h ON h.id = he.id
WHERE WHERE
he.account_id = ${accountId} he.account_id = ${accountId}
ORDER BY ORDER BY
he.excluded ASC, he.excluded ASC,
he.value DESC he.value DESC
OFFSET ${page * pageSize} OFFSET ${page * pageSize}
LIMIT ${pageSize}; LIMIT ${pageSize};
` `
) )
@ -204,7 +204,7 @@ export class AccountQueryService implements IAccountQueryService {
) v ) v
ORDER BY ORDER BY
v.date ASC v.date ASC
` `
) )
return rows return rows
@ -220,79 +220,79 @@ export class AccountQueryService implements IAccountQueryService {
const { rows } = await this.pg.pool.query<ReturnSeries>( const { rows } = await this.pg.pool.query<ReturnSeries>(
sql` sql`
WITH start_date AS ( WITH start_date AS (
SELECT SELECT
a.id AS "account_id", a.id AS "account_id",
GREATEST(account_value_start_date(a.id), a.start_date) AS "start_date" GREATEST(account_value_start_date(a.id), a.start_date) AS "start_date"
FROM
account a
WHERE
a.id = ANY(${pAccountIds})
GROUP BY
1
), external_flows AS (
SELECT
it.account_id,
it.date,
SUM(it.amount) AS "amount"
FROM
investment_transaction it
LEFT JOIN start_date sd ON sd.account_id = it.account_id
WHERE
it.account_id = ANY(${pAccountIds})
AND it.date BETWEEN sd.start_date AND ${pEnd}
-- filter for investment_transactions that represent external flows
AND (
(it.plaid_type = 'cash' AND it.plaid_subtype IN ('contribution', 'deposit', 'withdrawal'))
OR (it.plaid_type = 'transfer' AND it.plaid_subtype IN ('transfer'))
OR (it.plaid_type = 'buy' AND it.plaid_subtype IN ('contribution'))
OR (it.finicity_transaction_id IS NOT NULL AND it.finicity_investment_transaction_type IN ('contribution', 'deposit', 'transfer'))
)
GROUP BY
1, 2
), external_flow_totals AS (
SELECT
account_id,
SUM(amount) as "amount"
FROM
external_flows
GROUP BY
1
), balances AS (
SELECT
abg.account_id,
abg.date,
abg.balance,
0 - SUM(COALESCE(ef.amount, 0)) OVER (PARTITION BY abg.account_id ORDER BY abg.date ASC) AS "contributions_period",
COALESCE(-1 * (eft.amount - coalesce(SUM(ef.amount) OVER (PARTITION BY abg.account_id ORDER BY abg.date DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING), 0)), 0) AS "contributions"
FROM
account_balances_gapfilled(
${pStart},
${pEnd},
'1d',
${pAccountIds}
) abg
LEFT JOIN external_flows ef ON ef.account_id = abg.account_id AND ef.date = abg.date
LEFT JOIN external_flow_totals eft ON eft.account_id = abg.account_id
)
SELECT
b.account_id,
b.date,
b.balance,
b.contributions,
b.contributions_period,
COALESCE(ROUND((b.balance - b0.balance - b.contributions_period) / COALESCE(NULLIF(b0.balance, 0), NULLIF(b.contributions_period, 0)), 4), 0) AS "rate_of_return"
FROM
balances b
LEFT JOIN (
SELECT DISTINCT ON (account_id)
account_id,
balance
FROM FROM
balances account a
ORDER BY WHERE
account_id, date ASC a.id = ANY(${pAccountIds})
) b0 ON b0.account_id = b.account_id GROUP BY
1
), external_flows AS (
SELECT
it.account_id,
it.date,
SUM(it.amount) AS "amount"
FROM
investment_transaction it
LEFT JOIN start_date sd ON sd.account_id = it.account_id
WHERE
it.account_id = ANY(${pAccountIds})
AND it.date BETWEEN sd.start_date AND ${pEnd}
-- filter for investment_transactions that represent external flows
AND (
(it.plaid_type = 'cash' AND it.plaid_subtype IN ('contribution', 'deposit', 'withdrawal'))
OR (it.plaid_type = 'transfer' AND it.plaid_subtype IN ('transfer'))
OR (it.plaid_type = 'buy' AND it.plaid_subtype IN ('contribution'))
OR (it.finicity_transaction_id IS NOT NULL AND it.finicity_investment_transaction_type IN ('contribution', 'deposit', 'transfer'))
)
GROUP BY
1, 2
), external_flow_totals AS (
SELECT
account_id,
SUM(amount) as "amount"
FROM
external_flows
GROUP BY
1
), balances AS (
SELECT
abg.account_id,
abg.date,
abg.balance,
0 - SUM(COALESCE(ef.amount, 0)) OVER (PARTITION BY abg.account_id ORDER BY abg.date ASC) AS "contributions_period",
COALESCE(-1 * (eft.amount - coalesce(SUM(ef.amount) OVER (PARTITION BY abg.account_id ORDER BY abg.date DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING), 0)), 0) AS "contributions"
FROM
account_balances_gapfilled(
${pStart},
${pEnd},
'1d',
${pAccountIds}
) abg
LEFT JOIN external_flows ef ON ef.account_id = abg.account_id AND ef.date = abg.date
LEFT JOIN external_flow_totals eft ON eft.account_id = abg.account_id
)
SELECT
b.account_id,
b.date,
b.balance,
b.contributions,
b.contributions_period,
COALESCE(ROUND((b.balance - b0.balance - b.contributions_period) / COALESCE(NULLIF(b0.balance, 0), NULLIF(b.contributions_period, 0)), 4), 0) AS "rate_of_return"
FROM
balances b
LEFT JOIN (
SELECT DISTINCT ON (account_id)
account_id,
balance
FROM
balances
ORDER BY
account_id, date ASC
) b0 ON b0.account_id = b.account_id
` `
) )
@ -313,17 +313,17 @@ export class AccountQueryService implements IAccountQueryService {
const { rows } = await this.pg.pool.query<BalanceSeries>( const { rows } = await this.pg.pool.query<BalanceSeries>(
sql` sql`
SELECT SELECT
abg.account_id, abg.account_id,
abg.date, abg.date,
abg.balance abg.balance
FROM FROM
account_balances_gapfilled( account_balances_gapfilled(
${pStart}, ${pStart},
${pEnd}, ${pEnd},
${pInterval}, ${pInterval},
${pAccountIds} ${pAccountIds}
) abg ) abg
` `
) )
@ -344,14 +344,14 @@ export class AccountQueryService implements IAccountQueryService {
'accountIds' in id 'accountIds' in id
? id.accountIds ? id.accountIds
: sql`( : sql`(
SELECT SELECT
array_agg(a.id) array_agg(a.id)
FROM FROM
account a account a
LEFT JOIN account_connection ac ON ac.id = a.account_connection_id LEFT JOIN account_connection ac ON ac.id = a.account_connection_id
WHERE WHERE
(a.user_id = ${id.userId} OR ac.user_id = ${id.userId}) (a.user_id = ${id.userId} OR ac.user_id = ${id.userId})
AND a.is_active AND a.is_active
)` )`
const pStart = raw(`'${start}'`) const pStart = raw(`'${start}'`)
@ -379,26 +379,26 @@ export class AccountQueryService implements IAccountQueryService {
} }
>( >(
sql` sql`
SELECT SELECT
abg.date, abg.date,
a.category, a.category,
a.classification, a.classification,
SUM(CASE WHEN a.classification = 'asset' THEN abg.balance ELSE -abg.balance END) AS balance SUM(CASE WHEN a.classification = 'asset' THEN abg.balance ELSE -abg.balance END) AS balance
FROM FROM
account_balances_gapfilled( account_balances_gapfilled(
${pStart}, ${pStart},
${pEnd}, ${pEnd},
${pInterval}, ${pInterval},
${pAccountIds} ${pAccountIds}
) abg ) abg
INNER JOIN account a ON a.id = abg.account_id INNER JOIN account a ON a.id = abg.account_id
GROUP BY GROUP BY
GROUPING SETS ( GROUPING SETS (
(abg.date, a.classification, a.category), (abg.date, a.classification, a.category),
(abg.date, a.classification), (abg.date, a.classification),
(abg.date) (abg.date)
) )
ORDER BY date ASC; ORDER BY date ASC;
` `
) )
@ -453,14 +453,14 @@ export class AccountQueryService implements IAccountQueryService {
'accountId' in id 'accountId' in id
? [id.accountId] ? [id.accountId]
: sql`( : sql`(
SELECT SELECT
array_agg(a.id) array_agg(a.id)
FROM FROM
account a account a
LEFT JOIN account_connection ac ON ac.id = a.account_connection_id LEFT JOIN account_connection ac ON ac.id = a.account_connection_id
WHERE WHERE
(a.user_id = ${id.userId} OR ac.user_id = ${id.userId}) (a.user_id = ${id.userId} OR ac.user_id = ${id.userId})
AND a.is_active AND a.is_active
)` )`
const pStart = raw(`'${start}'`) const pStart = raw(`'${start}'`)
@ -469,58 +469,58 @@ export class AccountQueryService implements IAccountQueryService {
const { rows } = await this.pg.pool.query<AccountRollup>( const { rows } = await this.pg.pool.query<AccountRollup>(
sql` sql`
WITH account_rollup AS ( WITH account_rollup AS (
SELECT
abg.date,
a.classification,
a.category,
a.id,
SUM(abg.balance) AS balance,
CASE GROUPING(abg.date, a.classification, a.category, a.id)
WHEN 3 THEN 'classification'
WHEN 1 THEN 'category'
WHEN 0 THEN 'account'
ELSE NULL
END AS grouping
FROM
account_balances_gapfilled(
${pStart},
${pEnd},
${pInterval},
${pAccountIds}
) abg
INNER JOIN account a ON a.id = abg.account_id
GROUP BY
GROUPING SETS (
(abg.date, a.classification, a.category, a.id),
(abg.date, a.classification, a.category),
(abg.date, a.classification)
)
)
SELECT SELECT
abg.date, ar.date,
a.classification, ar.classification,
a.category, ar.category,
a.id, ar.id,
SUM(abg.balance) AS balance, ar.balance,
CASE GROUPING(abg.date, a.classification, a.category, a.id) ar.grouping,
WHEN 3 THEN 'classification' CASE
WHEN 1 THEN 'category' WHEN a.id IS NULL THEN NULL
WHEN 0 THEN 'account' ELSE json_build_object('id', a.id, 'name', a.name, 'mask', a.mask, 'syncStatus', a.sync_status, 'connection', CASE WHEN ac.id IS NULL THEN NULL ELSE json_build_object('name', ac.name, 'syncStatus', ac.sync_status) END)
ELSE NULL END AS account,
END AS grouping ROUND(
CASE ar.grouping
WHEN 'account' THEN COALESCE(ar.balance / SUM(NULLIF(ar.balance, 0)) OVER (PARTITION BY ar.grouping, ar.date, ar.classification, ar.category), 0)
WHEN 'category' THEN COALESCE(ar.balance / SUM(NULLIF(ar.balance, 0)) OVER (PARTITION BY ar.grouping, ar.date, ar.classification), 0)
WHEN 'classification' THEN COALESCE(ar.balance / SUM(NULLIF(ar.balance, 0)) OVER (PARTITION BY ar.grouping, ar.date), 0)
END, 4) AS rollup_pct,
ROUND(ar.balance / SUM(NULLIF(ar.balance, 0)) OVER (PARTITION BY ar.grouping, ar.date), 4) AS total_pct
FROM FROM
account_balances_gapfilled( account_rollup ar
${pStart}, LEFT JOIN account a ON a.id = ar.id
${pEnd}, LEFT JOIN account_connection ac ON ac.id = a.account_connection_id
${pInterval}, ORDER BY
${pAccountIds} ar.classification, ar.category, ar.id, ar.date;
) abg
INNER JOIN account a ON a.id = abg.account_id
GROUP BY
GROUPING SETS (
(abg.date, a.classification, a.category, a.id),
(abg.date, a.classification, a.category),
(abg.date, a.classification)
)
)
SELECT
ar.date,
ar.classification,
ar.category,
ar.id,
ar.balance,
ar.grouping,
CASE
WHEN a.id IS NULL THEN NULL
ELSE json_build_object('id', a.id, 'name', a.name, 'mask', a.mask, 'syncStatus', a.sync_status, 'connection', CASE WHEN ac.id IS NULL THEN NULL ELSE json_build_object('name', ac.name, 'syncStatus', ac.sync_status) END)
END AS account,
ROUND(
CASE ar.grouping
WHEN 'account' THEN COALESCE(ar.balance / SUM(NULLIF(ar.balance, 0)) OVER (PARTITION BY ar.grouping, ar.date, ar.classification, ar.category), 0)
WHEN 'category' THEN COALESCE(ar.balance / SUM(NULLIF(ar.balance, 0)) OVER (PARTITION BY ar.grouping, ar.date, ar.classification), 0)
WHEN 'classification' THEN COALESCE(ar.balance / SUM(NULLIF(ar.balance, 0)) OVER (PARTITION BY ar.grouping, ar.date), 0)
END, 4) AS rollup_pct,
ROUND(ar.balance / SUM(NULLIF(ar.balance, 0)) OVER (PARTITION BY ar.grouping, ar.date), 4) AS total_pct
FROM
account_rollup ar
LEFT JOIN account a ON a.id = ar.id
LEFT JOIN account_connection ac ON ac.id = a.account_connection_id
ORDER BY
ar.classification, ar.category, ar.id, ar.date;
` `
) )