How to do a count on a union query - mysql

I have the following query:
select distinct profile_id from userprofile_...
union
select distinct profile_id from productions_...
How would I get the count of the total number of results?

If you want a total count for all records, then you would do this:
SELECT COUNT(*)
FROM
(
select distinct profile_id
from userprofile_...
union all
select distinct profile_id
from productions_...
) x

you should use Union All if there are equals rows in both tables, because Union makes a distinct
select count(*) from
(select distinct profile_id from userprofile_...
union ALL
select distinct profile_id from productions_...) x
In this case, if you got a same Profile_Id in both tables (id is probably a number, so it's possible), then if you use Union, if you got Id = 1 in both tables, you will lose one row (it will appear one time instead of two)

This will perform pretty well:
select count(*) from (
select profile_id
from userprofile_...
union
select profile_id
from productions_...
) x
The use of union guarantees distinct values - union removes duplicates, union all preserves them. This means you don't need the distinct keyword (the other answers don't exploit this fact and end up doing more work).
Edited:
If you want to total number of different profile_id in each, where given values that appear in both table are considered different values, use this:
select sum(count) from (
select count(distinct profile_id) as count
from userprofile_...
union all
select count(distinct profile_id)
from productions_...
) x
This query will out-perform all other answers, because the database can efficiently count distinct values within a table much faster than from the unioned list. The sum() simply adds the two counts together.

These will not work if in one of the COUNT(*) the result is equals to 0.
This will be better:
SELECT SUM(total)
FROM
(
select COUNT(distinct profile_id) AS total
from userprofile_...
union all
select COUNT(distinct profile_id) AS total
from productions_...
) x

As omg ponies has already pointed out that there is no use of using distinct with UNION, you can use UNION ALL in your case.....
SELECT COUNT(*)
FROM
(
select distinct profile_id from userprofile_...
union all
select distinct profile_id from productions_...
) AS t1

Best solution is to add count of two query results. It will not be a problem if the table contains large number of records. And you don't need to use union query.
Ex:
SELECT (select COUNT(distinct profile_id) from userprofile_...) +
(select COUNT(distinct profile_id) from productions_...) AS total

Related

Is it possible in mysql to display the sum of two count() results obtained from two different "group by" criteria on the same table?

I have the data table as follows
GameTable
And this is the result i need
ResultTable
I tried this but iit wouldn't work
(
(select count() from game group by team_1)
+
(select count() from game group by team_2)
)
Use union all:
select Team_id, sum(Game_count) as 'Game_count'
from (
select Team_1 as 'Team_id', count(*) as 'Game_count'
from games
group by Team_1
union all
select Team_2, count(*)
from games
group by Team_2
) as q
group by Team_id

Select distincts of same column in mutiple tables sql

SQL novice here.
I have this schema:
What I need in plain English is:
"Out of the 2 columns, make one above the other in one column, and then count how many distincts values there is"
I've tried
SELECT COUNT (DISTINCT uid ) from nodes UNION SELECT COUNT (DISTINCT uid) from ways ;
SELECT distinct nodes.uid from nodes JOIN ways on nodes.uid = ways.uid ;
sqlite> SELECT COUNT (DISTINCT uid ) from nodes UNION SELECT COUNT (DISTINCT uid) from ways ;
1195
2182
sqlite> SELECT uid from nodes FULL OUTER JOIN ways on nodes.uid = ways.iud ;
Error: RIGHT and FULL OUTER JOINs are not currently supported
SELECT COUNT (DISTINCT iud) FROM (SELECT DISTINCT uid from nodes as uid UNION SELECT DISTINCT uid from ways as uid as subq);
SELECT count (distinct nodes.uid) from nodes JOIN ways on nodes.uid = ways.uid ;
takes ages and i'm not sure nodes.uid = ways.uid is the correct way to go
Any idea ?
I think i got it
SELECT COUNT (DISTINCT uid) from (SELECT DISTINCT uid from nodes UNION SELECT DISTINCT uid from ways) as subq ;
Since UNION returns a distinct set of the joining tables you dont need to use the DISTINCT keyword
SELECT COUNT(uid) Cnt
FROM (
SELECT uid
FROM nodes
UNION
SELECT uid
FROM ways
) t

How to get AS two separate selectors with UNION?

As sadly SQL is my weakest skill.
I'm trying to use UNION in a VIEW, where I can get statistics from two different tables with one query.
SELECT COUNT(*) AS `customer_count` FROM `Customers`
UNION
SELECT COUNT(*) AS `supplier_count` FROM `Suppliers`;
[Demo table]
However, it only returns customer_count, with two rows. Is there anyway, to make this work, so it returns customer_count and supplier_count separately?
You would need a cross join to see the results adjacent to each other in one row. So you would select from both the tables without a join condition.
select * from
(select count(*) as customer_count from Customers) x,
(select count(*) as supplier_count from Suppliers) y
select
(SELECT COUNT(*) FROM Customers) as customer_count,
(SELECT COUNT(*) FROM Suppliers) AS supplier_count
Using your Table Demo.
The key is use alias so the field names match on each union select.
In this case TableSource and Total
SELECT 'Customer' as TableSource, Count(City) as Total FROM Customers
UNION
SELECT 'Suppliers' as TableSource, Count(City) as Total FROM Suppliers;
CREATE VIEW `vw_count` AS
select (select count(0) from `tbl`) AS `customer_count`,
(select count(0) from `tbl2`) AS `supplier_count`;

How to get row count of 2 different tables (and databases) in one query?

I got a database named accounts and account table inside of this database, Also I got the database named players and player table inside of this database.
How can I get a rows count of this two tables in one query?
I've tried this:
SELECT
SUM(`account`.`account`.`id`) AS 'accounts',
SUM(`player`.`player`) AS 'players';
But it doesn't work.
If you need exactly rows count (not sum), than do something like this:
select
(select count(*) from accounts.account) as count1,
(select count(*) from players.player) as count2
or
select count(*) as `count`,"account" as `table` from accounts.account
union all
select count(*) as `count`,"player" as `table` from players.player
A simple UNION operation on two select statements will do:
SELECT COUNT(*), 'Accounts' FROM Accounts.Account
UNION
SELECT COUNT(*), 'Players' FROM Players.Player
And you have to qualify each table with the database name since they're in separate databases.
Try:
SELECT
COUNT(`account`.`id`) AS 'accounts',
COUNT(`player`.`player`) AS 'players'
FROM
`account`,
`player`
SELECT COUNT(*)
FROM (
SELECT Id
FROM accounts.account
UNION ALL
SELECT player
FROM players.player ) AS BothTables
with Value (nbr, name ) as
(
select count(*) amount, 'AccountsCount' as ab from accounts..account
union all
select count(*) amount, 'PlayersCount' as ab from players..player
)
select *
from value as s
PIVOT(sum(nbr) for name in (AccountsCount, PlayersCount) ) as pvt

Display two different columns from two different tables with ORDER BY

I want to display the values of two different tables, and two different columns from that table, sorted in ascending order.
How can I do that?
I use this to display values of two tables, but stuck in sorting by ascending order:
select
distinct varWinePrice
from
tbl_wines
union
select
distinct varPrice
from
tbl_price
If column names are different then sorting should be done using the column of the first subselect:
select distinct varWinePrice
from `tbl_wines`
union
select distinct varPrice
from tbl_price
order by varWinePrice
select distinct varWinePrice as x from tbl_wines union
select distinct varPrice as x from tbl_price
order by x asc;
you may nest your UNION select into outer select
select
pr.price
from (
select
distinct varWinePrice
from
tbl_wines
union
select
distinct varPrice
from
tbl_price
) pr
order by
pr.price
You can use temporary table to use order by syntax:
1) Create temporary table
2) Fill the temporary table with records
3) get the rows from temporary table by applying order by clause
SELECT Price
FROM ( SELECT DISTINCT varWinePrice AS Price FROM tbl_wines
UNION
SELECT distinct varPrice AS Price FROM tbl_price
)
ORDER BY Price