This topic is locked

sqlview query

7/29/2024 6:00:38 AM
PHPRunner General questions
L
leon author

The following query produces records when I run it iin the Sqlview. When I run the list page it however produces "No data yet" and shows no records. Any advice would be greatly appreciated.
Leon.

-- Subquery to calculate total VAT for all lines except "Vat Control"
WITH TotalVAT AS (
SELECT
SUM(AllRecords.VAT) AS TotalVAT
FROM AllRecords
LEFT OUTER JOIN AccountNos ON AllRecords.Account = AccountNos.`No`
WHERE AccountNos.`Desc` <> 'Vat Control'
AND ((AllRecords.`year` < :session_year) OR (AllRecords.`year` = :session_year AND AllRecords.period <= :session_period))
)

-- Main query to calculate balance, vat, and final balance including adjustment for "Vat Control"
SELECT
`Desc`,
`Type`,
balance,
CASE
WHEN `Desc` = 'Vat Control' THEN balance + COALESCE(tv.TotalVAT, 0)
ELSE balance
END AS `Final Balance`,
vat,
CASE
WHEN (CASE WHEN `Desc` = 'Vat Control' THEN balance + COALESCE(tv.TotalVAT, 0) ELSE balance END) >= 0
THEN (CASE WHEN `Desc` = 'Vat Control' THEN balance + COALESCE(tv.TotalVAT, 0) ELSE balance END)
ELSE 0
END AS Debit,
CASE
WHEN (CASE WHEN `Desc` = 'Vat Control' THEN balance + COALESCE(tv.TotalVAT, 0) ELSE balance END) < 0
THEN - (CASE WHEN `Desc` = 'Vat Control' THEN balance + COALESCE(tv.TotalVAT, 0) ELSE balance END)
ELSE 0
END AS Credit
FROM (
SELECT
CASE
WHEN AccountNos.BalanceSheet = 0 AND AccountNos.`Type` = 'Creditors' THEN 'Creditors'
WHEN AccountNos.BalanceSheet = 0 THEN 'Retained Earnings'
ELSE AccountNos.`Desc`
END AS `Desc`,
CASE
WHEN AccountNos.BalanceSheet = 0 AND AccountNos.`Type` = 'Creditors' THEN 'Creditors'
WHEN AccountNos.BalanceSheet = 0 THEN 'Retained Earnings'
ELSE AccountNos.`Type`
END AS `Type`,
SUM(
CASE
WHEN AccountNos.`Desc` = 'Vat Control' THEN AllRecords.Amount
ELSE AllRecords.Amount
END
) AS balance,
SUM(
CASE
WHEN AccountNos.`Desc` = 'Vat Control' THEN 0 -- Set to 0 for "Vat Control" line
ELSE AllRecords.VAT
END
) AS vat
FROM AllRecords
LEFT OUTER JOIN AccountNos ON AllRecords.Account = AccountNos.`No`
WHERE ((AllRecords.`year` < :session_year) OR (AllRecords.`year` = :session_year AND AllRecords.period <= :session_period))
GROUP BY
CASE
WHEN AccountNos.BalanceSheet = 0 AND AccountNos.`Type` = 'Creditors' THEN 'Creditors'
WHEN AccountNos.BalanceSheet = 0 THEN 'Retained Earnings'
ELSE AccountNos.`Desc`
END,
CASE
WHEN AccountNos.BalanceSheet = 0 AND AccountNos.`Type` = 'Creditors' THEN 'Creditors'
WHEN AccountNos.BalanceSheet = 0 THEN 'Retained Earnings'
ELSE AccountNos.`Type`
END
) AS subquery
CROSS JOIN TotalVAT tv -- Join with TotalVAT subquery to get the total VAT sum
ORDER BY
`Type`,
`Desc`;
C
Chris Whitehead 7/29/2024

Without having the data in the tables and the actual session variables then all I can do is take a guess on what you're wanting.

Your query gave me an error so I'm not sure if that caused issues, I've tweaked it slightly for the errors.

-- Subquery to calculate total VAT for all lines except "Vat Control"
WITH TotalVAT AS (
SELECT
SUM(AllRecords.VAT) AS TotalVAT
FROM AllRecords
LEFT OUTER JOIN AccountNos ON AllRecords.Account = AccountNos.`No`
WHERE AccountNos.`Desc` <> 'Vat Control'
AND ((AllRecords.`year` < 2023) OR (AllRecords.`year` = 2023 AND AllRecords.period <= 8))
)
SELECT * FROM TotalVAT;

-- Main query to calculate balance, vat, and final balance including adjustment for "Vat Control"
WITH TotalVAT AS (
SELECT
SUM(AllRecords.VAT) AS TotalVAT
FROM AllRecords
LEFT OUTER JOIN AccountNos ON AllRecords.Account = AccountNos.`No`
WHERE AccountNos.`Desc` <> 'Vat Control'
AND ((AllRecords.`year` < 2023) OR (AllRecords.`year` = 2023 AND AllRecords.period <= 7))
)
SELECT
`Desc`,
`Type`,
balance,
CASE
WHEN `Desc` = 'Vat Control' THEN balance + COALESCE(tv.TotalVAT, 0)
ELSE balance
END AS `Final Balance`,
vat,
CASE
WHEN (CASE WHEN `Desc` = 'Vat Control' THEN balance + COALESCE(tv.TotalVAT, 0) ELSE balance END) >= 0
THEN (CASE WHEN `Desc` = 'Vat Control' THEN balance + COALESCE(tv.TotalVAT, 0) ELSE balance END)
ELSE 0
END AS Debit,
CASE
WHEN (CASE WHEN `Desc` = 'Vat Control' THEN balance + COALESCE(tv.TotalVAT, 0) ELSE balance END) < 0
THEN - (CASE WHEN `Desc` = 'Vat Control' THEN balance + COALESCE(tv.TotalVAT, 0) ELSE balance END)
ELSE 0
END AS Credit
FROM (
SELECT
CASE
WHEN AccountNos.BalanceSheet = 0 AND AccountNos.`Type` = 'Creditors' THEN 'Creditors'
WHEN AccountNos.BalanceSheet = 0 THEN 'Retained Earnings'
ELSE AccountNos.`Desc`
END AS `Desc`,
CASE
WHEN AccountNos.BalanceSheet = 0 AND AccountNos.`Type` = 'Creditors' THEN 'Creditors'
WHEN AccountNos.BalanceSheet = 0 THEN 'Retained Earnings'
ELSE AccountNos.`Type`
END AS `Type`,
SUM(
CASE
WHEN AccountNos.`Desc` = 'Vat Control' THEN AllRecords.Amount
ELSE AllRecords.Amount
END
) AS balance,
SUM(
CASE
WHEN AccountNos.`Desc` = 'Vat Control' THEN 0 -- Set to 0 for "Vat Control" line
ELSE AllRecords.VAT
END
) AS vat
FROM AllRecords
LEFT OUTER JOIN AccountNos ON AllRecords.Account = AccountNos.`No`
WHERE ((AllRecords.`year` < 2023) OR (AllRecords.`year` = 2023 AND AllRecords.period <= 7))
GROUP BY
CASE
WHEN AccountNos.BalanceSheet = 0 AND AccountNos.`Type` = 'Creditors' THEN 'Creditors'
WHEN AccountNos.BalanceSheet = 0 THEN 'Retained Earnings'
ELSE AccountNos.`Desc`
END,
CASE
WHEN AccountNos.BalanceSheet = 0 AND AccountNos.`Type` = 'Creditors' THEN 'Creditors'
WHEN AccountNos.BalanceSheet = 0 THEN 'Retained Earnings'
ELSE AccountNos.`Type`
END
) AS subquery
CROSS JOIN TotalVAT tv -- Join with TotalVAT subquery to get the total VAT sum
ORDER BY
`Type`,
`Desc`;
L
leon author 7/29/2024

Hi Chris.
Thank you for the effort. It is truly appreciated and your understanding of the query is correct.
What puzzles me is that if I hardcode the year and period it returns records in the sqlview window and on the list page. As soon as I use session variables it still returns records in the sqlview window but does not return any records on the list page when I run the project.
I am trying ChatGTP to see if I can get result and will post here if I find answer.
Regards,
Leon

C
Chris Whitehead 7/30/2024

@Leon Could it be the session variables in the list view which aren't set when you run the query?

maybe run this in the list view, not sure where you'll need it but you could try BeforeQueryList, I'd also put an "exit;" after the statement so you just get the session to display.

echo "
File:" .basename(__FILE__) . "
Line:" .__LINE__ . "<pre>" . print_r($_SESSION,true) . "</pre>";
exit;// not needed only so you can view the output
D
Denes 8/13/2024

@Leon

My experience say it is better (when you have a complicated select) that you create a phisical view in your database, after that you could run your query.