I need some help with the query below.
I have for example the following data set:
and I need to get the following output:
I tried with a query similar to this one:
SELECT
id, ValA, count(1)
FROM dual
GROUP BY id, ValA;
but it is not working as expected. It's basically duplicating the values in the output:
Would you be able to help me?
count(*) counts all rows. count(ValA) counts non-null values. That means count(*) - count(ValA) counts null's.
SELECT
id, count(*) - count(ValA), count(ValA)
FROM dual
GROUP BY id;
Select count(*) from usa.adult group by `income`;
income col has mainly two values i.e >50k and <50k
which is found in the result as
how can i find which value represent >50k and <50k.
desired output:
2: https://i.stack.imgur.com/VKTKE.png
as said from #JNevill extract columns directly
Select income, count(*) from usa.adult group by income;
I have a table that contains 5 columns namely:
before_1, before_2, before_3, rule, name
where before_1,before_2, and before_3 are the three words before the name/word in a document.
What I wanted to find was:
Which are the two words that occur together before a name. I don't want just the top words, but all the words sorted by the number of occurrences.
I tried the following few queries but that didn't work for me.
select count(before_2),count(before_3),name from data_with_before_words group by name;
I got the same count for both columns, which is not what I was expecting.
Example data:
First 5 rows:
before_1,before_2,before_2,rule,name
a,league,of,Persona,Amell
the,assasin,of,Persona,Amell
the,league,of,Persona,Amell
a,assasin,of,Persona,Amell
a,league,of,Persona,Amell
Expected Output:
league,of,3,Amell
assasin,of,2,Amell
Any help would be appreciated.
To get the expected output you can use following query
select before_2,before_3,name,count(*)
from data_with_before_words
group by before_2,before_3,name
order by count(*) desc
Demo
Try this Query:
select count(res1.comWords) as occurrences, res1.name from (select concat(before_1,"-", before_2) as comWords, name from data_with_before_words) res1 group by res1.name order by occurrences desc;
Try this out
SELECT before_2,before_3,name FROM data_with_before_words GROUP BY before_2,before_3,name Having count(*)>=1
I have this kind of table
ID-----CategoryID-----Price
1---------1-----------200
2---------1-----------300
3---------2-----------150
4---------2-----------100
I need a SQL query where I can get average of elements by Category ID. The results should be like:
CategoryID----------AVG_Price
1-------------------250
2-------------------175
I have not been able to think it through..
Select categoryid, avg(price)
from table
group by categoryid
In MySQL backticks aren't required, but are used if your field names contain spaces or reserved words:
SELECT `CategoryID`, avg(`Price`) as `AVG_Price`
FROM `tablename`
GROUP BY `CategoryID`;
SQLFiddle
Note: Category 2 averages to 125 not 175 (150+100=250; 250/2=125)
I am trying to count line that has columns which been repeated (their value) more than x times in mysql, every time I try to execute this query I get an error :
SELECT DISTINCT
idLogin, count(idLogin ) AS "cou"
FROM
`products` AS t
GROUP BY
idLogin
HAVING
t.cou > 2
why this error is happening
Use this one
SELECT DISTINCT idLogin, count(idLogin ) AS cou FROM `products`
GROUP BY idLogin HAVING cou > 2
you can put that expression count(idLogin ) directly in having clouse....
it will not give any error.. and will be more understable as well....
or else you can do one thing -
select idLogin,cou from (
SELECT DISTINCT idLogin, count(idLogin ) AS cou
FROM products
GROUP BY idLogin ) t
where t.cou >2
Remove the double quotes from the aliased column name.
MySQL uses single back-quotes for escaping table and column names, not double quotes.
The correlation name t does not have a column cou. Just use this:
HAVING cou > 2
PS: DISTINCT is redundant in your query. The GROUP BY ensures there will only be one row per distinct value of idLogin.