This is the query and result without using grouping by:
Query without grouping by
SELECT count(T2.id_korisnika) As brojPonuda, CONCAT(T2.ime, ' ', T2.prezime) AS naziv
FROM ponude T1, tblKorisnici T2
WHERE T1.id_korisnika = T2.id_korisnika
And this is the query and result with grouping by: Query with grouping by
SELECT count(T2.id_korisnika) As brojPonuda, CONCAT(T2.ime, ' ', T2.prezime) AS naziv
FROM ponude T1, tblKorisnici T2
WHERE T1.id_korisnika = T2.id_korisnika
Group by naziv
My question is why are they opposite of what's supposed to happen? Isn't group by supposed to group multiple rows with same values and produce a result of an aggregate function alongside the grouped value? So why in this case it's doing the opposite of that? When I don't use group by it actually gets automatically grouped by and if I use it I get a weird result where the values are separated, also I don't understand why in some rows there is a 2 and all the others are a 1
Update: it seems to work when I replace the concat and the "naziv" column with id_korisnika. So maybe mysql can't group properly with strings?
Query with replaced column
Here is the data in the database:
tblKorisnici
ponude
Your first query should be written as:
SELECT count(T2.id_korisnika) As brojPonuda,
CONCAT(T2.ime, ' ', T2.prezime) AS naziv
FROM ponude T1 JOIN
tblKorisnici T2
ON T1.id_korisnika = T2.id_korisnika ;
This is an aggregation query with no GROUP BY. Hence, it theoretically returns one row -- treating the entire result set (from the JOIN) as a single group. That said, the query is syntactically incorrect, because naziv is not in the GROUP BY.
The second query:
SELECT count(T2.id_korisnika) As brojPonuda,
CONCAT(T2.ime, ' ', T2.prezime) AS naziv
FROM ponude T1 JOIN
tblKorisnici T2
ON T1.id_korisnika = T2.id_korisnika
GROUP BY naziv;
This simply says to produce a result set where the rows are defined by distinct values of naziv. Each of these rows is summarized from the result of the JOIN and the count is the first column.
The issue was I had another column named "naziv" in my "ponude" table and the sql apparently prioritized that name instead of the alias and therefore the results were incorrect.
SQL - using alias in Group By
Related
I am trying to create a Flow Control Function query on one of my mysql database. That can fetch some data based on previously fetched column values, before sharing my current query, I am explaining what i am trying to achieve.
As you can see in the below SELECT QUERY the result of first IF CLAUSE lead_id has been used as a parameter in WHERE CLAUSE on third IF CLAUSE and that works perfectly, similarly the result of 3rd IF CLAUSE Cont_id has been used in fourth IF CLAUSE as a WHERE CLAUSE parameter, and that also works as desired, but when we use these returned values lead_id and Cont_id of IF CLAUSES for comparing a value in either IF CLAUSE or CASE CLAUSE, it doesn't work, can anyone help how to achieve this by using a different approach?
My query is like this..
SELECT *,
IF(tte.task_id>0, (SELECT mt.lead_id FROM mt WHERE mt.id=tte.task_id), 'NA') AS lead_id,
IF(tte.task_id>0, (SELECT mt.assigned_to_staff_id FROM mt WHERE mt.id=tte.task_id), 'NA') AS staff_id,
IF(tte.task_id>0, (SELECT leads.contact_id FROM leads WHERE leads.id=lead_id),'NA') AS Cont_id,
IF(tte.task_id>0, (SELECT contacts.Name FROM contacts WHERE contacts.id=Cont_id),'NA') AS PC_Name,
CASE WHEN tte.task_id>0 THEN (SELECT contacts.Name AS LC_Name FROM contacts WHERE contacts.id=PC_id) ELSE 'NA' END,
FROM tte ORDER BY request_log_time DESC;
I want to use returned values lead_id and Cont_id of IF CLAUSES for comparing a value in either IF CLAUSE like this:
IF(Cont_id>0, (SELECT contacts.Name FROM contacts WHERE contacts.id=Cont_id),'NA') AS PC_Name
or CASE CLAUSE like this:
CASE WHEN Cont_id>0 THEN (SELECT contacts.Name AS LC_Name FROM contacts WHERE contacts.id=Cont_id) ELSE 'NA' END
I have this DB: (note id is unique)
I am trying to get this:
What would be an efficient SQL query to achieve this?
SELECT
DISTINCT
foo2.group_id AS group_id,
if(foo1.group_id = foo2.group_id, foo1.id, NULL) AS id,
if(foo1.group_id = foo2.group_id, foo1.some_attr, NULL) AS some_attr
FROM mytable AS foo1
JOIN
(SELECT DISTINCT group_id FROM mytable) as foo2
ORDER BY group_id, id
You seem to be mixing a result set with formatting of the result set. SQL result sets are tables of consistent rows. The closest you can come is to change the sequence of your column names in the SELECT statement. You'll have to add your own line breaks at time of presentation.
Here is my table. I have made the following query and get the result:
mysql - Query:
SELECT
GROUP_CONCAT(CONCAT('', j0.rent_agree_id,'-',j0.rent_sche_id) ) AS agreement_ref_no
FROM
rent_provision_history AS j0
GROUP BY provision_date
Result:
But I want to see the result differently.
For example for the last result row i am getting now: 4-68,4-69,6-107,6-108,6-109
But I want to see it like that:
4(68,69)|5(107,108,109)
How can i do that?
You need two levels of aggregation:
SELECT provision_date,
GROUP_CONCAT(rent_agree_id, '(', rsi, ')' SEPARATOR '|') agreement_ref_no
FROM (SELECT j0.rent_agree_id, GROUP_CONCAT(j0.rent_sche_id) as rsi
FROM rent_provision_historyj0
GROUP BY j0.provision_date, j0.rent_agree_id
) j0
GROUP BY j0.provision_date;
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.
This is what i am doing
update t1 set x=a,y=b where a and b are obtained from (select query here)
I know the select query
The select query returns multiple results which are the same
When I use group by or distinct query execution slows down considerably
a and b are forward references so mysql reports an error
I want to set a equal to the value obtained in the first row and b equal to the value obtained in the first row for the respective columns, to avoid group by. I don't know how to refer to the first result from the select query.
How can i achieve all this?
LIMIT specifies the number of rows to return from the beginning of the result set:
SELECT * FROM t2 LIMIT 1; # Retrieve 1st row
LIMIT in your case is applied in the subquery in your from clause.
These linsk can help you out with update that uses a subquery:
Update with Subquery
Subqueries in MySQL, Part 1
you might be looking for something like...
update T1, (select Sub1.a, Sub1.b from YourSubQueryTable Sub1 where ... ) SubResult
set T1.a = SubResult.a,
T1.b = SubResult.b
where
Some T1.Criteria To be applied