I have a table called apps in MYSQL database.
id
source
1
fb
2
gd
3
tw
4
fb
5
qu
6
fb
I want a single query that will give me total count along with fb count
totalcount
source
6
3
To count for fb in the table try this: (using SUM)
SELECT COUNT(*) totalcount,
SUM(source='fb') source
FROM table1
Another way to do the same: (using COUNT)
SELECT COUNT(*) totalcount,
COUNT(CASE WHEN source = 'fb' THEN 0 END) source
FROM table1;
To count all source in the table try this:
SELECT COUNT(id) totalcount,
COUNT(DISTINCT source) source
FROM table1
See this SQLFiddle
select sum(source='fb') as fb_count,
count(*) as totalcount
from your_table
SQLFiddle
Related
I have create this query:
SELECT table1.id, b.sum
FROM table1
CROSS JOIN (SELECT SUM(id) sum
FROM table1) b
ORDER BY id DESC;
But this produces results like this:
id
sum
3
6
2
6
1
6
Sum value print only one time. Can you help me.
But I want this result :
id
sum
3
6
2
1
This should do it:
select
id,
CASE WHEN id=(max(id) over())
THEN sum(id) over (order by id) END as 'sum'
from cte1
order by id desc;
more info see: Window Function Concepts and Syntax
I am trying to get users from mysql database if they have created a note with weather type = 'cloud','rain','clear' and not those who have missed even one out of these three.
eg.
id user_id weather_type
1 12 cloud
2 12 rain
3 12 clear
4 14 rain
5 15 clear
now here only user 12 have created notes with all three weather_type so only 12 should be fetched not 14 and 15.
Use group by and having:
select user_id
from mytable
where weather_type in ('cloud','rain','clear')
group by user_id
having count(distinct weather_type) = 3
You could use:
SELECT user_id
FROM tab
WHERE weather_type IN ('cloud','rain', 'clear')
GROUP BY user_id
HAVING SUM(weather_type='cloud') > 0
AND SUM(weather_type='rain') > 0
AND SUM(weather_type='clear') > 0;
SqlFiddleDemo
Output:
╔═════════╗
║ user_id ║
╠═════════╣
║ 12 ║
╚═════════╝
If you need all fields you could use:
SELECT *
FROM tab
WHERE user_id IN (prev_query);
Try this
SELECT user_id FROM tablex WHERE weather_type in ('cloud','rain','clear')
GROUP BY user_id
HAVING COUNT(DISTINCT weather_type) = 3
select user_id from table_name where weather_type in ('cloud','rain','clear')
group by user_id
having count(distinct weather_type) = 3
You can use group_concat function as below :
select user_id,weathers from
(select user_id, group_concat(weather_type)
weathers from tbl1 group by user_id
)t1
where find_in_set('cloud',weathers)>0 and
find_in_set('rain',weathers)>0 and
find_in_set('clear',weathers)>0 ;
I tested it and it works correctly.
More info :
A better and general solution for this kind of problem is :
Define another table named e.g. 'must_be' with two columns 'must_id' and 'weather' and fill it by favorite values like below:
must_id weather
1 cloud
2 rain
3 clear
Then use below query to fetch desired rows without hard code any value:
select user_id,count(*) cnt from
(select distinct user_id,weather_type from tbl1 join must_be on(weather_type=weather) ) t1
group by user_id having cnt=(select count(*) from must_be);
In the above query distinct keyword is necessary to get correct result in special situations.
After reading "trincot" response I decided to make my query better:
select user_id from tbl1 join must_be on(weather_type=weather)
group by user_id
having count(distinct weather_type)=(select count(*) from must_be);
I have data in a MySQL table in the following format. I want to retrieve the count in two different conditions as shown in the query below, I want to combine these queries into a single one, by which I mean I would like the first query result in one column and second query result in another column, as so:
Expected output:
count totalcount
--------------------------
3 6
Queries:
select count(*) as count from entries where
date between '2014-08-12' and '2014-08-14';
select count(*) as totalcount from entries ;
Data in mysql table:
id date
------------------------
1 2014-08-14
2 2014-08-13
3 2014-08-12
4 2014-08-11
5 2014-08-10
6 2014-08-09
sql fiddle http://sqlfiddle.com/#!2/faeb26/6
select sum(date between '2014-08-12' and '2014-08-14'), count(*) as totalcount from entries ;
The boolean expression in SUM() equals to true or false, 1 or 0. Therefore just use SUM() instead of COUNT().
Just put the two queries together:
select count(*) as count, b.totalcount from entries,
(select count(*) as totalcount from entries) b
where date between '2014-08-12' and '2014-08-14';
select sum(c) as count, sum(tc) as totalcount
from (select count(*) as c , 0 as tc from entries where date between '2014-08-12' and '2014-08-14'
union all
select 0 as c, count(*) as tc from entries)
simple combine to result in on other select query try this
SELECT (select count(*) as count from entries where
date between '2014-08-12' and '2014-08-14'
) as count, (select count(*) as totalcount from entries) as totalcount;
DEMO LINK
I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;
I'm currently working on an assignment which requires me to find the average on the number of resources for each module. The current table looks like this:
ResourceID ModulID
1 1
2 7
3 2
4 4
5 1
6 1
So basically, I'm trying to figure out how to get the average number of resources. The only
relevant test data here is for module 1, which has 3 different resources connected to it. But I need to display all of the results.
This is my code:
select avg(a.ress) GjSnitt, modulID
from
(select count(ressursID) as ress
from ressursertiloppgave
group by modulID) as a, ressursertiloppgave r
group by modulID;
Obviously it isn't working, but I'm currently at loss on what to change at this point. I would really appreciate any input you guys have.
This is the query you are executing, written in a slightly less obtuse syntax.
SELECT
avg(a.ress) as GjSnitt
, modulID
FROM
(SELECT COUNT(ressursID) as ress
FROM ressursertiloppgave
GROUP BY modulID) as a
CROSS JOIN ressursertiloppgave r <--- Cross join are very very rare!
GROUP BY modulID;
You are cross joining the table, making (6x6=) 36 rows in total and condensing this down to 4, but because the total count is 36, the outcome is wrong.
This is why you should never use implicit joins.
Rewrite the query to:
SELECT AVG(a.rcount) FROM
(select count(*) as rcount
FROM ressursertiloppgave r
GROUP BY r.ModulID) a
If you want the individual rowcount and the average at the bottom do:
SELECT r1.ModulID, count(*) as rcount
FROM ressursertiloppgave r1
GROUP BY r1.ModulID
UNION ALL
SELECT 'avg = ', AVG(a.rcount) FROM
(select count(*) as rcount
FROM ressursertiloppgave r2
GROUP BY r2.ModulID) a
I got the solution
SELECT AVG(counter)
FROM
(
SELECT COUNT(column to count) AS counter FROM table
) AS counter
Note that the nickname {counter} was added in SELECT COUNT and at the end of the inner SELECT