I have 3 tables :
60.000 record Invoice
100.000 record Exchange Rate based on current date
25 record Currency Symbol
table_invoice:
+-----------+-------------+----------+--------------+
| invoice_id | currency_id | amount | invoice_date |
+------------+-------------+----------+--------------+
| 1 | 2 | 10 | 4/28/2016 |
| 2 | 3 | 30 | 4/29/2016 |
| 3 | 4 | 50 | 4/30/2016 |
| 4 | 2 | 40 | 6/18/2016 |
| 5 | 6 | 25 | 6/20/2016 |
| 6 | 7 | 87 | 6/25/2016 |
| 7 | 4 | 100 | 6/29/2016 |
| 8 | 9 | 45 | 7/14/2016 |
| 9 | 2 | 71 | 9/27/2016 |
| 60000 | 3 | 430 | 1/18/2017 |
+------------+-------------+----------+--------------+
and
table_exchange_rate:
+-----------------+-------------+---------------+--------------------+
| exchange_rate_id | currency_id | exchange_rate | exchange_rate_date |
+------------------+-------------+---------------+--------------------+
| 1 | 2 | 13.352 | 4/25/2016 |
| 2 | 3 | 10.195 | 4/25/2016 |
| 3 | 4 | 14.390 | 4/25/2016 |
| 4 | 5 | 1.720 | 4/25/2016 |
| 5 | 6 | 118 | 4/25/2016 |
| 6 | 7 | 9.468 | 4/25/2016 |
| 7 | 2 | 13.125 | 6/15/2016 |
| 8 | 3 | 10.520 | 6/25/2016 |
| 9 | 4 | 14.800 | 6/25/2016 |
| 10 | 5 | 1.800 | 6/25/2016 |
| 11 | 6 | 120 | 6/25/2016 |
| 12 | 7 | 9.320 | 6/25/2016 |
| 100000 | 7 | 9.500 | 6/25/2016 |
+------------------+-------------+---------------+--------------------+
and
reference_currency:
+-----------------+---------------+-----------------------+
| currency_id |currency_symbol| currency_name |
+------------------+---------------+-----------------------+
| 1 | USD | US Dollar |
| 2 | AUD | Australian Dollar |
| 3 | EUR | Euro |
| 4 | HKD | Hong Kong Dollar |
| 5 | JPY | Japan Yen |
| 6 | SGD | Singapore Dollar |
| 7 | MYR | Malaysian Ringgit |
| 8 | CHF | Swiss Franc |
| 9 | THB | Thailand Baht |
| 10 | GBP | Great Britain Pounds |
| 11 | SEK | Swedish Krona |
| 12 | CNY | China Yuan |
| 25 | SAR | Saudi Arabian Riyal |
+------------------+-------------+-------------------------+
When I run:
SELECT
a.invoice_id AS 'INVOICE_ID',
a.currency_id AS 'CURRENCY_ID',
a.amount AS 'AMOUNT',
a.invoice_date AS 'INVOICE_DATE',
(
SELECT b.exchange_rate FROM table_exchange_rate b
WHERE b.exchange_rate_date <= a.invoice_date AND b.currency_id = a.currency_id
ORDER BY b.exchange_rate_date DESC LIMIT 1) AS 'EXCHANGE RATE'
)
FROM table_invoice a
Result:
`+------------+-------------+----------+--------------+--------------+
| INVOICE_ID | CURRENCY_ID | AMOUNT | INVOICE_DATE | EXCHANGE_RATE |
+------------+-------------+----------+--------------+---------------+
| 1 | 2 | 10 | 4/28/2016 | 13.352
| 2 | 3 | 30 | 4/29/2016 | 10.195
| 3 | 4 | 50 | 4/30/2016 | 14.390
| 4 | 2 | 40 | 6/18/2016 | 13.125
| 5 | 6 | 25 | 6/20/2016 | 118
| 6 | 7 | 87 | 6/25/2016 | 9.320
`
and the result works fine but it is very slow(approximately > 60sec) with 60k record(table_invoice) looping on 100k record(table_exchange_rate) finding exchange rate at the current date,
If invoice_date can't match with exchange_rate_date or the user doesn't input the exchange rate on the app, it will using exchange rate the latest record already in input before the current date (b.exchange_rate_date <= a.invoice_date AND b.currency_id = a.currency_id)
Can I speed up this query or there any other options? Thank you..
Try same thing with JOIN
select a.invoice 'invoice_id', a.currency_id 'curency_id', a.amount 'amount', a.invoice_date 'invoice_date', ter.exchange_rate ' exchange_rate'
from table_invoice ti
left join table_exchange_rate ter on ter.currency_id = ti.currency_id and ter.exchange_rate_date <= ti.invoice_date
Related
I have column user and rating.
SELECT rating.idUser, user.nmUser, rating.idBengkel, rating.nilai FROM `rating`
JOIN user on rating.idUser = user.idUser
WHERE rating.idBengkel=1 or rating.idBengkel=2
Result :
+--------+---------------------------+-----------+-------+
| idUser | nmUser | idBengkel | nilai |
+--------+---------------------------+-----------+-------+
| 10 | Hudson mas77 | 1 | 5 |
| 11 | Vina Nurfadzilah | 1 | 5 |
| 12 | Angelica Amartya | 1 | 5 |
| 15 | Syahrul K | 1 | 4 |
| 27 | Ashar Murdihastomo | 1 | 5 |
| 28 | Eril Obeit Choiri | 1 | 2 |
| 29 | Ariyadi | 1 | 3 |
| 30 | Robertus Dwian Augusta | 1 | 4 |
| 31 | Irfan Setiaji | 1 | 4 |
| 33 | Baby Ayuna | 1 | 5 |
| 9 | Nur k hamid | 2 | 5 |
| 10 | Hudson mas77 | 2 | 5 |
| 13 | Yuana Putra | 2 | 4 |
| 14 | Nanda Aulia Irza Ramadhan | 2 | 4 |
| 26 | taufiq rahman | 2 | 5 |
| 27 | Ashar Murdihastomo | 2 | 5 |
| 28 | Eril Obeit Choiri | 2 | 5 |
| 30 | Robertus Dwian Augusta | 2 | 4 |
| 44 | halim budiono | 2 | 1 |
+--------+---------------------------+-----------+-------+
When i try to get similar records using this query
SELECT rating.idUser, user.nmUser FROM rating
JOIN user
ON rating.idUser = user.idUser
WHERE rating.idBengkel = 1 and rating.idUser
IN (SELECT rating.idUser from rating WHERE rating.idBengkel = 2)
ORDER by idUser
Result :
+-----------+------------------------+
| idUser | nmUser |
+-----------+------------------------+
| 10 | Hudson mas77 |
| 27 | Ashar Murdihastomo |
| 28 | Eril Obeit Choiri |
| 30 | Robertus Dwian Augusta |
+-----------+------------------------+
The result work fine, but I want show column 'nilai' as ItemX and ItemY. Those are user similar data. In this case I have 4 similar user who rate on idBengkel=1 and idBengkel=2 as the results above. I want it like the table below.
+--------+------------------------+-------+-------+
| idUser | nmUser | ItemX | ItemY |
+--------+------------------------+-------+-------+
| 10 | Hudson mas77 | 5 | 5 |
| 27 | Ashar Murdihastomo | 5 | 5 |
| 28 | Eril Obeit Choiri | 2 | 5 |
| 30 | Robertus Dwian Augusta | 4 | 4 |
+--------+------------------------+-------+-------+
I need solution for this and i was trying with this solution in https://stackoverflow.com/a/7976379/12396302 but it resulting more than one row. Please help me, I cant implement that query's solution. Regards!
I think you need below query -
SELECT rating.idUser,
user.nmUser,
MAX(CASE WHEN rating.idBengkel = 1 THEN rating.nilai END) ItemX,
MAX(CASE WHEN rating.idBengkel = 2 THEN rating.nilai END) ItemY,
FROM `rating`
JOIN user on rating.idUser = user.idUser
WHERE rating.idBengkel IN (1, 2)
GROUP BY rating.idUser,
user.nmUser
I have a table like this in MS Access 2019:
+-----------+------------+--------+----------+-------+
| BillingID | Date | RoomID | Electric | Water |
+-----------+------------+--------+----------+-------+
| 1 | 12/23/2018 | 4 | 1669 | 106 |
| 2 | 12/26/2018 | 1 | 5035 | 289 |
| 3 | 12/27/2018 | 6 | 0 | 0 |
| 4 | 12/31/2018 | 5 | 3158 | 223 |
| 5 | 1/6/2019 | 2 | 3823 | 194 |
| 6 | 1/15/2019 | 3 | 1772 | 125 |
| 7 | 1/23/2019 | 4 | 1796 | 117 |
| 8 | 1/26/2019 | 1 | 5231 | 299 |
| 9 | 1/27/2019 | 6 | 0 | 0 |
| 10 | 1/31/2019 | 5 | 3366 | 242 |
| 11 | 2/14/2019 | 2 | 3975 | 201 |
| 12 | 2/15/2019 | 3 | 1839 | 129 |
+-----------+------------+--------+----------+-------+
I could calculate the electricity and water usage with Index & Match in MS Excel. However, I've had a lot of trouble to achieve this with MS Access. The result I want is as below:
+-----------+------------+--------+----------+---------------+-------+------------+
| BillingID | Date | RoomID | Electric | ElectricUsage | Water | WaterUsage |
+-----------+------------+--------+----------+---------------+-------+------------+
| 1 | 12/23/2018 | 4 | 1669 | | 106 | |
| 2 | 12/26/2018 | 1 | 5035 | | 289 | |
| 3 | 12/27/2018 | 6 | 0 | | 0 | |
| 4 | 12/31/2018 | 5 | 3158 | | 223 | |
| 5 | 1/6/2019 | 2 | 3823 | | 194 | |
| 6 | 1/15/2019 | 3 | 1772 | | 125 | |
| 7 | 1/23/2019 | 4 | 1796 | 127 | 117 | 11 |
| 8 | 1/26/2019 | 1 | 5231 | 196 | 299 | 10 |
| 9 | 1/27/2019 | 6 | 0 | | 0 | |
| 10 | 1/31/2019 | 5 | 3366 | 208 | 242 | 19 |
| 11 | 2/14/2019 | 2 | 3975 | 152 | 201 | 7 |
| 12 | 2/15/2019 | 3 | 1839 | 67 | 129 | 4 |
+-----------+------------+--------+----------+---------------+-------+------------+
For example, for RoomID = 4, the ElectricUsage is the difference between the Electric in BillingID #7 and BillingID #1 and so on.
I've tried some answer like this or this but Access ran into errors when using those solutions in SQL view (Syntax error in FROM clause).
Thanks.
You can use a couple of sub-queries to return the Electric/Water for each room on the previous date:
SELECT
B.BillingID, B.BillingDate, B.RoomID, B.Electric,
B.Electric-(SELECT TOP 1 E.Electric FROM tblBilling AS E WHERE B.RoomID=E.RoomID AND E.BillingDate<B.BillingDate ORDER BY E.BillingDate DESC) AS ElectricUsage,
B.Water,
B.Water-(SELECT TOP 1 W.Water FROM tblBilling AS W WHERE B.RoomID=W.RoomID AND W.BillingDate<B.BillingDate ORDER BY W.BillingDate DESC) AS WaterUsage
FROM tblBilling AS B
Note that I've renamed your Date field to be BillingDate, as Date is a reserved word in Access, and will cause you problems in the future.
Regards,
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have two tables 'tbl_orders' and 'tbl_instore'. tbl_orders have 'sel_product_qty' which I want to date wise SUM and table(tbl_instore) have 'inst_prod_qty' and 'chln_amount' which I calculate and want to get purchased unit price. But when I use join query on those two tables, the SUM(sel_product_qty) produces double, triple and quadruple amount from the expected result.
Sample data tables are..
Table "tbl_order":
+----------+------------+------------+-----------------+---------------+--------------------+
| order_id | ord_det_id | product_id | sel_product_qty | selling_price | order_date_time |
+----------+------------+------------+-----------------+---------------+--------------------+
| 3 | 1 | 4 | 50 | 67.82 | 2019-03-21 21:52:21|
| 4 | 1 | 1 | 100 | 37.88 | 2019-03-21 21:52:21|
| 5 | 2 | 4 | 120 | 67.82 | 2018-03-23 00:02:36|
| 6 | 3 | 3 | 300 | 123.67 | 2019-03-23 00:04:38|
| 7 | 3 | 2 | 50 | 76.28 | 2019-03-23 00:04:38|
| 8 | 4 | 4 | 50 | 67.82 | 2019-03-24 12:13:06|
| 9 | 4 | 2 | 100 | 76.28 | 2019-03-24 12:13:06|
| 10 | 5 | 1 | 10 | 37.88 | 2019-03-25 12:56:40|
| 11 | 5 | 4 | 7 | 67.82 | 2019-03-25 12:56:40|
| 12 | 6 | 4 | 23 | 67.82 | 2019-03-29 00:29:14|
| 13 | 6 | 2 | 25 | 76.28 | 2019-03-29 00:29:14|
| 16 | 7 | 1 | 120 | 37.88 | 2019-04-14 16:51:10|
| 17 | 7 | 3 | 90 | 123.67 | 2019-04-14 16:51:11|
| 18 | 8 | 1 | 100 | 66.95 | 2019-04-22 23:30:39|
| 19 | 8 | 2 | 22 | 70.04 | 2019-04-22 23:30:39|
+----------+------------+------------+-----------------+---------------+--------------------+
Table "tbl_instore":
+----------+----------+------------+---------------+-------------+--------------------+
| in_st_id | s_inv_id | product_id | inst_prod_qty | chln_amount | instore_date_time |
+----------+----------+------------+---------------+-------------+--------------------+
| 1 | 1 | 1 | 1000 | 65852 | 2/14/2018 17:28 |
| 14 | 9 | 1 | 100 | 6400 | 4/26/2019 8:26 |
| 3 | 2 | 1 | 2000 | 58885 | 3/19/2019 17:32 |
| 5 | 3 | 1 | 100 | 3588 | 3/19/2019 17:35 |
| 11 | 7 | 1 | 1000 | 65000 | 4/22/2019 23:17 |
| 9 | 5 | 1 | 100 | 6345 | 4/20/2019 0:13 |
| 12 | 8 | 2 | 100 | 7800 | 4/22/2019 23:20 |
| 8 | 4 | 2 | 2000 | 144567 | 3/23/2019 0:04 |
| 7 | 4 | 3 | 1000 | 121665 | 3/23/2019 0:04 |
| 13 | 8 | 3 | 150 | 32000 | 4/22/2019 23:20 |
| 15 | 9 | 3 | 100 | 19000 | 4/26/2019 8:26 |
| 10 | 6 | 4 | 1000 | 88022 | 4/20/2019 0:16 |
| 6 | 3 | 4 | 100 | 6582 | 3/19/2019 17:35 |
| 4 | 2 | 4 | 1000 | 65882 | 3/19/2019 17:32 |
| 2 | 1 | 4 | 5000 | 359877 | 2/14/2018 17:28 |
+----------+----------+------------+---------------+-------------+--------------------+
The following query I have currently tried:
SELECT SUM(tbl_orders.sel_product_qty) AS `sel_prod_qty`,
(SUM(chln_amount) / SUM(inst_prod_qty)) AS `pur_uni_price`,
date_format(`order_date_time`, '%M-%Y') as `month`,
tbl_orders.product_id AS `product_id`
FROM tbl_orders
INNER JOIN tbl_instore ON tbl_instore.product_id = tbl_orders.product_id
WHERE YEAR(`order_date_time`)= '2019'
GROUP BY `month`, `product_id`;
Which return the following result:
+--------------+---------------+--------+------------+
| sel_prod_qty | pur_uni_price | month | product_id |
+--------------+---------------+--------+------------+
| 1320 | 47.923256 | Apr-19 | 1 |
+--------------+---------------+--------+------------+
| 44 | 72.555714 | Apr-19 | 2 |
+--------------+---------------+--------+------------+
| 270 | 138.132 | Apr-19 | 3 |
+--------------+---------------+--------+------------+
| 660 | 47.923256 | Mar-19 | 1 |
+--------------+---------------+--------+------------+
| 350 | 72.555714 | Mar-19 | 2 |
+--------------+---------------+--------+------------+
| 900 | 138.132 | Mar-19 | 3 |
+--------------+---------------+--------+------------+
| 520 | 73.290563 | Mar-19 | 4 |
+--------------+---------------+--------+------------+
If I run the query individually without JOIN, I will get SUM(sel_prod_qty) value as following (also my expected result should be):
+--------------+---------------+--------+------------+
| sel_prod_qty | pur_uni_price | month | product_id |
+--------------+---------------+--------+------------+
| 220 | 47.923256 | Apr-19 | 1 |
+--------------+---------------+--------+------------+
| 22 | 72.555714 | Apr-19 | 2 |
+--------------+---------------+--------+------------+
| 90 | 138.132 | Apr-19 | 3 |
+--------------+---------------+--------+------------+
| 110 | 47.923256 | Mar-19 | 1 |
+--------------+---------------+--------+------------+
| 175 | 72.555714 | Mar-19 | 2 |
+--------------+---------------+--------+------------+
| 300 | 138.132 | Mar-19 | 3 |
+--------------+---------------+--------+------------+
| 130 | 73.290563 | Mar-19 | 4 |
+--------------+---------------+--------+------------+
So, my question is, why does my query return SUM(sel_product_qty) double, triple and quadruple amount from the expected result?
Try this:
SELECT `sel_prod_qty`,
`pur_uni_price`,
`month`,
orders.product_id AS `product_id`
FROM
(SELECT product_id,SUM(sel_product_qty) AS `sel_prod_qty`,
DATE_FORMAT(`order_date_time`, '%M-%Y') AS `month`
FROM tbl_orders
WHERE YEAR(`order_date_time`)='2019'
GROUP BY `month`, `product_id`) orders
INNER JOIN
(SELECT product_id,(SUM(chln_amount) / SUM(inst_prod_qty)) AS `pur_uni_price`
FROM tbl_instore GROUP BY product_id) instore
ON orders.product_id=instore.product_id;
I created two sub-query from those tables that you joined because what doesn't work is that you are doing INNER JOIN ... ON tbl_instore.product_id = tbl_orders.product_id. Which if you refer back to your table, those value were repeated multiple times in both tables. Therefore, this sub-queries will perform the mathematical operation and the outer query will only return the results from it.
The pur_uni_price field is not an aggregate field anymore, therefore you need to add it on your GROUP BY. See code below
SELECT SUM(tbl_orders.sel_product_qty) AS `sel_prod_qty`,
(SUM(chln_amount) / SUM(inst_prod_qty)) AS `pur_uni_price`,
date_format(`order_date_time`, '%M-%Y') as `month`,
tbl_orders.product_id AS `product_id`
FROM tbl_orders
INNER JOIN tbl_instore ON tbl_instore.product_id = tbl_orders.product_id
WHERE YEAR(`order_date_time`)= '2019'
GROUP BY `month`, `product_id`, (SUM(chln_amount) / SUM(inst_prod_qty))
I need to create a log having the purchase date of an item.
Items can be owned by only one buyer at time. So, for example, if item1 was purchased by buyer2 in 2009 and after by buyer1 in 2015, then between 2009 and 2015 was owned by buyer2.
Here is my table:
+--------+------------+-----------+----------+
| id_doc | date | id_item | id_buyer |
+--------+------------+-----------+----------+
| 11 | 2016-06-07 | 1 | 4 |
| 10 | 2016-06-06 | 1 | 4 |
| 1 | 2015-11-30 | 1 | 1 |
| 9 | 2009-01-01 | 1 | 2 |
| 4 | 2001-01-12 | 1 | 2 |
| 8 | 1996-06-06 | 1 | 2 |
| 3 | 1995-05-29 | 1 | 1 |
| 2 | 1998-05-23 | 2 | 2 |
| 7 | 2014-10-10 | 3 | 2 |
| 6 | 2003-12-12 | 3 | 3 |
| 5 | 1991-01-12 | 3 | 2 |
+--------+------------+-----------+----------+
Here is a kind of table/view I need:
+------------+------------+-----------+----------+--------+
| date_from | date_to | id_item | id_buyer | id_doc |
+------------+------------+-----------+----------+--------+
| 2016-06-07 | - | 1 | 4 | 11 |
| 2016-06-06 | 2016-06-07 | 1 | 4 | 10 |
| 2015-11-30 | 2016-06-06 | 1 | 1 | 1 |
| 2009-01-01 | 2015-11-30 | 1 | 2 | 9 |
| 2001-01-12 | 2009-01-01 | 1 | 2 | 4 |
| 1996-06-06 | 2001-01-12 | 1 | 2 | 8 |
| 1995-05-29 | 1996-06-06 | 1 | 1 | 3 |
| 1998-05-23 | - | 2 | 2 | 2 |
| 2014-10-10 | - | 3 | 2 | 7 |
| 2003-12-12 | 2014-10-10 | 3 | 3 | 6 |
| 1991-01-12 | 2003-12-12 | 3 | 2 | 5 |
+------------+------------+-----------+----------+--------+
I've tried a lot with GROUP BY, GROUP_CONCAT, trying to access next record date, etc ... but I can't found out how to solve the problem.
Thanks in advance.
I finally found out the solution only for past purchases.
SELECT
main.id_doc, main.id_item, main.date AS "date_from", bi.date AS "date_to", main.id_buyer
FROM
MyTable main, MyTable bi
WHERE
bi.id_doc =
(
SELECT sub.id_doc
FROM MyTable sub
WHERE sub.id_item = main.id_item AND sub.date > main.date ORDER BY sub.date ASC LIMIT 1
);
I have three tables, mess_stock, mess_voucher, add_grocery.
Mess_stock table is below,
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| sno | voucher_id | particular_name | opening_balance | inward | outward | balance | pay_amount | pay_type |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| 49 | 5 | 4 | 100 | 10 | 100 | 10 | 10.00 | 1 |
| 50 | 17 | 5 | 111 | 10 | 20 | 101 | 60.00 | 1 |
| 51 | 7 | 3 | 123 | 2 | 1 | 124 | 300.00 | 1 |
| 52 | 7 | 1 | 123 | 20 | 20 | 123 | 500.00 | 2 |
| 53 | 14 | 8 | 100 | 5 | 95 | 10 | 60.00 | 2 |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
Mess_voucher table is below
+------------+--------------+--------------+
| voucher_id | voucher_name | voucher_date |
+------------+--------------+--------------+
| 5 | VG1001 | 2015-02-19 |
| 6 | VG1001 | 2015-02-20 |
| 7 | VG1002 | 2015-02-20 |
| 8 | VG1002 | 2015-02-19 |
| 9 | MS1001 | 2015-02-20 |
| 10 | VG10012 | 2015-02-19 |
| 11 | 0 | 2015-02-23 |
| 12 | 1 | 2015-02-24 |
| 13 | MS1001 | 2015-02-25 |
| 14 | MS1001 | 2015-02-28 |
| 15 | VG1003 | 2015-02-28 |
| 16 | MS1001 | 2015-02-19 |
| 17 | MS1001 | 2015-02-21 |
+------------+--------------+--------------+
Add_grocery table is below
+-----+-----------------+------------------+
| sno | particular_name | particular_price |
+-----+-----------------+------------------+
| 1 | Rice | 25.00 |
| 3 | Mango | 150.00 |
| 4 | Coconut | 22.00 |
| 5 | Banana | 6.00 |
| 6 | Raddish | 12.00 |
| 7 | Apple | 150.00 |
| 8 | Pumkin | 12.00 |
+-----+-----------------+------------------+
I want to group the sum of pay_amount of mess_stock table. I have used the below query
SELECT opening_balance AS ope_stock,
balance AS clo_stock,
SUM(IF(pay_type = 1, pay_amount, 0)) mess_pay,
SUM(IF(pay_type=2, pay_amount, 0)) est_pay
FROM mess_stock;
That works fine. The particular_name is the auto increment id of add_grocery table. I need the inward outward amount total. For example the inward amount 10 means it has to get the particular_price from add_grocery using the particular_name provided in the mess_stock table, similarly I need all the answer. And I want to sort that by date wise. The date of the entry is stored in the mess_voucher table that is connected to mess_stock table.
Try this it will work :
Use Inner Join :
SELECT t2.`particular_name`,t1.`inward`,t1.`outward`,t2.`particular_price`,t3.`voucher_date` from Mess_stock t1 JOIN Add_grocery t2 ON t1.`particular_name`=t2.`sno` JOIN Mess_voucher t3 ON t3.`voucher_id`=t1.`voucher_id` ORDER BY t3.`voucher_date` DESC