I initially tried fetching data using individual queries which took too much of resources, so I am trying to combine the queries in order to reduce the time taken. The first select works fine in itself but I want to combine with the second select in order to reduce the time taken.
Current error: #1241 - Operand should contain 1 column(s)
SELECT (SELECT sc.description AS description,
sc.maincode,
sc.controlcode,
sc.subcode,
( ob.debitbalance - ob.creditbalance ) AS mainbalance
FROM subcode AS sc
LEFT JOIN openingbalance AS ob
ON ( sc.maincode = ob.maincode
AND sc.controlcode = ob.controlcode
AND sc.subcode = ob.subcode )
GROUP BY sc.maincode,
sc.controlcode,
sc.subcode
ORDER BY sc.maincode,
sc.controlcode,
sc.subcode ASC) AS test,
(SELECT Sum(v.debit) AS totaldebit,
Sum(v.credit) AS totalcredit,
( mainbalance + totaldebit - totalcredit ) AS openingbalance
FROM subcode AS sc
LEFT JOIN voucher AS v
ON ( sc.maincode = v.maincode
AND sc.controlcode = v.controlcode
AND sc.subcode = v.subcode )
WHERE Substring(v.voucherdate, 1, 7) < '07-2019'
GROUP BY sc.maincode,
sc.controlcode,
sc.subcode
ORDER BY sc.maincode,
sc.controlcode,
sc.subcode ASC) AS test2
Table structure is as follows:
Subcode Table
+----------+-------------+---------+--------------+
| Maincode | Controlcode | Subcode | Description |
+----------+-------------+---------+--------------+
| 01 | 01 | 123 | Test Account |
| 01 | 02 | 124 | Test Account |
+----------+-------------+---------+--------------+
Voucher Table
+-------------+--------------+---------------+----------+-------------+---------+----------------+-------+--------+
| Voucherdate | Vouchertype | Vouchernumber | Maincode | Controlcode | Subcode | Description | Debit | Credit |
+-------------+--------------+---------------+----------+-------------+---------+----------------+-------+--------+
| 2019-07-13 | BV | 01 | 01 | 01 | 123 | Entering Test | 100 | 0 |
| 2019-07-13 | BV | 01 | 01 | 02 | 124 | Enterting Test | 0 | 100 |
+-------------+--------------+---------------+----------+-------------+---------+----------------+-------+--------+
OpeningBalance Table
+----------+-------------+---------+--------------+---------------+
| Maincode | Controlcode | Subcode | Debitbalance | Creditbalance |
+----------+-------------+---------+--------------+---------------+
| 01 | 01 | 123 | 100 | 0 |
| 01 | 02 | 124 | 100 | 0 |
+----------+-------------+---------+--------------+---------------+
Hoping for following outcome
+--------------+----------+-------------+---------+-------------+------------+-------------+----------------+
| Description | Maincode | Controlcode | Subcode | Mainbalance | Totaldebit | Totalcredit | Openingbalance |
+--------------+----------+-------------+---------+-------------+------------+-------------+----------------+
| Test Account | 01 | 01 | 123 | 100 | 100 | 0 | 200 |
| Test Account | 01 | 02 | 124 | 100 | 0 | 100 | 0 |
+--------------+----------+-------------+---------+-------------+------------+-------------+----------------+