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
Related
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 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
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
I have the following query:
SELECT owner,
typeOwner,
type,
count(*) AS count
FROM myTable
GROUP BY typeOwner
ORDER BY count DESC
LIMIT 0, 30
Now , the typeOwner values = '<test>a</test>' but , some times the typeOwner field will have some string else like '<test>b</test>, how can i let this query count the <test>b</test> as a group of '<test>a</test>.
I want to make an exception for this, I mean typeOwner <test>a</test> AND typeOwner <test>b</test> should be counted as one row that have two count.
here's a fiddle : http://sqlfiddle.com/#!2/70053/1 , take look
actually these are should be grouped by <type></type><aId></aId>
"actually these are should be grouped by "
You can GROUP BY something relatively ugly like:
GROUP BY LEFT(typeOwner, position('<xType>' in typeOwner) -1)
But you are probably better off preprocessing the data in some fashion. I'm not sure how MySQL handles xml, but in SQL server I might extract the XML values into first class relation fields if I needed to do this sort of processing with any frequency.
sqlfiddle.com
Based on your sqlfiddle, this works
SELECT
left(typeOwner, instr(typeOwner, '<xType>')-1) as typeOwner,
owner,type, count(*) as count
FROM `test`
GROUP BY left(typeOwner, instr(typeOwner, '<xType>')-1)
ORDER BY count DESC
LIMIT 0 , 30