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
Related
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;
Basically I need to merge these into one single query:
SELECT COUNT( DISTINCT id ) AS totalRows1
FROM other_events WHERE status = "approved"
AND Location = 1
SELECT COUNT( DISTINCT Id ) AS totalRows2
FROM core_events WHERE Status = "Active"
AND Location_id = 1
When I do it like below, if there is no event with Location_id = 1 query returns 0. In that condition I need it to return the count of the first table only.
SELECT COUNT( DISTINCT t1.id ) + COUNT( DISTINCT t2.Id ) AS total
FROM other_events AS t1, core_events AS t2
WHERE t1.status = "approved"
AND t1.Location = 1
AND t2.Location_id = 1
AND t2.Status = 'Active'
ps. column names are exactly like above
Use a UNION statement to merge the result like this:
SELECT SUM(total) FROM (
SELECT COUNT(DISTINCT id) AS total
FROM other_events
WHERE (status = "approved" AND Location = 1)
UNION ALL
SELECT COUNT(DISTINCT Id) AS total
FROM core_events
WHERE (Location_id = 1 AND Status = 'Active')
) union_result
This is my SQL query :
SELECT DISTINCT dest
FROM messages
WHERE exp = ?
UNION
SELECT DISTINCT exp
FROM messages
WHERE dest = ?
With this query, I get all the messages I've sent or received. But in my table messages, i have a field timestamp, and i need, with this query, add an order by timestamp... but how ?
You can do this without a union:
SELECT (case when exp = ? then dest else exp end), timestamp
FROM messages
WHERE exp = ? or dest = ?;
Then to get the most recent message for each participant, use group by not distinct:
SELECT (case when exp = ? then dest else exp end) as other, max(timestamp)
FROM messages
WHERE exp = ? or dest = ?
group by (case when exp = ? then dest else exp end)
order by max(timestamp) desc;
SELECT *
FROM
( SELECT
col1 = dest,
col2 = MAX(timestampCol)
FROM
messages
WHERE
exp = ?
GROUP BY
dest
UNION
SELECT
col1 = exp,
col2 = MAX(timestampCol)
FROM
messages
WHERE
dest= ?
GROUP BY
exp
) tbl
ORDER BY col2
This should return only one row per distinct exp / dest though I'm sure this could probably be done without a union; The GROUP BY will only get the most recent one.
Updated SQL: Given that it is possible for an exp on one record to equal a dest on the same or another record.
SELECT
CASE WHEN exp = ? THEN dest ELSE exp END AS col1
,MAX(timestampCol) AS col2
FROM
messages
WHERE
exp = ?
OR dest = ?
GROUP BY
(CASE WHEN exp = ? THEN dest ELSE exp END)
ORDER BY
MAX(timestampCol) DESC;
You might want to consider adding an SQL Fiddle with some dummy data to allow users to better help you.
SELECT *
FROM
(
SELECT DISTINCT dest AS Column1, timestamp AS Column2
FROM messages
WHERE exp = ?
UNION
SELECT DISTINCT exp AS Column1, timestamp AS Column2
FROM messages
WHERE dest = ?
) tbl
ORDER BY Column2
This query will help you in getting records order by timestamp
SELECT tbl.mailId, Max(timestampColumn) MostRecentDate
FROM
(
SELECT exp AS mailId, Max(timestampCol) AS timestampColumn
FROM Employeeatd
WHERE dest = ?
GROUP BY exp
UNION
SELECT Dept AS abc, Max(timestampCol) AS timestampColumn
FROM Employeeatd
WHERE exp = ?
GROUP BY Dept
) tbl
GROUP BY mailId
ORDER BY Max(timestampColumn) desc
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
I have some thing to do here with subquery but I am not able to do.
I want the result from a table with a extra field to show to no of results from other table with a column value from table 1.
table1:
CountryId Country ISO2 ISO3
table2:
id noof_country state
I have to retrive noof_country count in table 1 as count field
EDIT
my actual tables are
table 1:
ad_id job_country status delete days_left
table 2:
CountryId Country ISO2 status
I have done query in two phase:
$sql_map = "select distinct c.ISO2, c.Country, a.job_country
from rec_countries c, rec_advert a
where c.status = 1
and DATE(a.modified_date) >= CURDATE() - INTERVAL 30 DAY
and c.ISO2 <> '--'
and c.ISO2 <> ''
and c.CountryId = a.job_country
and a.status = 1
and a.`delete` = 0
and a.days_left >0
";
$res = mysql_query($sql_map);
while($row = mysql_fetch_array($res)){
$jobs_no = count($row['job_country']);
$sql_job = "SELECT COUNT( job_country ) AS jobs_no
FROM rec_advert
WHERE job_country = ".$row['job_country']."
and status = 1
and `delete` = 0
and days_left >0";
$resjob=mysql_query($sql_job);
$rowjob = mysql_fetch_array($resjob);
//here jobs_no is the count of total rows
}
Here I want to do with subquery.
If I read the question right, this should work:
SELECT
CountryId,
Country,
ISO2,
ISO3,
(
SELECT COUNT(DISTINCT noof_country)
FROM table2
WHERE table2.id = table1.CountryId
) AS noof_country_count
FROM table1
It's not immediately clear in your question which column in table1 is a foreign key to which column in table2... or if they are even related that way. If this query doesn't work for you, please clarify your schema.
Based on your updated information, try this:
select distinct c.ISO2, c.Country, a.job_country,
(
select COUNT(a2.job_country)
from rec_advert a2
where a2.job_country = a.job_country
and a2.status = 1
and a2.`delete` = 0
and a2.days_left >0
) as jobs_no
from rec_countries c, rec_advert a
where c.status = 1
and DATE(a.modified_date) >= CURDATE() - INTERVAL 30 DAY
and c.ISO2 <> '--'
and c.ISO2 <> ''
and c.CountryId = a.job_country
and a.status = 1
and a.`delete` = 0
and a.days_left >0