How to SUM the result of two different SQL query? - mysql

I want to SUM these two different queries. I joined both with USING function. but it shows the two results in two different cells.
I need a single result to SUM these two queries where the answer should be 4069.
The CODE
SELECT COUNT(*) Active_Projects FROM
(SELECT ProjectID, ProjectStatusID, ClientID
FROM project) a
INNER JOIN
(SELECT ProjectStatusID, ClientID, ProjectStatusName
FROM ProjectStatus) b
ON (a.ProjectStatusID = b.ProjectStatusID AND a.ClientID = b.ClientID)
WHERE ProjectStatusName LIKE '%active%'
AND a.ClientID = 4
UNION
SELECT COUNT(*) Total_Projects
FROM Project
WHERE ClientID=4
Picture
Screenshot
Screenshot Here

Try this. select query1+ query2;
select (
SELECT COUNT(*) Active_Projects FROM
(SELECT ProjectID, ProjectStatusID, ClientID
FROM project) a
INNER JOIN
(SELECT ProjectStatusID, ClientID, ProjectStatusName
FROM ProjectStatus) b
ON (a.ProjectStatusID = b.ProjectStatusID AND a.ClientID = b.ClientID)
WHERE ProjectStatusName LIKE '%active%'
AND a.ClientID = 4
) + (
SELECT COUNT(*) Total_Projects
FROM Project
WHERE ClientID=4) ;

Related

Employee hierarchy recursive query - Mysql

I have an employee table called resources. the resources table has info about the resources and their managers along with their levels.
I need a query to always return the L7 manager (not the immediate manager)
This is what I've come up with so far.
But the problem in this is because of the where condition where mananger_level = 7 and Im joining cte with resource_alias, a few resources are getting filtered out
with recursive cte (resource_alias, manager_alias, manager_level) as (
select resource_alias,
manager_alias,
manager_level
from rpt.resources
union all
select r.resource_alias,
r.manager_alias,
r.manager_level
from rpt.resources r
inner join cte
on r.resource_alias = cte.manager_alias
)
select rr.resource_full_name,
m.resource_id,
rr.manager_level,
m.resource_alias,
r.resource_type,
r.resource_home_city,
r.resource_home_state,
r.resource_home_country,
r.resource_home_zip,
r.eligible_program_id,
rr.manager_alias,
rr.manager_name,
m.skip_reason
from rpt.model_input_resources m
join rpt.resource_pool r on m.resource_alias = r.resource_alias
join rpt.programs p on find_in_set(p.id, r.eligible_program_id) > 0
join rpt.resources rr on rr.resource_alias = r.resource_alias
left join cte on cte.resource_alias = rr.resource_alias
where m.run_id in ('202301291674972257') and cte.manager_level = 7
and m.skip_reason <> 'Good' and r.portfolio in ('NACF') and r.resource_type in ('Launch Manager') and
m.resource_id not in ( select distinct(resource_id) from rpt.project_resource_recommendation_runs where run_id in ('202301291674972257') ) -- and p.program in (?)
group by m.resource_alias,
r.resource_type,
r.resource_home_city,
r.resource_home_state,
r.resource_home_country,
r.resource_home_zip,
rr.manager_alias,
m.skip_reason

SUM(DISTINCT) on the same table?

I currently have table as below:
By executing the code as:
SELECT FromLedger, SUM(TransactionVal)
FROM Journal
WHERE FromGroup = 'Asset'
GROUP BY FromLedger
I get the result as:
Simlarly, I get the another set by executing:
SELECT ToLedger, SUM(TransactionVal)
FROM Journal
WHERE ToGroup = 'Asset'
GROUP BY ToLedger
I want to get a single table combining the both table with Group By Ledger Id and another column as the difference between the SUM of the above 2 table. In other words, I am looking for table as below
How do I get it please?
You can join the 2 queries like this:
SELECT t.ToLedger UniqueLedgerId, t.total - f.total ClosingBalance
FROM (SELECT ToLedger, SUM(TransactionVal) total FROM Journal WHERE ToGroup = 'Asset' GROUP BY ToLedger) t
INNER JOIN (SELECT FromLedger, SUM(TransactionVal) total FROM Journal WHERE FromGroup = 'Asset' GROUP BY FromLedger) f
ON f.FromLedger = t.ToLedger
or use UNION ALL and then aggregate:
SELECT t.UniqueLedgerId, SUM(t.TransactionVal) ClosingBalance
FROM (
SELECT ToLedger UniqueLedgerId, TransactionVal FROM Journal WHERE ToGroup = 'Asset'
UNION ALL
SELECT FromLedger, -TransactionVal FROM Journal WHERE FromGroup = 'Asset'
) t
GROUP BY t.UniqueLedgerId

How to get data by using join in mysql

I have a table:
and this table:
I would like to create report like this
I've tried this SQL:
select
master_problem.problem,
master_problem.sop_reference,
master_problem.adidas_spec,
count(log_roving_qc.id_problem) as jumlahfrom
master_problem
inner join log_roving_qc
on master_problem.id_problem = log_roving_qc.id_problem
group by master_problem.id_problem
but the empty data does not show. I want to display blank data with a description of 0
Do a left join of the master_problem table to a subquery which does the count aggregation:
SELECT
mp.problem,
mp.sop_reference,
mp.adidas_spec,
COALESCE(t.cnt, 0) AS jumlahfrom
FROM master_problem mp
LEFT JOIN
(
SELECT id_problem, COUNT(*) as cnt
FROM log_roving_qc
GROUP BY id_problem
) t
ON mp.id_problem = t.id_problem;

mySQL change multiple queries into single query

I have two mySQL tables:
tblcoach: contains fields (coachid,coachschoolid, otherstuff)
tblschool: contains fields (schoolid,schooldivision)
I am trying to get a list of schools that is limited to the only those schools that are in the same schooldivision as a coach.
This code I have works, but is there someway to produce a single mySQL query to achieve the same results?
SELECT #cs := coachschoolid FROM tblcoach c;
SELECT #sd := s.schooldivision FROM tblschool s WHERE s.schoolid = #cs;
SELECT s2.schoolid, s2.schoolname FROM tblschool s2 WHERE schooldivision = #sd
Nest the queries:
select schoolId, schoolname
from tblschool
where
schooldivision = (
select schooldivision from tblschool where schoolid = (
select coachschoolid from tblcoach
)
)
This seems like it would work only with 1 coach.
You can use a self-join to do this:
SELECT s2.schoolid, s2.schoolname
FROM tblschool s
INNER JOIN tblschool s2 on s.schooldivision = s2.schooldivision
WHERE s.schoolid = ?
Allows you to pass in the schoolID. If you want to pass in the coachID:
SELECT s2.schoolid, s2.schoolname
FROM tblschool s
INNER JOIN tblschool s2 on s.schooldivision = s2.schooldivision
INNER JOIN tblcoach c on c.coachschoolid = s.schoolid
WHERE c.coachid = ?

SQL JOIN to count and SUM issue

I know that I am missing something, I have tried several different ways but I am overlooking (or trying too hard). Can someone please tell me where I am wrong on this SQL?
SELECT id,
COUNT(id) AS dupBlocks
FROM tbl_duplicates8 INNER JOIN (
tbl_accounts8,
tbl_delaccounts,
tbl_bad_bots,
tbl_log,
tbl_ipban,
tbl_ipban8
) ON (
tbl_accounts8.SUM(num_attacks) AND
tbl_delaccounts.SUM(noattacks) AND
tbl_bad_bots.COUNT(id) AND
tbl_log.COUNT(id) AND
tbl_ipban.COUNT(txt_ip) AND
tbl_ipban8.COUNT(ip)
);
I did notice this MySQL Join two tables count and sum from second table but it gives me a null on return.
Any help would be appreciated.
To further the question for a better answer, this is what I am currently doing:
$statsresults['newIPBan'] = $db->query("SELECT COUNT(ip) AS newIPBan FROM tbl_ipban8;");
$statsresults['oldIPBan'] = $db->query("SELECT COUNT(txt_ip) AS oldIPBan FROM tbl_ipban;");
$statsresults['log_blocks'] = $db->query("SELECT COUNT(id) AS logBlocks FROM tbl_log;");
$statsresults['badbots'] = $db->query("SELECT COUNT(id) AS badBots FROM tbl_bad_bots;");
$statsresults['del_num_attacks'] = $db->query("SELECT SUM(noattacks) AS deltotalattacks FROM tbl_delaccounts;");
$statsresults['num_attacks'] = $db->query("SELECT SUM(num_attacks) AS totalattacks FROM tbl_accounts8;");
$statsresults['dup_blocks'] = $db->query("SELECT COUNT(id) AS dupBlocks FROM tbl_duplicates8;");
Which will return this:
| ['newIPBan0newIPBan'] = String(6) "289033"
| ['oldIPBan0oldIPBan'] = String(6) "125723"
| ['log_blocks0logBlocks'] = String(4) "6481"
| ['badbots0badBots'] = String(5) "15310"
| ['del_num_attacks0deltotalattacks'] = String(9) "119494860"
| ['num_attacks0totalattacks'] = String(8) "25286478"
| ['dup_blocks0dupBlocks'] = String(6) "179916"
So right now it is calling the database 7 times to get each sum or count. I was hoping to change that to 1 database call and returning the sum of them all.
This is one way that you can combine them:
select (newIPBan + oldIPBan + logBlocks + badBots + deltotalattacks + totalattacks + dupBlocks
) as NumIPs
from (SELECT COUNT(ip) AS oldIPBan FROM tbl_ipban8) ipb8 cross join
(SELECT COUNT(txt_ip) AS newIPBan FROM tbl_ipban) ipb cross join
(SELECT COUNT(id) AS logBlocks FROM tbl_log) l cross join
(SELECT COUNT(id) AS badBots FROM tbl_bad_bots) bb cross join;
(SELECT coalesce(SUM(noattacks), 0) AS deltotalattacks FROM tbl_delaccounts) da cross join
(SELECT coalesce(SUM(num_attacks), 0) AS totalattacks FROM tbl_accounts8) ta cross join
(SELECT COUNT(id) AS dupBlocks FROM tbl_duplicates8) d;
Edit
I would personally create a view for the various metrics, along the following lines:
CREATE VIEW vwMetrics AS
SELECT 'newIPBan' AS Metric, COUNT(ip) AS Value FROM tbl_ipban8
UNION
SELECT 'oldIPBan', COUNT(txt_ip) FROM tbl_ipban
UNION
SELECT 'logBlocks', COUNT(id) FROM tbl_log
UNION
SELECT 'badBots', COUNT(id) AS badBots FROM tbl_bad_bots
UNION
SELECT 'deltotalattacks', SUM(noattacks) FROM tbl_delaccounts
UNION
SELECT 'totalattacks', SUM(num_attacks) FROM tbl_accounts8
UNION
SELECT 'dupBlocks', COUNT(id) FROM tbl_duplicates8;
And then you can aggregate the components:
SELECT SUM(Value) AS TotalOfEverything
FROM vwMetrics;
The benefit of the view is that you can drill down into the components for detail / debugging purposes, rather than arrive at a magical total - there might be some reuse for the view to be had elsewhere in your system.
Prototype Fiddle here
(+Thanks for clarifying the question)