I am new to mysql.
I have a query to get count from different tables and it works okay. But, now I want to club all 3 query COUNT into 1 like SUM (COUNT1, COUNT2, COUNT3)
select COUNT(*) as count1, 0 as count2, 0 as count3 from table1
UNION ALL
select 0 as count1, COUNT(*) as count2, 0 as count3 from table2
UNION ALL
select 0 as count1, 0 as count2, COUNT(*) as count3 from table3
E.g. count1 = 10, count2 = 20 and count3 = 15
Then, I want to get sum = 45
You can put the UNION in a subquery and then SUM() the counts. But there's no need to put them in separate columns in each unioned query.
SELECT SUM(count) AS total
FROM (
SELECT COUNT(*) AS count FROM table1
UNION ALL
SELECT COUNT(*) AS count FROM table2
UNION ALL
SELECT COUNT(*) AS count FROM table3
) AS subquery
to get sum of all counts from you tables you can write a query as
select
(select COUNT(*) from table1) +
(select COUNT(*) from table2) +
(select COUNT(*) from table3) as total
Following your approach, you can wrap unioined query in as sub select and then apply sum on the results
SELECT SUM(count1) + SUM(count2) + SUM(count3) total
FROM (
SELECT COUNT(*) AS count1, 0 AS count2, 0 AS count3 FROM table1
UNION ALL
SELECT 0 AS count1, COUNT(*) AS count2, 0 AS count3 FROM table2
UNION ALL
SELECT 0 AS count1, 0 AS count2, COUNT(*) AS count3 FROM table3
) t
Related
I have a query I made:
SELECT DISTINCT player_1 AS player,
(SELECT COUNT(*) FROM results WHERE player_1=player OR player_2=player) AS since_start_matches,
(SELECT COUNT(*) FROM results WHERE (player_1=player OR player_2=player) AND ht_total_goals=0) AS since_start_ht_0,
(SELECT COUNT(*) FROM results WHERE (player_1=player OR player_2=player) AND ht_total_goals=1) AS since_start_ht_1,
(SELECT COUNT(*) FROM results WHERE (player_1=player OR player_2=player) AND ht_total_goals=2) AS since_start_ht_2,
(SELECT COUNT(*) FROM results WHERE (player_1=player OR player_2=player) AND ht_total_goals=3) AS since_start_ht_3,
(SELECT COUNT(*) FROM results WHERE (player_1=player OR player_2=player) AND ht_total_goals=4) AS since_start_ht_4,
(SELECT COUNT(*) FROM results WHERE (player_1=player OR player_2=player) AND ht_total_goals>=5) AS since_start_ht_5_plus
FROM results ORDER BY player
The results table has 25000 entries and it takes around 7 seconds to do this query, which is far too long. The query is incredibly inefficient as each column I'm creating is searching again on the same table but with different conditions.
I tried indexing the columns of interest in my where clause. This knocks off a couple of seconds. But it's still too slow.
What is the best approach to handle this kind of query?
I'm using MariaDB 10.2
Unpivot the data then aggregation:
SELECT player,
COUNT(*) AS since_start_matches,
SUM(ht_total_goals=0) AS since_start_ht_0,
SUM(ht_total_goals=1) AS since_start_ht_1,
SUM(ht_total_goals=2) AS since_start_ht_2,
SUM(ht_total_goals=3) AS since_start_ht_3,
SUM(ht_total_goals=4) AS since_start_ht_4,
SUM( ht_total_goals>=5) AS since_start_ht_5_plus
FROM ((SELECT player_1 as player, ht_total_goals
FROM results
) UNION ALL
(SELECT player_2 as player, ht_total_goals
FROM results
)
) p
GROUP BY player
You can use the LEFT JOIN and conditional aggregation as follows:
SELECT player_1 AS player,
COUNT(T2.player_1) AS since_start_matches,
SUM(CASE WHEN T2.ht_total_goals=0 THEN 1 END) AS since_start_ht_0,
SUM(CASE WHEN T2.ht_total_goals=1 THEN 1 END) AS since_start_ht_1,
SUM(CASE WHEN T2.ht_total_goals=2 THEN 1 END) AS since_start_ht_2,
SUM(CASE WHEN T2.ht_total_goals=3 THEN 1 END) AS since_start_ht_3,
SUM(CASE WHEN T2.ht_total_goals=4 THEN 1 END) AS since_start_ht_4,
SUM(CASE WHEN T2.ht_total_goals>=5 THEN 1 END) AS since_start_ht_5_PLUS
FROM results T1 LEFT JOIN results T2
ON (T2.player_1=T.player OR T2.player_2=T.player)
GROUP BY T1.PLA
YER_1
ORDER BY player
I'm getting the error "Table 'metrics.t1' doesn't exist" even though t1 is defined. I've read several of the articles on here about doesn't exist errors and can't find a solution.
I can make it work if I replace "FROM t1 as t2" by inserting the entire t1 query into the from statement. However that means the huge t1 query runs twice, which takes about 4 minutes.
SELECT
date_format(t1.date, '%Y') as year, date_format(t1.date, '%m') as month, date_format(t1.date, '%d') as day, t1.epc, t1.scrap, t1.freight, t1.smo, t1.extsort, t1.total,
( SELECT SUM(t2.total) / COUNT(t2.total)
FROM
t1 as t2
WHERE DATEDIFF(t1.date, t2.date) BETWEEN 0 AND 92
) AS movavg
FROM (SELECT date, sum(epc_labor_cost) as epc, sum(scrap_value) as scrap, sum(prem_freight_cost) as freight, sum(smo_sort_hours) as smo, sum(extsort) as extsort, sum(epc_labor_cost+scrap_value+prem_freight_cost+smo_sort_hours+extsort) as total FROM (
select pn, date, labor_cost as epc_labor_cost, 0 as scrap_value, 0 as prem_freight_cost, 0 as smo_sort_hours, 0 as extsort, 0 as total from epc_data
union all select pn, date, 0 as epc_labor_cost, abs(value) as scrap_value, 0 as prem_freight_cost, 0 as smo_sort_hours, 0 as extsort, 0 as total from mke_scrap
union all select pn, date, 0 as epc_labor_cost, 0 as scrap_value, cost as prem_freight_cost, 0 as smo_sort_hours, 0 as extsort, 0 as total from prem_freight
union all select pn, date, 0 as epc_labor_cost, 0 as scrap_value, 0 as prem_freight_cost, hours*4.02 as smo_sort_hours, 0 as extsort, 0 as total from smo_sort
union all select STRIP_NON_DIGIT(extsort.pn) as pn, date, 0 as epc_labor_cost, 0 as scrap_value, 0 as prem_freight_cost, 0 as smo_sort_hours, (extsort.sorted*pn_data.extsort_cost) as extsort, 0 as total from extsort inner join pn_data on STRIP_NON_DIGIT(extsort.pn)=STRIP_NON_DIGIT(pn_data.pn) ) as test group by year(date), week(date,3) ORDER BY date desc) AS t1
ORDER BY t1.date desc limit 26
I'd like to be able to have it run the t1 query a single time for use in both parts of the full statement.
You can probably create a temp table first and then use the temp table for the querying. That way you are not query the same multiple times.
If you're trying to find a way to join a subquery to itself, you may want to look into CTEs (Common Table Expressions).
You cannot select from a subquery in the outer query's select; you can select fields from the subquery.
OK
SELECT t.something AS aSomething
FROM (SELECT * FROM x) AS t
;
NOT OK
SELECT (SELECT something FROM t LIMIT 1) AS aSomething
FROM (SELECT * FROM x) AS t
;
However, with CTEs you can do things like:
WITH t AS (SELECT * FROM x)
SELECT t1.*, SUM(t2.something)
FROM t AS t1
INNER JOIN t AS t2 ON t1.somevalue > t2.somevalue
I've mainly only used them in MSSQL, so my syntax for MySQL may be a little off.
Below is my query to get some data for dashboard screen.
SELECT COUNT(*) as occupied_rooms FROM rooms where available='N' ;
SELECT COUNT(*) as checkedIn_guests FROM booking where checkout_time='' ;
SELECT COUNT(*) as available_rooms FROM rooms where available='Y' ;
SELECT COUNT(*) as total_guest FROM booking;
SELECT COUNT(*) as total_booking FROM booking;
SELECT COUNT(*) as total_staff FROM employee;
It produce output as show in above
How ever I want the output like given above image.
Use union all
SELECT 'occupied_rooms' as which, COUNT(*) as cnt FROM rooms where available = 'N'
UNION ALL
SELECT 'checkedIn_guests', COUNT(*) FROM booking where checkout_time = ''
UNION ALL
SELECT 'available_rooms', COUNT(*) FROM rooms where available = 'Y' ;
UNION ALL
SELECT 'total_guest', COUNT(*) FROM booking;
UNION ALL
SELECT 'total_booking', COUNT(*) FROM booking;
UNION ALL
SELECT 'total_staff', COUNT(*) FROM employee;
You could make the query a bit more efficient by combining the queries that reference the same table. But the overall structure would be essentially the same.
You could use a UNION for build single table result with each result in a row
SELECT 'occupied_rooms', COUNT(*) count
FROM rooms
where available='N'
UNION
SELECT 'checkedIn_guests', COUNT(*)
FROM booking
where checkout_time=''
UNION
SELECT 'available_rooms', COUNT(*)
FROM rooms
where available='Y'
UNION
SELECT 'total_guest', COUNT(*)
FROM booking
UNION
SELECT 'total_booking', COUNT(*)
FROM booking
UNION
SELECT 'total_staff', COUNT(*)
FROM employee;
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
How can I sum the columns of 3 tables?
I have table1, table2, and table3 with the column 'revenue'.
I can do SELECT SUM(REVENUE) FROM TABLE1 but what do i do for all of them?
I tried: SELECT SUM(
table1.Revenue+
table2.Revenue +
table3.Revenue
)
FROM
table1,
table2,
table3' but it doesn't work...
Any ideas? Thanks!
select sum(rev) as trev
from
(
SELECT SUM( Revenue) as rev FROM table1
union all
SELECT SUM( Revenue) as rev FROM table2
union all
SELECT SUM( Revenue) as rev FROM table3
) as tmp
You need to add all of the individual sums together:
SELECT (SUM(table1.Revenue) + SUM(table2.Revenue) + SUM(table3.Revenue))
AS total_rev FROM table1, table2, table3
select sum( revenue )
from (
select revenue from table1
union
select revenue from table2
union
select revenue from table3
)
SELECT SUM(REVENUE) FROM (
SELECT REVENUE FROM TABLE1
UNION ALL
SELECT REVENUE FROM TABLE2
UNION ALL
SELECT REVENUE FROM TABLE3
) revenues