I'm trying to join two tables and sum the results from both tables columns but I can't figure out. I'm doing it in Joomla.
invoices table:
id
owner
amount
1
123
300.00
2
123
125.00
3
144
200.00
4
166
155.00
expenses table:
id
owner
amount
1
123
10.00
2
123
50.00
3
144
50.00
results:
owner
invoices
expenses
123
425.00
60.00
144
200.00
50.00
166
155.00
0.00
This should give you the expect results. I do LEFT JOIN for results from both tables.
SELECT l.`owner`, invoices, IFNULL(expenses,0) FROM
(
SELECT
`owner`, SUM(amount) AS invoices
FROM
invoices
GROUP BY
`owner`
) AS l LEFT JOIN (
SELECT
`owner`, SUM(amount) AS expenses
FROM
expenses
GROUP BY
`owner`
) AS r ON l.`owner` = r.`owner`
Related
I have two tables, customer and customer_order
Customer
cust_id, cust_name
121 Acme Wholesalers
234 Griffen Electric
336 East Coast Marine Supplies
544 Sanford Automotive
Customer_Orders
order_num,cust_id,order_date
1 121 2019-01-15
2 234 2019-07-24
3 336 2020-05-02
4 121 2019-01-15
5 336 2020-03-19
6 234 2019-07-24
7 121 2019-01-15
8 336 2020-06-12
I need to write a query that returns the name of the customer who has placed exactly 3 orders. I cannot return the same customer name than once and I must use a correlated subquery against the customer_order table.
So far I have the following, which returns all four customers.
select c.cust_name
from customer c
where exists
(select count(*)
from customer_order co
where c.cust_id = co.cust_id);
I need to add the = 3 qualifier but not sure where.
Try this:
select c.cust_name, count(co.id) as num_orders
from customer c join customer_order co on c.cust_id = co.cust_id
group by c.cust_name
having num_orders = 3;
I am assuming, that cust_name is unique, else you must group by cust_id.
Here is my table
loan_id bid_id lender_id borrower_id amount interest duration loan_status
1 1 60 63 300.00 12.00 3 'completed'
2 2 61 63 300.00 12.00 3 'completed'
3 3 62 63 300.00 12.00 3 'pending',
4 1 62 63 300.00 12.00 3 'pending'
7 4 60 63 300.00 12.00 3 'completed'
I want to pull only those bid_id whose loan_status of all records is completed. It means if there is any record of bid_id with status pending then it will not pull that record.
I am using the followin query that is working fine:
SELECT bid_id
FROM loan
WHERE bid_id NOT IN (
SELECT l.bid_id
FROM loan l
WHERE l.`loan_status` = 'pending'
AND l.bid_id = bid_id
GROUP BY l.`bid_id`
HAVING COUNT(l.`bid_id`)>= 1
)
GROUP BY bid_id
Is there any other way in which we can get desired result without using sub query.
You can readily do this with group by and having:
select bid_id
from loan
group by bid_id
having sum(loand_status = 'pending') = 0
I have 6 tables and 3 of them is what I need to show in my table and the other 3 is what I needed to hide in the same table.
Report:
id name branch comp_id start_date end_date
100 A 001 011 2022-08-14 2022-08-14
200 B 002 012 2022-08-14 2022-08-14
Report Details:
id product_id product_code price deliveries
100 01 11 20.00 10
100 01 11 20.00 10
200 01 11 20.00 20
200 02 12 25.00 20
Products:
id code name
01 11 Prod 1
02 12 Prod 2
Product Details:
id code name Desc
01 11 Prod 1 Desc 1
02 12 Prod 2 Desc 2
Branches:
id code name
001 021 Branch 1
002 022 Branch 2
Companies:
id name branch
011 Company 1 021
012 Company 2 022
I want the output to be like this:
id: will come from reports table
branch_name: will come from branches table using the branch in report table
company_name: will come company using the comp_id in report table
product_name: will come from products table using product_id in report details table
description: will come from product_details table using product_code in report details table
start_date: will come from report table
end_date: will come from report table
id branch_name company_name product_name description start_date end_date
100 branch 1 Company 1 Prod 1 Desc 1 2022-08-14 2022-08-14
200 branch 2 Company 2 Prod 1 Desc 1 2022-08-14 2022-08-14
200 branch 2 Company 2 Prod 2 Desc 2 2022-08-14 2022-08-14
I have this sql and it all shows the id in report details table:
SELECT *, `acc`.`name` AS `cname`, `out`.`name` AS `outname`, `pro`.`name` AS `pname`, `prod`.`name` AS `sname`
FROM `report` AS `rep`
JOIN `companies` AS `acc` ON `rep`.`account_id`=`acc`.`code`
JOIN `branches` AS `out` ON `rep`.`outlet_id`=`out`.`code`
JOIN `report_details` AS `red` ON `rep`.`report_id`=`red`.`report_id`
JOIN `products` AS `pro` ON `red`.`product_id`=`pro`.`id`
JOIN `product_details` AS `prod` ON `red`.`sku_id`=`prod`.`id`
Try this:
SELECT R.id, B.name AS branch_name, C.name AS company_name, PD.product_name AS product_name, PD.Desc AS description, R.start_date
FROM Report R
LEFT JOIN report_details RD ON R.id = R.id
LEFT JOIN products P ON RD.product_id = P.id
LEFT JOIN product_details PD ON PD.product_id = PD.id
LEFT JOIN branches B ON R.branch = B.id
LEFT JOIN companies C ON R.comp_id = C.id
GROUP BY R.id, B.name, C.name, PD.product_name, PD.Desc, R.start_date
I want to return a report that looks like the following:
unit | days | stays | income | charges | mgmt fee | bal
1001 20 6 775.00 1500.00 310.00 0.00
1002 40 14 5000.00 200.00 2100.00 2700.00
1003 50 20 6000.00 10.00 2500.00 3490.00
So the bal is (income - (charges+mgmt fee).
I have tables that look like this:
Unit
id | name | mgmt_fee
1001 blossom 30
1002 charlie 25
1003 deniro 30
1004 archie 20
1005 lilly 25
The mgmt fee is used as a percentage (%)
Reservations
id | unit | arrival | depart | total_price
10111 1001 2014-02-09 2014-02-13 400.00
10012 1001 2014-03-10 2014-03-15 300.00
10145 1002 2014-04-01 2014-04-05 600.00
10043 1003 2014-05-30 2014-06-03 350.00
NOTE: these are not actual data. It is a representation of my fields and what their data may look like, though.
Charges
id | unit | amount | charged_dtm
1 1001 40.00 2014-03-24 19:04:31
2 1001 30.00 2014-03-25 20:51:08
3 1002 100.00 2014-04-05 12:52:25
**There are cases where there may not be charges for a unit in a given month.
I have tried the following query:
SELECT u.name AS unit,
(r.departure-r.arrival) AS days,
COUNT(r.id) AS stays,
SUM(r.total_price) AS income,
SUM(c.amount) AS charges,
(SUM(r.total_price)*(.01*u.mgmt_fee)) AS management,
((SUM(r.total_price)-(SUM(r.total_price)*
(.01*u.mgmt_fee)))-SUM(c.amount)) AS bal
FROM reservations r
JOIN units u ON r.unit = u.id
JOIN charges c ON u.id = c.unit
GROUP BY u.id
It will return one unit and all the other data is totaled together. It is really odd.
Now, I tried removing everything and adding on one a time to find the culprit.
I found that up to this query I am good.
SELECT CONCAT(u.unit_name,', "',u.unit_nickname,'"') AS unit,
SUM(r.departure-r.arrival) AS days,
COUNT(r.id) AS stays,
SUM(r.total_price) AS income
FROM reservations r
JOIN units u ON r.unit = u.id
GROUP BY u.id
Once I add in the charges is when the query goes astray.
Any ideas on how to accomplish my desired result?
I think you can't do it all in one go as you are trying to because you want to group/sum items from different tables at the same time. Try this:
select unit, name, stays, days, total_price, charges, mgmt_fee,
(total_price*.01*mgmt_fee) AS management,
(total_price-(total_price*.01*mgmt_fee)-charges) AS bal
from (
select
x.unit,
name,
count(*) as stays,
sum(days) as days,
sum(total_price) as total_price,
charges,
mgmt_fee
from
(select
unit ,
datediff(depart,arrival) as days,
total_price
from
Reservations
) as x
join (
select unit, sum(amount) as charges
from Charges
group by unit
) as y
on x.unit = y.unit
join Unit as u
on u.id = x.unit
group by unit
) as inner_result
Not very tidy or elegant, but I think it works.
Note that you will have to be careful if there can be > 1 row in charges per unit.
Here is a fiddle to show it running: http://sqlfiddle.com/#!2/f05ba/18
I have two similar tables (one for bills, the other payments) right now I show to users a union mixing data from both..
Table Bills
CustomerId Amount
1 100
2 100
1 100
2 100
Table Payments
CustomerId Amount
1 100
2 100
1 100
Right now my users can see the following informtation
From Customer 1
Type CustomerId Amount
B 1 100
P 1 -100
B 1 100
P 1 -100
TOTAL 0
From Customer 2
Type CustomerId Amount
B 1 100
P 1 -100
B 1 100
Total 100
Everything works fine using UNION Statement
Right now I need to show Partial Balance on each record so users can keep in mind balanca while they are looking at records..
Like this... (DESIRED)
From Customer 1
Type CustomerId Amount Partial
B 1 100 100
P 1 -100 0
B 1 100 100
P 1 -100 0
I've alredy try using Variables like #Partial := #Partial + Amount but it seems that it first sum the first part (bills) and then the rest (payments)... like this...
From Customer 1
Type CustomerId Amount Partial
B 1 100 100
P 1 -100 200
B 1 100 200
P 1 -100 100
it seems that first sum everything from bills and then start subtraction... anyone knows how to solve it?
****** // update // ********
here original query ...
(SELECT 'Bill' as cType , b.type, b.tal , 'Customer', b.number , b.date , b.subtot, b.tax, IF(b.type='CA' or b.type='CB' or b.type='CC' or b.type='CX',b.total*-1,b.total) as total FROM bills b WHERE b.idcustomer='000140') UNION ALL
(SELECT 'Payment' as cType, 'CO' , '1' , '' , c.idcash , c.date , 0 ,0 , -c.amount FROM cash c WHERE c.idcustomer='000140' and ( c.type='CO' or c.type='DM') ) order by date asc;
this brings something like this
Bill FX 1 Customer 9 2011-02-25 0.00 0.00 100.00
Payment CO 1 37 2011-03-04 0.00 0.00 -100.00
Bill FX 1 Customer 616 2011-03-23 0.00 0.00 100.00
Payment CO 1 751 2011-04-12 0.00 0.00 -100.00
Bill FX 1 Customer 1267 2011-04-27 0.00 0.00 100.00
Payment CO 1 1157 2011-05-10 0.00 0.00 -100.00
Bill FX 1 Customer 1974 2011-05-26 0.00 0.00 100.00
Payment CO 1 1654 2011-06-08 0.00 0.00 -100.00
then When I try to sum patiars...using the following code
set #running_total=0;
(SELECT 'Bill' as cType , b.type, b.tal , 'Customer', b.number , b.date , b.subtot, b.tax, IF(b.type='CA' or b.type='CB' or b.type='CC' or b.type='CX',b.total*-1,b.total) as total, ( #running_total := #running_total + total) AS RunningTotal FROM bills b WHERE b.idcustomer='000140') UNION ALL
(SELECT 'Payment' as cType, 'CO' , '1' , '' , c.idcash , c.date , 0 ,0 , -c.amount, ( #running_total := #running_total-c.amount) AS RunningTotal FROM cash c WHERE c.idcustomer='000140' and ( c.type='CO' or c.type='DM') ) order by date asc;
results...
Bill FX 1 Customer 9 2011-02-25 0.00 0.00 100.00 100.00
Payment CO 1 37 2011-03-04 0.00 0.00 -100.00 1905.00
Bill FX 1 Customer 616 2011-03-23 0.00 0.00 100.00 200.00
Payment CO 1 751 2011-04-12 0.00 0.00 -100.00 1805.00
Bill FX 1 Customer 1267 2011-04-27 0.00 0.00 100.00 300.00
Payment CO 1 1157 2011-05-10 0.00 0.00 -100.00 1705.00
As you Can See seems to sum first all from bills and then start substraccion from payments...
The following is the only way I can think of doing this in MySQL, assuming that you have a time stamp for ordering the records. If you have even a moderate amount of data, this may prove to be too inefficient:
select t.*,
((select sum(amount)
from bills b
where b.customerId = t.customerId and
b.datetime <= t.datetime
) -
(select sum(amount)
from payments p
where p.customerid = t.customerid and
p.datetime <= t.datetime
)
) as balance
from ((select 'B' as type, customerid, amount, datetime from bills b) union all
(select 'P', customerid, amount, datetime from payments p)
) t