Mysql query concate data into group concate - mysql

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;

Related

SQL COUNT if a specific value is found and output in the same line

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;

Mysql group by does opposite of grouping by

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

CONCAT() inside GROUP_CONCAT() with count

I am trying to get results with group_concat, concat and count functions in MySQL but it gives me error.
here is my table
First, when I try to get count and status with concat, it works fine.
$query="SELECT CONCAT(`status`,':',count(status)) FROM `mytable` GROUP BY status"
output:
Hold:2
Completed:3
Cancelled:2
It's all fine till here. Now I want this output in one row. So I tried using GROUP_CONCAT().
$query="SELECT GROUP_CONCAT(CONCAT(`status`,':',count(status)) SEPARATOR ',') as rowString FROM `mytable` GROUP BY status"
but now its giving me error " Invalid use of group functions"
Note: the same query works if I replace count(status) with some other field from table ( without count). The count() function is causing some problem when used in this manner.
Desired Output
Hold:2,Completed:3,Cancelled:2
Appreciate your help.
You cannot nest aggregation functions (count() is in the arguments to group_conat()). One solution is to select from a nested subquery.
SELECT group_concat(status, ':', count SEPARATOR ',') rowstring
FROM (SELECT status,
count(*) count
FROM mytable
GROUP BY status) x;

PHP MySQL Hierarchy Data group_concat

any help would be amazing. currently got the following MySQL query:
SELECT
GROUP_CONCAT( sp.`slug` SEPARATOR '/' )
FROM
`category` sn, `category` sp
WHERE
sn.lft BETWEEN sp.`lft` AND sp.`rgt`
AND sn.`id` =3
ORDER BY
sp.`lft` ASC;
Without the group_concat function this returns the desired results in the correct order. as soon as I apply group_contact the order is mashed up and incorrect. Does anyone know how to rewrite the query to give me the concatenated result desired and in the correct order?
Im at a loss on this. On a side note does anyone know how to rewrite it using the "inner join" statements as opposed to the two table names quoted next to each other as I dont understand how its joining the two tables.
Try this:
SELECT
GROUP_CONCAT( sp.`slug` ORDER BY sp.`lft` ASC SEPARATOR '/' )
FROM
`category` sn,
INNER JOIN `category` sp
ON sn.lft BETWEEN sp.`lft` AND sp.`rgt`
WHERE
sn.`id` =3

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.