how to write query which represents group names also - mysql

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;

Related

SQL COUNT if a specific value is found and output in the same line

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;

MySQL: Group By multiple columns not giving exact results

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

Count number of records with duplicates

My subreport goes through a list of ID's. Each ID has a location assigned to it. The report is grouped by the location (Group #1), then by ID (Group #2).
In the table, the ID's should look like:
14600
14602
14602
14602
14700
14703
14704
14704
My desired output would be 2 because there are 2 ID's with more than one entry. How can I easily calculate this?
You can try this:
DECLARE #temp table(num integer);
INSERT INTO #temp(num) VALUES (14600), (14602), (14602), (14602), (14700), (14703), (14704), (14704);
SELECT COUNT(distinct num) repeats
FROM
(SELECT
num, count(num) as counts
FROM #temp
GROUP BY num
HAVING COUNT(num) > 1) a;
Sounds like you're looking for a combination of COUNT and DISTINCT, in conjunction with ensuring that there is more than one occurrence. You can accomplish this with something like:
SELECT COUNT(DISTINCT id) FROM table_name HAVING COUNT(*) > 1
This returns the number of distinct IDs in the table. In your case, this is 2.

Count the number of occurrences of each article number in MYSQL

I want to count the number of occurrences of each article number in my table. My table has the following structure:
|customerNumber|OrderNumber|ArticleNumber|
|1|1|1|
|2|2|2|
|3|3|4|
|1|4|3|
|3|3|2|
|4|5|2|
|5|6|4|
Expected Outcome:
|ArticleNumber|NumberOfOrders|
|1|1|
|2|3|
|3|1|
|4|2|
How can I do it? ( I got no idea atm)
You can use the count(fieldName) from tablename syntax to achieve your requirement.
So now, the code can be,
select ArticleNumber,count(NumberOfOrders) as NumberOfOrders from
tableName group by ArticleNumber
you need to count ArticleNumber then use below query
SELECT ArticleNumber,COUNT(*) AS NumberOfOrders FROM your_table group by ArticleNumber

Why does query group names into single row

I'm running this query where I want to check the names of items that the seller is selling and also to count them.
SELECT name, COUNT(name) AS how_many_items FROM items WHERE seller_id=6
Then when I try to achieve this result only with one command works.
SELECT name FROM items WHERE seller_id=6
Try this:
SELECT `name`, COUNT(`name`) AS `how_many_items `
FROM items WHERE seller_id=6
GROUP BY `name`
COUNTis causing your results to be grouped into a single line, use GROUP BY to distinguish the results by the name field
See more examples here: http://dev.mysql.com/doc/refman/5.7/en/counting-rows.html
you need a GROUP BY clause
SELECT name, COUNT(name) AS how_many_items
FROM items
WHERE seller_id=6
GROUP BY name