Semi-newbyism ahead: I need to do two selects and count the number of items in both of them. Here's a bad example of what I thought would work --
sum(
select count(*) as count1 from users where name = 'John'
union
select count(*) as count1 from users where name = 'Mary'
) as theCount
(This is, as I said, a BAD example, since I could obviously write this as a single select with an appropriate WHERE clause. In what I really have to do, the two things I have to do are such that I can't do them as a single select (or, at least, I haven't yet found a way to do them as a single select).
Anyway, I think what I'm trying to do is clear: the select-union-select bit returns a column containing the counts of the two selects; that part works fine. I thought that wrapping them in a SUM() would get me what I wanted, but it's throwing a syntax error. The right thing is probably trivial, but I just don't see it. Any thoughts out there? Thanks!
For generic selects that you can't necessarily write with one where:
SELECT sum(count1) as totalcount FROM (
select count(*) as count1 from users where name = 'John'
union all
select count(*) as count1 from users where name = 'Mary'
) as theCount
select count(*) as count1 from users where name in ('John','Mary')
This is another alternative
select ( select count(*) as count1 from users where name = 'John')
+
( select count(*) as count1 from users where name = 'Mary') as total
Another possible solution:
select
sum(if(name='John',1,0)) as tot_John,
sum(if(name='Mary',1,0)) as tot_Mary,
sum(if(name in ('John','Mary'),1,0)) as total
from users
Related
Objective:
I wanted to show the number of distinct IDs for any combination selected.
In the below example, I have data at a granular level: ID level data.
I wanted to show the number of distinct IDs for each combination.
For this, I use count distinct which will give me '1' for the below combinations.
But let's say if I wanted to find the number of IDs who made both E-commerce and Face to face transactions, in that case, if I just use this data, I would be showing the sum of E-comm and Face to face and the result would be '2' instead of '1'.
And this is not limited to Ecom/Face to face. I wanted to apply the same logic for all columns.
Please let me know if you have any other alternative approach to address this issue.
First aggregate in your table to get the distinct ids for each TranType:
SELECT TranType, COUNT(DISTINCT id) counter_distinct
FROM tablename
GROUP BY TranType
and then join to the table:
SELECT t.*, g.counter_distinct
FROM tablename t
INNER JOIN (
SELECT TranType, COUNT(DISTINCT id) counter_distinct
FROM tablename
GROUP BY TranType
) g ON g.TranType = t.TranType
Or use a correlated subquery:
SELECT t1.*,
(SELECT COUNT(DISTINCT t2.id) FROM tablename t2 WHERE t2.TranType = t1.TranType) counter_distinct
FROM tablename t1
But let's say if I wanted to find the number of IDs who made both E-commerce and Face to face transactions, in
You can get the list of ids using:
select id
from t
where tran_type in ('Ecomm', 'Face to face')
group by id
having count(distinct tran_type) = 2;
You can get the count using a subquery:
select count(*)
from (select id
from t
where tran_type in ('Ecomm', 'Face to face')
group by id
having count(distinct tran_type) = 2
) i;
What I'm trying to do is returning both my queried (and limited) results as well as the total number of items in the table. Here's what I have so far.
SELECT id, name
FROM ??
WHERE
user_id = ?
LIMIT ? , ?;
SELECT COUNT(*) as total
FROM ??
WHERE
user_id = ?
;
This is two SQL queries and I was wondering how or if it's possible to combine it into one. I'd like the total to be by itself (if possible), otherwise as an attached column to each row. Or maybe what I have now is the most optimal solution.
Well, you could include the count in each row using a subquery:
SELECT
id,
name,
(SELECT COUNT(*) FROM yourTable WHERE user_id = ?) total
FROM yourTable
WHERE
user_id = ?;
But, from a separations of concerns point of view, this is probably not best practice. Instead, more typically I would expect to see two separate queries here.
SELECT SQL_CALC_FOUND_ROWS
...
LIMIT ...
SELECT FOUND_ROWS();
Reference.
Or, I would vote for the CROSS JOIN, not the subquery.
Another way to do the same using CROSS JOIN
SELECT
t1.id,
t1.[name],
t2.cnt
FROM
yourtable t1
CROSS JOIN
(
SELECT
COUNT(1) AS cnt
FROM
yourtable
WHERE
[user_id] = ?
) t2
WHERE
t1.[user_id] = ?
LIMIT
?, ?
i am using MySql workbench 5.7 to run this.
i am trying to get the result of this query:
SELECT COUNT(Users) FROM UserList.custumers;
and this query:
SELECT Users FROM UserList.custumers;
at the same table, meaning i want a list of users in one column and the amount of total users in the other column.
when i tries this:
SELECT Users , COUNT(Users) FROM UserList.custumers;
i get a single row with the right count but only the first user in my list....
You can either use a cross join since you know the count query will result in one row... whose value you want repeated on every row.
SELECt users, userCount
FROM userlist.custumers
CROSS JOIN (Select count(*) UserCount from userlist.custumers)
Or you can run a count in the select.... I prefer the first as the count only has to be done once.
SELECT users, (SELECT count(*) cnt FROM userlist.custumers) as userCount
FROM userlist.custumers
Or in a environment supporting window functions (not mySQL) you could count(*) over (partition by 1) as userCount
The reason you're getting one row is due to mySQL's extension of the GROUP BY which will pick a single value from non-aggregated columns to display when you use aggregation without a group by clause. If you add a group by to your select, you will not get the count of all users. Thus the need for the inline select or the cross join.
Consider: -- 1 record not all users
SELECT Users , COUNT(Users) FROM UserList.custumers;
vs --all users wrong count
SELECT Users , COUNT(Users) FROM UserList.custumers group by users;
vs -- what I believe you're after
SELECT Users, x.usercount FROM UserList.custumers
CROSS JOIN (Select count(*) UserCount from userlist.custumers) x
Use a subquery in SELECT.
Select Users,
(SELECT COUNT(Users) FROM UserList.custumers) as total
FROM UserList.custumers;
I have the following table:
CREATE TABLE sometable (my_id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING, number STRING);
Running this query:
SELECT * FROM sometable;
Produces the following output:
1|someone|111
2|someone|222
3|monster|333
Along with these three fields I would also like to include a count representing the amount of times the same name exists in the table.
I've obviously tried:
SELECT my_id, name, count(name) FROM sometable GROUP BY name;
though that will not give me an individual result row for every record.
Ideally I would have the following output:
1|someone|111|2
2|someone|222|2
3|monster|333|1
Where the 4th column represents the amount of time this number exists.
Thanks for any help.
You can do this with a correlated subquery in the select clause:
Select st.*,
(SELECT count(*) from sometable st2 where st.name = st2.name) as NameCount
from sometable st;
You can also write this as a join to an aggregated subquery:
select st.*, stn.NameCount
from sometable st join
(select name, count(*) as NameCount
from sometable
group by name
) stn
on st.name = stn.name;
EDIT:
As for performance, the best way to find out is to try both and time them. The correlated subquery will work best when there is an index on sometable(name). Although aggregation is reputed to be slow in MySQL, sometimes this type of query gets surprisingly good results. The best answer is to test.
Select *, (SELECT count(my_id) from sometable) as total from sometable
I am trying to UNION two columns in a SELECT, and alias to a third.
I also need to retrieve the data matching the WHERE clause, then sort by the aliased column (MLS_SORT).
This, and variations of it that I have tried, don't work.
SELECT *
FROM
(SELECT MLS_AGENT_ID AS MLS_SORT FROM mlsdata)
UNION
(SELECT MLS_OFFICE_ID AS MLS_SORT FROM mlsdata)
WHERE (MLS_AGENT_ID = $agent_narid) OR (MLS_OFFICE_ID = $office_narid)
ORDER BY MLS_SORT
This part does work and creates the MLS_SORT alias with the correct values, but I can't figure out how to limit the results to the WHERE clause above:
(SELECT MLS_AGENT_ID AS MLS_SORT FROM mlsdata)
UNION
(SELECT MLS_OFFICE_ID AS MLS_SORT FROM mlsdata)
Am I at least going down the correct path or is this not the proper way to proceed?
Thanks for any assistance.
The trick is to understand the syntax of UNION: query UNION query
I think you want:
SELECT MLS_SORT
FROM
(
SELECT MLS_AGENT_ID AS MLS_SORT
FROM mlsdata
WHERE MLS_AGENT_ID = $agent_narid
UNION
SELECT MLS_OFFICE_ID AS MLS_SORT
FROM mlsdata
WHERE MLS_OFFICE_ID = $office_narid
)
ORDER BY MLS_SORT
To get the two ID subsets into a single result set then sort them.
But, this whole query looks like it's going to give a two-row result set -- one row for an agent and another for an office. Is that what you want?
Your logic effectively typecasts agent id and office id numbers into a single result set. Does that make sense in your application?
try something like this:
select * from
(
(SELECT MLS_AGENT_ID AS MLS_SORT
FROM mlsdata
WHERE (MLS_AGENT_ID = $agent_narid)
)
UNION
(SELECT MLS_OFFICE_ID AS MLS_SORT
FROM mlsdata
WHERE (MLS_OFFICE_ID = $office_narid)
)) a
ORDER BY MLS_SORT
edit:
alternate order by
ORDER BY 1