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
Related
I am trying to retrieve rows with same Volume value or with only 1 Volume, but could not come up with a SQL logic.
Data:
ID
Volume
A
100
A
100
B
101
B
102
B
103
B
104
C
400
Required Output:
ID
Volume
A
100
A
100
C
400
This one is achievable using a subquery.
select * from test where col1 in (
select t.col1
from(
select col1, col2,
dense_rank() over (partition by col1 order by col2) as dr
from test) t
group by t.col1
having sum(case when t.dr = 1 then 0 else t.dr end) = 0)
Try this dbfiddle.
This can be done on a more easy way:
select t1.id,
t1.volume
from tbl t1
inner join (select id
from tbl
group by id
having count(distinct volume) = 1
) as t2 on t1.id=t2.id;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=92bc234e631a1106b0e322bc4954d696
having count(distinct volume) = 1 will return only the id that have the same volume , including the id with just one volume.
I'd naturally be inclined towards Ergest Basha's pattern.
It can also be expressed using NOT EXISTS()
SELECT
t.*
FROM
tbl AS t
WHERE
NOT EXISTS (
SELECT *
FROM tbl
WHERE id = t.id
AND volume <> t.volume
)
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=243c42008f527391514d1ad124730587
I have following data in my table t1
unique_id myid price1 price2 price3 price4
1 100 20 30 40 50
2 200 12 24 48 90
3 100 15 20 30 25
4 300 100 200 300 400
5 400 10 10 20 40
6 100 5 6 7 8
7 200 1 2 3 4
Now i want data of particular myid and whose price3 is minimum
Expected output :
unique_id myid price1 price2 price3 price4
1 100 5 6 7 8
2 200 1 2 3 4
4 300 100 200 300 400
I have tried following query :
select t1.*,t2.txn_amount from table1 t1
left join table1 t2 on t2.ummyid = t1.myid
where t1.myid IN (100,200,300)
GROUP by t1.myid HAVING min(price3)
But it is not working as expected.
If your db version not allow over partition` you could use a join with the subquery for min values
select * from table1 t1
inner join (
select myid, min(price3) min_price
from table1
group by myid
) t on t.myid = t1.myid
and t.min_price = t1.price
And for you case
select t1.*
, t2.txn_amount
from table1 t1
inner join (
select myid, min(price3) min_price
from table1
group by myid
) t on t.myid = t1.myid
and t.min_price = t1.price
left join table1 t2 on t2.ummyid = t1.myid
where t1.myid IN (100,200,300)
GROUP by t1.myid
If you are using MySQL 8.0 then following solution should work using window function row_number. Here is the demo.
select
unique_id,
myid,
price1,
price2,
price3,
price4
from
(
select
unique_id,
myid,
price1,
price2,
price3,
price4,
row_number() over (partition by myid order by price3) as rn
from myTable
) subq
where rn = 1
You have to use analytic functions, please use below query
select unique_id, myid, price1, price2, price3, price4 from
(slect unique_id, myid, price1, price2, price3, price4,
row_number() over(partition by myid order by price3) as rnk from table) qry
where rnk = 1;
I think that this solution should work:
Select distinct myid,
min(price3)
from t1 group by myid
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;
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
I have 3 queries, the first one is like this
SELECT amount FROM table1
WHERE id = "1"
the output of this is
amount
10000
then my second query
SELECT SUM(hours * 10) as hours FROM table2
WHERE empid = "1"
output is
hours
400
and then my third query
SELECT totalcost FROM table3
WHERE id = "1"
output
totalcost
5000
I added the value of the second and third query
SELECT 'SUMQ2Q3',
(SELECT SUM(hours * 10) as hours FROM table2
WHERE empid = "1")
+
(SELECT totalcost FROM table3
WHERE id = "1")
and got the output of
5400
Next thing that I want to happen is to minus the value of the first query to the output i got when i sum the second and third query.
But i keep on getting a syntax error. It currently looks like this
SELECT amount FROM table1
WHERE id = "1"
-
SELECT
(SELECT SUM(hours * 10) as hours FROM table2
WHERE empid = "1"
+
SELECT totalcost FROM table3
WHERE id = "1")
How is the right way to do this?
Unfortunately no DB here, but
SELECT t1.amount - t2.sum + t3.totalcost
FROM (SELECT amount FROM table1 WHERE id = "1") as t1,
(SELECT SUM(hours * 10) as hours FROM table2 WHERE empid = "1") as t2
(SELECT totalcost FROM table3 WHERE id = "1") as t3
might do the job.
How about trying
SELECT
(SELECT amount FROM table1 WHERE id = "1")
- (SELECT SUM(hours * 10) as hours FROM table2 WHERE empid = "1")
- (SELECT totalcost FROM table3 WHERE id = "1")
SELECT (
(
SELECT amount FROM table1 WHERE id = "1") As x -
(SELECT SUM(hours * 10) as hours FROM table2 WHERE empid = "1") As y +
(SELECT totalcost FROM table3 WHERE id = "1") As z
)
As answer