Here I want to get the previous row balance value in to my field.
The last id of customer_id '16' of balance is 200, but I want to get the previous ids value in to the field and this is my table
id order_id customer_id amount actual_amount paid_amount balance type
25 11 16 100.00 50.00 50.00 Cash
26 12 16 200.00 100.00 100.00 Cash
27 13 16 150.00 100.00 50.00 Cash
28 14 16 300.00 250.00 50.00 Cash
29 14 16 170.00 100.00 70.00 Cash
30 15 16 100 170.00 70.00 100.00 Cash
31 16 16 400 500.00 300.00 200.00 Cash
this is my model
public function order_balance($order_code)
{
$this->db->join('services','payment.customer_id=services.customer_id','left');
$this->db->select('payment.*,payment.balance,payment.actual_amount,payment.customer_id');
$this->db->order_by('payment.id','desc');
$query = $this->db->get_where('payment',array('code' => $order_code));
return $query->previous_row();
}
This is my control:
public function final_payment($order_code)
{
$data['active_mn']='';
$data['result'] = $this->Account_model->order_balance($order_code);
$this->load->view('final_payment',$data);
}
My services table looks like this:
id code customer_id particulars
11 ORD00011 16 phone
12 ORD00012 16 gdf
13 ORD00013 16 ghgfh
14 ORD00014 16 tv
15 ORD00015 16 ghfg
16 ORD00016 16 tv
17 ORD00017 16 gdfg
18 ORD00018 16 desk
19 ORD00019 16 gdf
My result should be like this:
id order_id customer_id amount actual_amount paid_amount balance type
31 16 16 400 500.00 300.00 100.00 Cash
One option here would be to do a self join with the payment table, joining on the customer_id and order_id columns. You can add another join function call and then use the result in your select. The following solution uses a raw query since Codeigniter does not seem to tolerate arithmetic in the join condition:
public function order_balance($order_code)
{
$this->db->query("SELECT p1.*, p2.balance AS previous_balance FROM payment p1 INNER JOIN payment p2 ON p1.order_id = p2.order_id + 1 AND p1.customer_id = p2.customer_id LEFT JOIN services s ON p1.customer_id = s.customer_id ORDER BY p1.id DESC");
$query = $this->db->get_where('p1', array('code' => $order_code));
return $query->previous_row();
}
The query has the form:
SELECT p1.*, p2.balance AS previous_balance
FROM payment p1 INNER JOIN payment p2
ON p1.order_id = p2.order_id + 1 AND
p1.customer_id = p2.customer_id
LEFT JOIN services s
ON p1.customer_id = s.customer_id
ORDER BY p1.id DESC
There are lots of ways of writng this. Here's one way. I don't know codeigniter, so you'll have to reverse engineer it...
SELECT a.id
, a.order_id
, a.customer_id
, a.amount
, a.actual_amount
, a.paid_amount
, b.balance
, a.type
FROM
( SELECT x.*
, MAX(y.id) prev
FROM payment x
JOIN payment y
ON y.id < x.id
AND y.customer_id = x.customer_id
GROUP
BY x.id
) a
JOIN payment b
ON b.id = a.prev
ORDER
BY id DESC LIMIT 1;
Related
I have a few MySQL tables from which I need to JOIN and return data. The return data must show only one row for one of the JOINed tables, but MySQL mixes the rows.
I have tried different methods using subqueries and normal JOIN with GROUP but the results remain pretty much the same.
Example table structure
suppliers
id name ...
1 ACME ...
2 EMCA ...
3 ORG ...`
ratings
id supplier_id rating expiry_date report_file
1 1 5.0 2017-01-31 a.pdf
3 1 7.9 2019-06-30 c.pdf
4 2 5.0 2016-01-31 d.pdf
5 2 2.0 2018-11-30 g.pdf
6 245 9.5 2009-03-31 p.pdf
spends
id report_id supplier_id amount
1 1 1 150.00
2 1 2 100.00
3 1 245 200.00
Here are example queries I have tried to resolve this and return the correct dataset with no luck.
SELECT
reports.id,
suppliers.id AS supplier_id,
suppliers.name,
...
spends.amount,
...
ratings.rating,
ratings.report_file,
ratings.expiry_date
FROM reports
INNER JOIN spends ON reports.id=spends.report_id
INNER JOIN suppliers ON spends.supplier_id=suppliers.id
LEFT JOIN (
SELECT id,
level,
report_file,
supplier_id,
MAX(expiry_date) AS expiry_date
FROM ratings
GROUP BY supplier_id
) ratings ON (ratings.supplier_id=suppliers.id
AND ratings.expiry_date >= reports.period_start)
...
WHERE reports.id = 1
GROUP BY spends.id
ORDER BY spends.amount DESC
Another query
SELECT
reports.id,
suppliers.id AS supplier_id,
suppliers.name,
...
spends.amount,
...
ratings.rating,
ratings.report_file,
MAX(ratings.expiry_date) AS expiry_date
FROM reports
INNER JOIN spends ON reports.id=spends.report_id
INNER JOIN suppliers ON spends.supplier_id=suppliers.id
LEFT JOIN ratings ON (ratings.supplier_id=suppliers.id
AND ratings.expiry_date >= reports.period_start)
...
WHERE reports.id = 1
GROUP BY spends.id
ORDER BY spends.amount DESC
I expect the results to be
id supplier_id name amount rating report_file expiry_date
1 1 ACME 150.00 7.9 c.pdf 2019-06-30
1 2 EMCA 100.00 2.0 g.pf 2018-11-30
1 245 MACE 200.00 null null```
However, the actual output is
```sql
id supplier_id name amount rating report_file expiry_date
1 1 ACME 150.00 5.0 a.pdf 2019-06-30
1 2 EMCA 100.00 5.0 d.pf 2018-11-30
1 245 MACE 200.00 null null
Please could anyone advise how I can fix this.
try a query like this:
SELECT
reports.id,
suppliers.id AS supplier_id,
suppliers.name,
...
spends.amount,
...
r.rating,
r.report_file,
r.expiry_date
FROM reports
INNER JOIN spends ON reports.id=spends.report_id
INNER JOIN suppliers ON spends.supplier_id=suppliers.id
LEFT JOIN ratings r ON r.supplier_id=suppliers.id
AND r.`expire_date` = (
SELECT MAX(`expire_date`) FROM ratings WHERE supplier_id = supplier.id GROUP BY supplier_id)
) as maxdate
...
WHERE reports.id = 1
GROUP BY spends.id
ORDER BY spends.amount DESC
Here am having two tables namely rent and student_hostel.the rent table looks like this
id date stud_id paid balance
18 10-2016 94 15000 15000
19 10-2016 94 10000 5000
20 10-2016 96 25000 5000
21 10-2016 96 5000 0
my student_hostel table looks like this..
id first_name last_name stud_id admit_date hostel class room bed status
94 ss ff PHBH00094 01-10-2016 12 16 115 501A P
96 maltu uv PHBH00096 01-10-2016 12 16 115 501C p
In order to get the last inserted stud_id's balance i used my code like this,
public function rent_outstanding($hos,$dt)
{
$sql = "select s.stud_id ,s.admit_date ,s.class,first_name,sum(paid) as rt_paid,balance,rt.stud_id
from student_hostel s, rent rt where s.id=rt.stud_id and hostel=? and rt.date=?
and rt.id = (select max(id) from rent r where r.stud_id = rt.stud_id and r.date='$dt')
and status!= 'G' and status!= 'R' GROUP BY rt.stud_id";
$query=$this->db->query($sql, array($hos, $dt));
return $query;
}
the problem am facing here is i was not able to sum the values comes under the paid columns of same stud_id.
the output am getting is like this
SI.No STUDENT ID NAME RENT PAID BALANCE
1 PHBH00094 Ss 30000 10000 5000
2 PHBH00096 Maltu 30000 5000 0
the desired output i need to get is like this
SI.No STUDENT ID NAME RENT PAID BALANCE
1 PHBH00094 Ss 30000 25000 5000
2 PHBH00096 Maltu 30000 30000 0
Your final solved query is :
select s.stud_id ,s.admit_date ,s.class,first_name,sum(paid) as rt_paid,
(select r.balance from rent r where r.stud_id = rt.stud_id and r.date='$dt' order by r.id desc limit 1) as balance ,rt.stud_id
from student_hostel s join rent rt on s.id=rt.stud_id where hostel=? and rt.date=?
and status!= 'G' and status!= 'R' GROUP BY s.stud_id
I have removed this line in your existing query rt.id = (select max(id) from rent r where r.stud_id = rt.stud_id and r.date='$dt')
your (select max(id) from rent r where r.stud_id = rt.stud_id and r.date='$dt') means all time getting only one last row rt.id like 19 (for PHBH00094) or 21 (for PHBH00096).
that's why you getting all time one last calculation.
I am stuck on a query where i have to show list of manufacturers and the Amount of Loan they took and Amount of Refund they returned. i have generated the query but i want to sum total no: of refunds of a manufacturer and total no: of Loans manufacturer has taken. and the difference , that is LoanTaken - RefundAmount = Remaining Amount. If its possible in the query.
SELECT MM.*, ML.*, MR.* FROM microfinance_manufacturers AS MM
INNER JOIN manufacturer_loans AS ML ON MM.ManufacturerId = Ml.ManufacturerId
INNER JOIN manufacturer_refunds AS MR ON MM.ManufacturerId = MR.manufacturerId
WHERE 1 = 1
ManufacturerId FirstName LastName Gender Religion PhoneNumber EmailAddress Notes CustomerAddedDateTime manufacturerTypeId manufacturerRoles LoanID ManufacturerId LoanAmount LoanDate RefundId manufacturerId RefundAmount RefundDate
5 Saud Jibran 0 0 8475983748G HFDKJFH VGHJXGVHJD 2015-04-29 14:12:20 21 O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_ 6 5 65644343:1:"2015-05-06Em 6 5 77744s_Station2015-05-06:{
5 Saud Jibran 0 0 8475983748G HFDKJFH VGHJXGVHJD 2015-04-29 14:12:20 21 O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_ 13 5 543543;s:1:"2015-05-07Em 6 5 77744s_Station2015-05-06:{
7 Naveed Ahmed 0 0 847893 hfkjhfskj fjksddshkfjdshfkj 2015-04-29 14:22:16 19 O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_ 16 7 8798u656:1:"2015-05-07Em 9 7 4354334Station2015-05-07:{
7 Naveed Ahmed 0 0 847893 hfkjhfskj fjksddshkfjdshfkj 2015-04-29 14:22:16 19 O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_ 16 7 8798u656:1:"2015-05-07Em 10 7 896789798ation2015-05-07:{
7 Naveed Ahmed 0 0 847893 hfkjhfskj fjksddshkfjdshfkj 2015-04-29 14:22:16 19 O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_ 17 7 87987687:1:"2015-05-07Em 9 7 4354334Station2015-05-07:{
7 Naveed Ahmed 0 0 847893 hfkjhfskj fjksddshkfjdshfkj 2015-04-29 14:22:16 19 O:16:"clsEmployeeRoles":68:{s:56:"aEmployeeRole_Organization_RegionalHierarchy_RegionTypes";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:52:"aEmployeeRole_Organization_RegionalHierarchy_Regions";a:4:{i:0;i:0;i:1;i:0;i:2;i:0;i:3;i:0;}s:69:"aEmployeeRole_Organization_RegionalHierarchy_Regions_RegionStatistics";a:2:{i:0;i:0;i:2;i:0;}s:82:"aEmployeeRole_Organization_RegionalHierarchy_Regions_ 17 7 87987687:1:"2015-05-07Em 10 7 896789798ation2015-05-07:{
I have attached the pic of the output i am getting...
tables are manufacturer,loan,refund, manufacutrerId is common in all tables.
now in the pic it shows repeated record as the manufacturer took loan 2 times but return amount 1 time.. but it repeats in refund column. Please Help !!!
This query should work:
SELECT ManufacturerId,FirstName,LA,RA, LA - RA as RemainingAmount FROM (SELECT MM.ManufacturerId,MM. FirstName,SUM(LoanAmount) as LA, SUM(RefundAmount) as RA FROM microfinance_manufacturers AS MM
INNER JOIN manufacturer_loans AS ML ON MM.ManufacturerId = Ml.ManufacturerId
INNER JOIN manufacturer_refunds AS MR ON MM.ManufacturerId = MR.manufacturerId
GROUP BY MM.ManufacturerId) as a
You need to do the aggregations before the join. Otherwise, you end up with a cartesian product that throws off the values:
SELECT MM.*, ML.*, MR.*
FROM microfinance_manufacturers MM left join
(select Ml.ManufacturerId, count(*) as numloans, sum(ml.amount) as loanamount
from manufacturer_loans ML
group by ManufacturerId
) ml
on MM.ManufacturerId = Ml.ManufacturerId left join
(select Mr.ManufacturerId, count(*) as numrefunds, sum(ml.amount) as refundamount
from manufacturer_refunds mr
group by ManufacturerId
) mr
on MM.ManufacturerId = MR.manufacturerId;
Your question is unclear both on the data layout and on the expected results. But this should be basically what you need.
1.Purchase table
Id user pur_type
----------------------
1 408 5-12
2 408 5-12
3 222 1-11
4 222 5-12
5 408 1-11
6 408 1-11
2. goods table
Id parts days
-----------------
1 1-11 50
2 5-12 40
I implemented such query
SELECT p1.id, p1.user, p1.pur_type, g.parts, g.days
FROM purchase p1
INNER JOIN goods g ON p1.pur_type = g.parts
LEFT JOIN goods p2 ON ( p1.pur_type= p2.pur_type
AND p1.id < p2.id )
WHERE p2.id IS NULL
The result is only last record for each pur_type
Id user pur_type parts days
---------------------------------
4 222 5-12 5-12 40
6 408 1-11 1-11 50
How to get last record from purchase table of each pur_type for one specific user?
for example for user 408 need result:
Id user parts days
-------------------------
2 408 5-12 40
6 408 1-11 50
Result explanation:
last record fot part 5-12 for user 408 is id=2
last record for part 1-11 for user 408 is id=6
SELECT a.*, c.* -- select columns youw ant
FROM Purchase a
INNER JOIN
(
SELECT user, MAX(ID) max_ID
FROM Purchase
GROUP BY user, pur_type
) b ON a.user = b.user AND
a.ID = b.max_ID
INNER JOIN goods c
ON a.pur_type = c.parts
WHERE a.USER = 408
SQLFiddle Demo
for better performance, add an INDEX on column parts on table goods as well on pur_type
A simple modification to the existing query:
SELECT p1.id, p1.user, p1.pur_type, g.parts, g.days
FROM purchase p1
INNER JOIN goods g ON p1.pur_type = g.parts
LEFT JOIN purchase p2
ON ( p1.pur_type= p2.pur_type AND p1.id < p2.id AND p1.user=p2.user)
WHERE p2.id IS NULL and p1.user = 408
I have this table:
idTransactions idCampaignsList idMemberCard amountOriginal amountFinal dateTransaction
1 2 1 50.00 100.00 2012-10-31 12:45:41
2 3 1 0.00 -50.00 2012-10-31 12:47:25
3 2 2 255.00 255.00 2012-10-31 17:19:07
4 1 2 95.00 95.00 2012-11-02 20:38:36
5 3 2 0.00 -400.00 2012-11-02 20:39:50
24 1 4 10.00 2.00 2012-11-03 11:16:3
With this query
SELECT SUM(amountOriginal) AS euro,
SUM(amountFinal) AS deducted,
EXTRACT(YEAR_MONTH FROM(dateTransaction)) AS period
FROM transactions
INNER JOIN campaignsList ON campaignsList.idCampaignsList = transactions.idCampaignsList
INNER JOIN customers ON customers.idCustomer = campaignsList.idCustomer
WHERE customers.idCustomer = 14
AND
transactions.idCampaignsList = 2
GROUP BY period
ORDER BY period
I obtain this result
euro deducted period
305.00 305.00 201210
14860.46 -22758.50 201211
1845.00 -34710.00 201212
For last 12 month, sum of "charged" and discharged.
Now, idCampaignsList could be 1, 2, also 500, it depend on how many "campaigns" have my idCustomer (retrieved via JOIN).
I'd like have a query dinamic, that, "for each" idCampaignsList, print me sum of amountOriginal and amountFinal.
To intend, from previos table, i would like to have
idCampaignsList SUM(amountOriginal) SUM(amountFinal) period
1 50 50 201210
2 255 255 201210
2 95 -305 201211
4 10 2 201211
So, for every period, sum columns for every distinct idCampaignsList, where idCampaignsList is dinamically (SELECT idCampaignsList FROM myOtherTable where idCustomer = 14)
I'd like have a query dinamic, that, "for each" idCampaignsList, print
me sum of amountOriginal and amountFinal.
I think the several For each that you mean, is a GROUP BY transactions.idCampaignsList.
Try to add the transactions.idCampaignsList to the SELECT list, remove the predicate transactions.idCampaignsList = 2 from the WHERE clause and list that column in the GROUP BY clause as well, like so:
SELECT
transactions.idCampaignsList
SUM(amountOriginal) AS euro,
SUM(amountFinal) AS deducted,
EXTRACT(YEAR_MONTH FROM(dateTransaction)) AS period
FROM transactions
INNER JOIN campaignsList
ON campaignsList.idCampaignsList = transactions.idCampaignsList
INNER JOIN customers
ON customers.idCustomer = campaignsList.idCustomer
WHERE customers.idCustomer = 14
GROUP BY period, transactions.idCampaignsList
ORDER BY period