SQL idea for a query - mysql

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)

Related

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

distinct multiple columns in SQL

I want to select distinct combination of user age (a column name) and user name (another column name) , but when I write distinct (user_age, user_name), there is syntax error. If anyone have ideas how to write distinct with multiple columns it will be great.
BTW, using MySQL Workbench/MySQL
thanks in advance,
Lin
You have to leave the parenthesis. Just write
SELECT distinct user_age, user_name FROM foo where bar;
;-)
There are 2 ways to achieve this
SELECT DISTINCT user_age, user_name FROM table WHERE some_condition;
OR
SELECT user_age, user_name FROM table WHERE some_condition GROUP BY user_age, user_name;
Hope this helps

what does it mean when we use " group by 1" in an SQL Query

I have come across a query where it is specified by
select concat(21*floor(diff/21), '-', 21*floor(diff/21) + 20) as `range`, count(*) as
`number of users` from new_table group by 1 order by diff;
here what exactly does group by 1 mean?
Assuming you have a Select:
SELECT name FROM employee GROUP BY 1;
No matter what, it will always group by the first column given in the select.
In this case, the column 'name' is grouped.
So if we alternate the above statement to:
SELECT department FROM employee GROUP BY 1;
We now group the department, without having to change the '1' in the group by.
EDIT: (as requested by Stewart)
If we have the following Data in table 'employe':
-- name --
Walt
Walt
Brian
Barney
A simple select would deliver all rows above, whereas the 'group by 1' would result in one Walt-row:
output with group by:
-- name --
Walt
Brian
Barney
+1 to #FabianBigler for answering first, but I'll add this:
http://dev.mysql.com/doc/refman/5.6/en/select.html says:
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column names, column aliases, or column positions. Column positions are integers and begin with 1.
For what it's worth, this is non-standard SQL, so don't expect it to work on other brands of SQL database.

Condition as a column named using AS "name" in mysql

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.