How can I can I query this and get this kind of result Toyota (3)?
This is my database structure.
Make | Model | Total Cars |
Toyota | Vios | 1 |
Toyota | Hilux | 2 |
You can use sum with group by to calculate the inventory of a specific make.
select sum(totalcars) from cars group by make
Demo: http://sqlfiddle.com/#!9/d6503/1
To include the make add it to the select query:
select sum(totalcars), make from cars group by make
http://sqlfiddle.com/#!9/d6503/2
You may also try this query :
SELECT count(*) as cnt FROM tableName WHERE Make='Toyota'
If you want to display all Makes woth count then
SELECT count(*) as cnt FROM tableName Group By Make
Try this
select `Make`, sum(`Total Cars`) from table_name group by `Make`;
Related
I'm using the Northwind database from W3 schools and my query is
SELECT Price from products group by Price having Price < max(Price)
It's currently showing no results, but how would I fix that? You can see the database here: https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_having
Additionally, is it possible to get more than one entry returned from having, i.e, all rows that meet the having clause condition. For example, in the link above, only unique countries are returned that meet the condition.
EDIT: Also with the Northwind database:
SELECT *FROM Customers GROUP BY City HAVING COUNT(City) > 2;
Why does it have unexpected behaviour, i.e, not return the rows where there are more than two occurrences of the city.
try like below
SELECT Price from products
where Price < (select max(Price) from products)
if you add the max(price) to the select
DROP table if exists t;
create table t
(price int);
insert into t values (1),(2),(1),(2),(3),(10);
SELECT Price, max(price) mp from t group by Price;
you get
+-------+------+
| Price | mp |
+-------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 10 | 10 |
+-------+------+
4 rows in set (0.001 sec)
the having test is on the grouped items and is never true . To produce the correct result you need the sub query suggested by #akina.
name order_id
tom | 1 |
tom | 0 |
tom | 2 |
tom | 3 |
tom | 4 |
ken | 2 |
ken | 1 |
ken | 0 |
I have a table like above, how can I select the data group by the name and order by the order id. I already try the query below but it's not the result I want.
SELECT * FROM tbl_dummy GROUP BY name ORDER BY order_id ASC
This might be what you want:
SELECT name, MIN(order_id) AS order_id
FROM tbl_dummy
GROUP BY name
ORDER BY order_id
SELECT * FROM tbl_dummy GROUP by name,order_id order by name,order_id
For using GROUP BY clause, you must use some type of aggregate function (SUM, MIN, MAX ..) on all the other columns that you are selecting.
Try understand it this way, if you do group by name in the above data, then you will have two rows in the result, one for tom and one for ken. But what value of order_id should it display against each name? It cannot display all the values separated by comma (or anything else like that). The value to be shown must be a value calculated using all the values corresponding to that name. It could either be sum, average, min or max of all the values.
Ref: http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
May be what you are looking for is to order your results by name first and then order_id in increasing order. In that case you can use order by multiple columns
SELECT name, order_id from yourtable order by name, order_id;
Once again i need yours help ;). I have a lot data and mysql request are slower and slower so the need request that i need i want group in one comand.
My example DB structure:
|product|opinion (pos/neg)|reason|
__________________________________
|milk | pos | good |
|milk | pos |fresh |
|chocolate| neg | old |
|milk | neg | from cow|
So i need information about all diffrent product (GROUP BY) count of it, and count of pos opinion for each product. I want output like that:
|product|count|pos count|
_________________________
|milk | 3 | 2 |
|chocolate| 1 | 0 |
I hope that my explain was good enought ;)
Or go to work: I write two commands
SELECT COUNT(*) as pos count FROM table WHERE product = "milk" AND opinion = "pos" GROUP BY `opinion`
And Second one
SELECT product, COUNT(*) FROM table GROUP BY `product`
I don't know how to join this two request, maybe that is impossible? In my real use code i have additional category collumn and i use WHERE in second command too
select product,
count(*) as total_count
sum(
case when opinion='pos' then 1
else 0 end
) as pos_count
from the_table
group by product;
SELECT product,
COUNT(*) TotalCount,
SUM(opinion = 'pos') POSCount
FROM tableName
GROUP BY product
SUM(opinion = 'pos') is MySQL specific syntax that counts the total of result based from the result of boolean arithmethic. If you want it to be more RDBMS friends, use CASE
SUM(CASE WHEN opinion = 'pos' THEN 1 ELSE 0 END)
SQLFiddle Demo
Please find db structure as following...
| id | account_number | referred_by |
+----+-----------------+--------------+
| 1 | ac203003 | ac203005 |
+----+-----------------+--------------+
| 2 | ac203004 | ac203005 |
+----+-----------------+--------------+
| 3 | ac203005 | ac203004 |
+----+-----------------+--------------+
I want to achieve following results...
id, account_number, total_referred
1, ac203005, 2
2, ac203003m 0
3, ac203004, 1
And i am using following query...
SELECT id, account_number,
(SELECT count(*) FROM `member_tbl` WHERE referred_by = account_number) AS total_referred
FROM `member_tbl`
GROUP BY id, account_number
but its not giving expected results, please help. thanks.
You need to use table aliases to do this correctly:
SELECT id, account_number,
(SELECT count(*)
FROM `member_tbl` t2
WHERE t2.referred_by = t1.account_number
) AS total_referred
FROM `member_tbl` t1;
Your original query had referred_by = account_number. Without aliases, these would come from the same row -- and the value would be 0.
Also, I removed the outer group by. It doesn't seem necessary, unless you want to remove duplicates.
One idea is to join the table on itself. This way you can avoid the subquery. There might be performance gains with this approach.
select b.id, b.account_number, count(a.referred_by)
from member_tbl a inner join member_tbl b
on a.referred_by=b.account_number
group by (a.referred_by);
SQL fiddle: http://sqlfiddle.com/#!2/b1393/2
Another test, with more data: http://sqlfiddle.com/#!2/8d216/1
select t1.account_number, count(t2.referred_by)
from (select account_number from member_tbl) t1
left join member_tbl t2 on
t1.account_number = t2.referred_by
group by t1.account_number;
Fiddle for your data
Fiddle with more data
I have table like this
-------------------------------------------------------------------
id | title | image | name |
-------------------------------------------------------------------
1 | xyzab | so.jpg | googl |
2 | acbde | am.jpg | artic |
3 | xyzab | pp.jpg | other |
i want to select unique or distinct title with it's image and name also.
DO not want to repeat the values. I use this this code
SELECT DISTINCT title,image,name,id FROM `some_table`
but this is not working fine
NOTE: The OP is working with MySQL
Using DISTINCT will ensure no 2 records have all columns matching, so this is working correctly.
If you want to return unique titles, you need to decide what image and name would be returned.
You could use a group by with an aggregate function to do this. For example:
SELECT title, MIN(image), MIN(name), MIN(id)
FROM `some_table`
GROUP BY title
But it depends on what results you are after...
You will need to specify the WINNER... in other words if there is a duplicate title but differening data in other columns you need to pick one...
For example you could try this.
select * from 'some_table' where id in (select min(id) from 'some_table' group by title)
DISTINCT is not applied to the one field after the keyword, but for fields in your select statement. What you're looking for is GROUP BY:
SELECT title,image,name,id FROM some_table GROUP BY title