CONCAT() inside GROUP_CONCAT() with count - mysql

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;

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 subquery on itself returns all records

The following query returns all results, but according to my understanding should return the same ID's as the subquery. Could somebody explain why the subquery that returns all ID's (when ran separately) returns all records
select mya.id from mytable mya WHERE mya.id IN (
SELECT myb.id
FROM mytable myb
GROUP BY myb.mysecondcolumn
)
The subquery when ran as individual query would for example return 1,5,10,15, but when I run this query above it returns 1,2,3,4,5,...
Thanks!
Your query is malformed. You need an aggregation function in the subquery. Perhaps:
select mya.id
from mytable mya
where mya.id in (SELECT MIN(myb.id)
FROM mytable myb
GROUP BY myb.mysecondcolumn
);
This does not explain your actual problem. My guess is that the subquery is returning all ids, but just in a different order. You can check if this is the case by looking at the results of:
SELECT MIN(myb.id)
FROM mytable myb
GROUP BY myb.mysecondcolumn
ORDER BY MIN(myb.id)

Mysql query concate data into group concate

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;

Group different groups as the same and count?

I have the following query:
SELECT owner,
typeOwner,
type,
count(*) AS count
FROM myTable
GROUP BY typeOwner
ORDER BY count DESC
LIMIT 0, 30
Now , the typeOwner values = '<test>a</test>' but , some times the typeOwner field will have some string else like '<test>b</test>, how can i let this query count the <test>b</test> as a group of '<test>a</test>.
I want to make an exception for this, I mean typeOwner <test>a</test> AND typeOwner <test>b</test> should be counted as one row that have two count.
here's a fiddle : http://sqlfiddle.com/#!2/70053/1 , take look
actually these are should be grouped by <type></type><aId></aId>
"actually these are should be grouped by "
You can GROUP BY something relatively ugly like:
GROUP BY LEFT(typeOwner, position('<xType>' in typeOwner) -1)
But you are probably better off preprocessing the data in some fashion. I'm not sure how MySQL handles xml, but in SQL server I might extract the XML values into first class relation fields if I needed to do this sort of processing with any frequency.
sqlfiddle.com
Based on your sqlfiddle, this works
SELECT
left(typeOwner, instr(typeOwner, '<xType>')-1) as typeOwner,
owner,type, count(*) as count
FROM `test`
GROUP BY left(typeOwner, instr(typeOwner, '<xType>')-1)
ORDER BY count DESC
LIMIT 0 , 30

Checksum of SELECT results in MySQL

Trying to get a check sum of results of a SELECT statement, tried this
SELECT sum(crc32(column_one))
FROM database.table;
Which worked, but this did not work:
SELECT CONCAT(sum(crc32(column_one)),sum(crc32(column_two)))
FROM database.table;
Open to suggestions, main idea is to get a valid checksum for the SUM of the results of rows and columns from a SELECT statement.
The problem is that CONCAT and SUM are not compatible in this format.
CONCAT is designed to run once per row in your result set on the arguments as defined by that row.
SUM is an aggregate function, designed to run on a full result set.
CRC32 is of the same class of functions as CONCAT.
So, you've got functions nested in a way that just don't play nicely together.
You could try:
SELECT CONCAT(
(SELECT sum(crc32(column_one)) FROM database.table),
(SELECT sum(crc32(column_two)) FROM database.table)
);
or
SELECT sum(crc32(column_one)), sum(crc32(column_two))
FROM database.table;
and concatenate them with your client language.
SELECT SUM(CRC32(CONCAT(column_one, column_two)))
FROM database.table;
or
SELECT SUM(CRC32(column_one) + CRC32(column_two))
FROM database.table;