Mysql query from table 2 using value from table 1 - mysql

I want to search both table with all the records for cid 23 here
Total is table1-cid:23&w_id:1+2->(500+300),
Advance is table1-cid:23&w_id:1+2(100+100)+table 2-w_id:1+2(100+100+100+150)
Pending is Total-Advance
Tried using below query to display last table in pic with no luck.
SELECT E.cid, SUM(E.total) as Total, SUM(E.advance)as Advance, (SUM(E.total)-SUM(E.advance)- SUM(R.advance)) as Pending
FROM table1 AS E
LEFT JOIN table2 R ON E.w_id=R.w_id
WHERE (E.cid =23)

This is not the best query I've done but I obtain the result you want:
wwtest1 = table-1 , wwtest2 = table 2.
SELECT w1.cid AS cid,
(SELECT SUM(total) FROM wwtest WHERE cid = 23) AS total,
((SELECT SUM(advance) FROM wwtest WHERE cid = 23) + (SELECT SUM(advance) FROM wwtest2)) AS advance,
((SELECT SUM(total) FROM wwtest WHERE cid = 23) - ((SELECT SUM(advance) FROM wwtest WHERE cid = 23) + (SELECT SUM(advance) FROM wwtest2))) AS pending
FROM wwtest w1
WHERE w1.cid = 23 GROUP BY w1.cid;

Related

Count union of two table that has subquery

I have this working query. It has count with subquery.
SELECT
COUNT(*) AS total
FROM
(SELECT
COUNT(aset)
FROM
`public_1`
WHERE `public_1`.`aset` NOT IN
(SELECT
asset_code
FROM
application_detail
WHERE application_id = 6)
AND org_id = 7
AND status_id = 8
GROUP BY aset) t
now I need to union with different table and get the total from both table. This code could get count record but the value is incorrect.
SELECT
COUNT(*) AS total
FROM
(SELECT
COUNT(aset)
FROM
`public_1`
WHERE `public_1`.`aset` NOT IN
(SELECT
asset_code
FROM
application_detail
WHERE application_id = 6)
AND org_id = 7
AND status_id = 8
UNION
SELECT
COUNT(aset)
FROM
`public_2`
WHERE `public_2`.`aset` NOT IN
(SELECT
asset_code
FROM
application_detail
WHERE application_id = 6)
AND org_id = 7
AND status_id = 8
GROUP BY aset) z
Please assist me in getting the query correct. Thanks in advance
Use SELECT COUNT(DISTINCT aset) to get your counts, and then add them together.
SELECT t1.total + t2.total AS total
FROM (
SELECT COUNT(DISTINCT aset) AS total
FROM `public_1`
WHERE `public_1`.`aset` NOT IN
(SELECT
asset_code
FROM
application_detail
WHERE application_id = 6)
AND org_id = 7
AND status_id = 8) AS t1
CROSS JOIN (
SELECT COUNT(DISTINCT aset) AS total
FROM `public_2`
WHERE `public_2`.`aset` NOT IN
(SELECT
asset_code
FROM
application_detail
WHERE application_id = 6)
AND org_id = 7
AND status_id = 8) AS t2

Getting the sum of column grouped by date

Was wondering if there is a way to get the sum of the stock_case column for items with the same date_of_export ?
Updated with fiddle here and some relevant data:
https://www.db-fiddle.com/f/szC1Ftj3ZGEC24gSYp6ad4/4
The expected output would be this:
This is the query used
SELECT
st.product_code,
st.date_of_export,
st.best_before_date,
st.stock_case,
(
SELECT
SUM(st2.stock_case)
FROM
stock_tracking AS st2
WHERE
st2.product_code IN ('MGN003')
AND MONTH(st2.date_of_export) IN (07)
AND YEAR(st2.date_of_export) IN (2018)
AND st2.stock_case != 0
) AS total
FROM
stock_tracking st
WHERE
product_code IN ('MGN003')
AND MONTH(st.date_of_export) IN (07)
AND YEAR(st.date_of_export) IN (2018)
AND stock_case != 0
and my results
Would like to have a total column like 16, 16, 16, ... , 19, etc
For another case I used a subquery like so
SELECT
d.products_name,
stock_case,
st.date_of_export,
st.best_before_date,
st.product_code,
(SELECT
SUM(st2.stock_case)
FROM
stock_tracking AS st2
WHERE
DATE(st2.date_of_export) = (SELECT
DATE(tmp.last_update)
FROM
(SELECT
date_of_export AS last_update
FROM
stock_tracking
ORDER BY date_of_export DESC
LIMIT 1) AS tmp
WHERE
product_code = 'MGN003')) AS total
FROM
stock_tracking st
LEFT JOIN
products AS p ON p.products_model = st.product_code
LEFT JOIN
products_description AS d ON d.products_id = p.products_id
WHERE
product_code = 'MGN003'
AND d.language_id = 2
AND DATE(st.date_of_export) = (SELECT
DATE(tmp.last_update)
FROM
(SELECT
date_of_export AS last_update
FROM
stock_tracking AS st
ORDER BY date_of_export DESC
LIMIT 1) AS tmp)
with this result:
You can write a subquery to sum(stock_case) by date_of_export, then self join on Date, then you can get your expect result.
SELECT
s.product_name,
s.date_of_export,
s.best_before_date,
s.product_code,
s.stock_case,
t.totle
FROM
stock_tracking s
INNER JOIN
(
SELECT SUM(stock_case) totle,date_of_export dt
FROM stock_tracking
where
product_code = 'MGN003'
AND MONTH(date_of_export) =07
AND YEAR(date_of_export) =2018
AND stock_case != 0
GROUP BY date_of_export
) t on DATE_FORMAT(s.date_of_export, "%d-%m-%Y") = DATE_FORMAT(t.dt, "%d-%m-%Y")
where
s.product_code = 'MGN003'
AND MONTH(s.date_of_export) =07
AND YEAR(s.date_of_export) =2018
AND s.stock_case != 0
sqlfiddle
Without giving you the exact answer: You should think in the direction of:
SELECT SUM(column) FROM table WHERE ... GROUP BY date
or
SELECT SUM(column), DISTINCT date FROM table WHERE ...
So lookup the way GROUP BY and DISTINCT work :-)

MYSQL total count more than 1 table total result

I have 3 tables. I want to select count in 1 result, like:
table1=1000 records + table2=400 records + table3=200 records = 1600
1600 is the one result I want back from the server.
MySQL inner join perhaps? Any suggestions?
Try this :
select sum(c) from (
select count(*) as c from table1
union
select count(*) as c from table2
union
select count(*) as c from table3
) tmp
That'll give you the total.
select
(
select count(columnname) from table1
) + (
select count(columnname) from table2
)+ (
select count(columnname) from table3
)
try this...,
SELECT (SELECT COUNT(*) FROM tbl1
)+
(SELECT COUNT(*) FROM tbl2
)+
( SELECT COUNT(*) FROM tbl3
) as 'AllCount'
select
((select count(*) from table1) + (select count(*) from table2) + (select count(*) from table3))
as totalCount;
Try this:
SELECT ((SELECT COUNT(*) FROM tbl1 ) + (SELECT COUNT(*) FROM tbl2 ) + (SELECT COUNT(*) FROM tbl3 )) AS TotalRecords
Thank you all for your responses I have 3 tables and i want to select a count in 1 single result
still i get results back like this
count1 count2 count3
1235 134 234
and this is not what i want a total one result

MySQL SELECT Query - Subtract a SUM() value with the combined total of two other SUM() values

I have two SELECT statements that give the values "TotalSales" and "VendorPay_Com". I want to be able to subtract VendorPay_Com from TotalSales within the one MySQL statement to get the value "Outstanding_Funds" but I haven't found a reliable way to do so.
These are my two statements:
Query 1:
SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = 'Sold';
Query 2:
SELECT SUM(AC.Amount) AS VendorPay_Comm
FROM (
SELECT Amount FROM lawyer_pays_vendor
UNION ALL
SELECT CommissionEarned AS `Amount` FROM COMMISSION AS C WHERE C.`status` = 'Paid') AS AC
Any help on this matter would be greatly appreciated :)
You can do it as follows :
select (select ...) - (select ...)
In your example, simply :
select
(
SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = 'Sold'
)
-
(
SELECT SUM(AC.Amount) AS VendorPay_Comm
FROM (
SELECT Amount FROM lawyer_pays_vendor
UNION ALL
SELECT CommissionEarned AS `Amount` FROM COMMISSION AS C WHERE C.`status` = 'Paid') AS AC
) AS Outstanding_Funds
Try
SELECT TotalSales-VendorPay_Comm AS Outstanding_Funds
FROM
(SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = 'Sold') t1,
(SELECT SUM(Amount) AS VendorPay_Comm
FROM (SELECT Amount FROM lawyer_pays_vendor
UNION ALL
SELECT CommissionEarned AS Amount
FROM COMMISSION
WHERE Status = 'Paid') t0) t2
Here is sqlfiddle

Combine two Mysql SUM select query to one query

I have below two query's SUM the values
Query1:*
SELECT SUM(price) FROM TABLE1 WHERE acc_id = '555'
Query2:
SELECT SUM(price) FROM TABLE2 WHERE account = '555' && active='1'
I try to combine this two query but give wrong sum result , for example if query1 sum is: -86500 and Query2 sum is: 76000 , RESULT must be -10500 but result shown with a number like -486000
I'm trying like this, but i'm not getting expected result.
SELECT SUM(t1.price + t2.price) AS TotalCredit
FROM TABLE1 AS t1, TABLE2 AS t2
WHERE t1.`acc_id` = '555' && t2.`Account`='555' && t2.`Active`='1'
Table image :
Due to join the number of records get duplicated and you get a higher value for sum
try this
SELECT sum(prc)
FROM (
SELECT SUM(price) prc FROM TABLE1 WHERE acc_id = '555'
union all
SELECT SUM(price) prc FROM TABLE2 WHERE account = '555' && active='1'
) a
Try this
SELECT SUM(C.TOTAL) AS TOTAL_CREDIT FROM (SELECT SUM(A.price) AS TOTAL FROM TABLE1 A WHERE A.acc_id = '555'
UNION ALL
SELECT SUM(B.price) AS TOTAL FROM TABLE2 B WHERE B.account = '555' && B.active='1') C;
try that
SELECT (SUM(t1.price) + SUM(t2.price)) AS TotalCredit
FROM TABLE1 AS t1, TABLE2 AS t2
WHERE t1.`acc_id` = '555' && t2.`Account`='555' && t2.`Active`='1'
try this
SELECT (t1.price + t2.price) AS TotalCredit
FROM TABLE1 AS t1, TABLE2 AS t2
WHERE t1.`acc_id` = '555' && t2.`Account`='555' && t2.`Active`='1'
EDIT:
here what you looking for i think
SELECT (SUM(t1.price)+SUM(t2.price) )/2 AS TotalCredit
FROM Table1 AS t1, Table2 AS t2
WHERE t1.`acc_id` = '555' && t2.`account`='555' && t2.`active`='1'
DEMO FIDDLE HERE
How about this:
SELECT SUM(a)
FROM
(SELECT SUM(price) AS a
FROM TABLE1
WHERE acc_id = '555'
UNION ALL
SELECT SUM(price) AS a
FROM TABLE2
WHERE account = '555' && active='1')
Join could be better. :) Would be even better if you could have showed us the table schema. Here is a solution based on some assumed sample data.
Sample data:
Table1:
ID PRICE
11 200
55 300
33 200
44 100
55 500
Table2:
ID PRICE ACTIVE
1 200 0
2 300 1
55 200 0
55 100 1
55 400 1
SQLFIDDLE DEMO
Query:
select sum(t.price) + x.tb2credit
from tb1 as t
inner join
(SELECT id, SUM(price) AS Tb2Credit
FROM tb2
WHERE id = 55
and `Active`=1) x
on t.id = x.id
Results:
SUM(T.PRICE) + X.TB2CREDIT
1300