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
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;
Sorry about the title, I don't know what's the proper term to use.
I have this data:
I want to produce a table something like this:
I want to get the total of each 'group by' and I'm doing this code but I'm getting wrong total.
SELECT code, (a * b)
AS total_each_code
FROM table1
GROUP BY code
UPDATE: updated photos of sample data, sorry for typos.
you can use sub query for this
select code, sum(a.total_each_code)
from (
SELECT code, (a * b)
AS total_each_code
FROM table1 order by code
)a
group by a.total_each_code
or just simple as
select code,sum(a*b) as total from table1 group by code.
Use sum() aggregation
SELECT code, sum(a * b)
AS total_each_code
FROM table1
GROUP BY code
table :
profile_data : id(int),age(int),gender(varchar),goals(longtext)
I want to write a query which will give the average number of goals set by each of the ids.
How do I count on it when its in a textual format?
I tried :
select id,avg(count(goals)) from profile_data;
Its showing query is incorrect.
Do you perhaps require something like this?
SELECT avg(cnt)
FROM (
SELECT id
, count(goals) as cnt
FROM profile_data
GROUP BY id
) gr;
You can use GROUP BY, go through aggregate functions of your DBMS
SELECT id
,avg(goals)
FROM profile_data
GROUP BY id;
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 am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC