I'm trying to count the number of softwares names using mysql
Here is my original that show everything
Select dico_soft.FORMATTED,dico_soft.EXTRACTED,softwares.NAME
From dico_soft
INNER JOIN softwares
ON dico_soft.EXTRACTED=softwares.NAME;
Where is what I added count and Group by sql
Select dico_soft.FORMATTED,dico_soft.EXTRACTED,softwares.NAME,count(*)
From dico_soft
INNER JOIN softwares
ON dico_soft.EXTRACTED=softwares.NAME;
Group BY = softwares.NAME;
the result only give me 1 row which adds everything, the group by doesn't work
it should be
Group BY softwares.NAME;
and not
Group BY = softwares.NAME;
remove "=" from GROUP BY of SQL query
Select dico_soft.FORMATTED,dico_soft.EXTRACTED,softwares.NAME,count(*)
From dico_soft
INNER JOIN softwares
ON dico_soft.EXTRACTED=softwares.NAME;
Group BY softwares.NAME;
I think you need to execute the following query
Select softwares.NAME,count(softwares.NAME) as Quantity
From dico_soft
INNER JOIN softwares
ON dico_soft.EXTRACTED=softwares.NAME;
Group BY softwares.NAME;
This will fetch software name and the quantity of each one
You are trying set GROUP BY on JOIN table. You need grouping by FROM table. Also not need = in GROUP BY
Select dico_soft.FORMATTED,
dico_soft.EXTRACTED,
softwares.NAME,
count(dico_soft.EXTRACTED)
From dico_soft
INNER JOIN softwares ON dico_soft.EXTRACTED = softwares.NAME;
Group BY dico_soft.EXTRACTED;
Related
I am developing an eCommerce website using Laravel 8. I write the following script for find out total price & total quantity under a single order number. From following script getting the ERROR where is the problem please help me.
*At first I write row mysql then i will convert laravel query Builder.
SELECT COUNT (total_price) as totaPrice, COUNT (productqty) as proQnty
FROM (SELECT DISTINCT order_id FROM orderDetails)
LEFT JOIN ordertbl
ON ordertbl.id = orderDetails.order_id;
I guess you want to sum the prices and quantities, so use SUM() aggregate function.
Also you should do a LEFT join of ordertbl to orderDetails and not the other way around:
SELECT ot.id,
SUM(od.total_price) AS totaPrice,
SUM(od.productqty) AS proQnty
FROM ordertbl ot LEFT JOIN orderDetails od
ON ot.id = od.order_id
WHERE ot.id = ?
GROUP BY ot.id;
Or, without a join:
SELECT SUM(total_price) AS totaPrice,
SUM(productqty) AS proQnty
FROM orderDetails
WHERE order_id = ?;
Replace ? with the id of the order that you want.
In Your raw in missing the tablename alis for the subquery ..
Your raw query should be
SELECT COUNT(total_price) as totaPrice, COUNT(productqty) as proQnty
FROM (
SELECT DISTINCT order_id FROM orderDetails
) T
LEFT JOIN ordertbl ON ordertbl.id = T.order_id;
I am trying to make a voting system through a SQL server, and I can't get it right. What I am trying to do is get the party with the highest amount of votes.
SELECT COUNT(*)
FROM Vote
INNER JOIN Members ON Vote.Voted = Members.PartyName
WHERE (PartyName is the biggest one)
I expect something like [DEMS][8], or at the very least, the party name of the party with the highest votes.
Rather than using a WHERE clause you need to use whatever the syntax is for the top record in your SQL dialect. You also need to group by partijnaam. This is a bit of a guess as I don;t know your exact data structure.
Postgres/MySQL
SELECT PartijNaam, COUNT(*)
FROM stem
INNER JOIN leden ON stem.Gestemt = Leden.lidnummer
GROUP BY PartijNaam
ORDER BY 2 DESC
LIMIT 1
SQL Server
SELECT TOP 1 PartijNaam, COUNT(*)
FROM stem
INNER JOIN leden ON stem.Gestemt = Leden.lidnummer
GROUP BY PartijNaam
ORDER BY 2 DESC
SELECT PartijNaam FROM leden INNER JOIN stem ON leden.LidNummer = stem.Gestemt GROUP BY PartijNaam ORDER BY COUNT(gestemt) DESC LIMIT 1;
try this:
SELECT PartijNaam, COUNT(*)as vote
FROM stem
INNER JOIN leden ON stem.Gestemt = Leden.lidnummer
GROUP BY PartijNaam
ORDER BY DESC
Firstly, I'm a beginner to MySQL and I'm still learning. I'm trying to join 2 tables to display a count. Primarily, I use 2 codes. One code to display names -
SELECT tag_logs.timestamp, People.Name FROM `tag_logs` INNER JOIN People WHERE tag_logs.tag_no = People.nametag
Another code to display count of names -
SELECT tag_logs.tag_no, COUNT(tag_logs.tag_no) FROM tag_logs GROUP BY tag_no HAVING COUNT(tag_no) >= 1
I want to display Name and a count number, instead of a tag number and count. I attempted to join both tables by using the following code, however, I've had little luck -
SELECT People.Name FROM `tag_logs` INNER JOIN People WHERE tag_logs.tag_no = People.nametag AND COUNT(tag_logs.tag_no) FROM tag_logs GROUP BY tag_no HAVING COUNT(tag_no) >= 1
I'm given an error when I try to call 'FROM tag_logs' a second time. Is there a way to work around this?
I aim to make this my final result, except I should be able to see names instead of numbers.
Two tables are joined using ON clause. You should learn joins.
SELECT People.Name ,COUNT(tag_logs.tag_no)
FROM `tag_logs`
INNER JOIN People ON tag_logs.tag_no = People.nametag
GROUP BY tag_logs.tag_no
HAVING COUNT(tag_no) >= 1
It should be
SELECT People.Name FROM `tag_logs`
INNER JOIN People on tag_logs.tag_no = People.nametag
GROUP BY tag_no HAVING COUNT(tag_no) >= 1
EDIT
SELECT People.Name, COUNT(tag_no) FROM `tag_logs`
INNER JOIN People on tag_logs.tag_no = People.nametag
GROUP BY tag_no HAVING COUNT(tag_no) >= 1
I believe the query that you want looks like this:
SELECT p.Name, COUNT(*)
FROM tag_logs tl INNER JOIN
People p
ON tl.tag_no = p.nametag
GROUP BY p.Name;
Notes:
COUNT(*) is shorter than COUNT(tl.tag_no) and they do the same thing.
GROUP BY clause now matches the SELECT. If you could have people with the same names, then add p.nametag to the GROUP BY. A version use only GROUP BY tl.tag_no is invalid SQL and should fail in most databases, because of the non-matching p.Name in the SELECT.
The HAVING clause (HAVING COUNT(tag_no) >= 1) is unnecessary, because the INNER JOIN requires at least one match and tag_no is never NULL (because it is used for the JOIN).
I introduced table aliases, so the query is easier to write and to read.
I have 2 tables: Projects and custom_values which are linked by the project id.
I did this inner join query
SELECT custom_values.customized_id ,custom_values.custom_field_id,
custom_values.value FROM projects INNER JOIN custom_values ON
projects.id=custom_values.customized_id order by custom_values.customized_id asc
You will find the responce of this query in the file joined.
I want to know if there is a way to group those rows by customized_id field.
By the way I tried Group by but it doesn't work
If you want group you need a aggregation function eg (max, min group_concat) and group by , if you want all the values related to a customized_id on a row you could use group_concat eg:
SELECT custom_values.customized_id
,group_concat(custom_values.value )
FROM projects
INNER JOIN custom_values ON projects.id=custom_values.customized_id
GROUP BY custom_values.customized_id
order by custom_values.customized_id asc
I want to group order's count to show how many clients have that number of orders.
I have come up with:
select count(*) as quantidade_pedidos, clientes.id
from pedidos
inner join clientes
on pedidos.cliente_id = clientes.id
where pedidos.aprovado = 1
group by quantidade_pedidos
but I just can't group by 'quantidade_pedidos' anyway.
Is there any way to group by a temporary column? Another way of doing this? show how many clients (number) have that number of orders placed?
Example
8 orders placed -> 3 clients have 8 orders placed
etc
Your original query is wrong. You need to group by clientes.id:
select count(*) as quantidade_pedidos, c.id
from pedidos p inner join
clientes c
on p.cliente_id = c.id
where p.aprovado = 1
group by c.id;
In an aggregation query, the unaggregated columns go in the group by, not the aggregated ones.
Also note that table aliases make the query easier to write and to read.
As for the question in the first line, use a subquery:
select quantidade_pedidos, count(*)
from (select count(*) as quantidade_pedidos, c.id
from pedidos p inner join
clientes c
on p.cliente_id = c.id
where p.aprovado = 1
group by c.id
) x
group by quantidade_pedidos;
But given that the query in the question doesn't work, I'm not sure this is what you really want to do.