mysql - In Left join with order by always fetch first data from relational table. (Rails) -
I have two tables like invoices and receipts
1 invoice ID name MT position 1 Test 100 Payout 2 Examination 1 300 Not paid 3 Examination 2 400 Not paid 2. Receipts ID amount Challan_Add receipt_date 1 50 1 22-Apr 2014 2 30 1 24-Apr 2014 3 30 1 25-AR-20144 > The following is my query
@ invoice_info = invoice. Selection (as "inv_id as invoice.id, receipts.receipt_date receipt_date, sum (receipts.amount)) (as receipt_amount").) (Joy '(Receipts.invoice_id =' Receipts left on invoices.id '). Where ("Invoice. Stats in ('Paid')"). The group ("Invoice.ID"). The order ("Receipts.receipt_date desc") is shown in the form of data: inv_id receipt_amount receipt_date 1 100 22-apr-2014 So the problem is, before the descending date Receives the date of I need the date of the last receipt "25-APR-2014"
Can anyone help me?
Your invoice .select () does not map to valid SQL for each closed column for valid SQL The SELECT clause is required to be displayed in the group by clause also. MySQL's "Extension" group BY, which allows you to do, is not deterministic. (MySQL is giving you whatever is the easiest to fetch the date.) SQL developers consider this as a bug, not a feature.
I will use SQL for this whole answer.
Implement this SQL statement in PostgreSQL. .
Choose inv_id as invoice, receipts.receipt_date, as Receipt_date, Receipts at receipts.invoice_id = invoices.id, as receipt_mount from the amount (receipts.mount) invoice, where Invoice ('Paid') receipts received by the group, receipts by invoice. ID RCITIDA order returns this output.
1 2014-04-2 24 30 1 2014-04-22 80 If you only want a per-invoice amount, which makes more sense , You can use a very simple SQL query. Select Invoice_ID, the amount (amount) from the receipt group by invoice_id; 1 110 If you want the amount and latest receipt date. . .
Select invoice_id, maximum (receipt_date), amount (sum) from receipt group by invoice_id; 1 2014-04-24 110
Comments
Post a Comment