mySQL - query to group values together - mysql

I have a query (below) which returns and ID value and two additional associated values (ReplacesReference). The query returns 2 records. I want to return one record so I need to concatenate the first "ReplacesReference" value with a comma ',' and the second "ReplacesReference" value.
my query is as follows
select
distinct(c.wiid) as ID,
a.reference as ReplacesReference
from npp_projects a,
npp_assoc b,
npp_projects c
where a.id = b.parent_id
and b.type = 'replace'
and c.id = b.child_id
and c.reference like '%EN 815%'
Its output is :
ID | ReplacesReference
====================================
CEN3406 | I.S. EN 815:1996
CEN3406 | I.S. EN 815:2004
I want to be able to return the below output:
ID | ReplacesReference
====================================
CEN3406 | I.S. EN 815:1996 , I.S. EN 815:2004
Many thanks in advance for your help - I am tearing my hair out with this one !

You want to use group by instead of select distinct. Your use of parentheses around c.wild suggests that you don't understand how select distinct works. It applies to all columns in the select list. The parentheses don't affect it. The query looks like:
select p2.wiid as ID,
group_concat(distinct p.reference separator ', ') as ReplacesReference
from npp_projects p join
npp_assoc a
on p.id = a.parent_id
npp_projects p2
on p2.id = a.child_id
where a.type = 'replace' and p2.reference like '%EN 815%'
group by p2.wild;
The changes I made:
Added the group_concat() to get the list you want.
Added the group by to replace the select distinct.
Changed the table aliases to use table abbreviations so the query is easier to follow.
Changed the join syntax to the more powerful explicit join syntax. As a simple rule: just do not use commas in the from clause.

You'd have to do something like:
SELECT id, GROUP_CONCAT(string SEPARATOR ' ') FROM table GROUP BY id;
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat

Related

How to query selecting name with specific code in field and the same time has no code in the same field

Example of my table
Name Code
Jolas sd02
Jolas da014
Albert cca01
Albert da014
Julito sd02
Julito da014
Vergel cca01
Vergel da014
I want to select all da014 without sd02, so my output should be Vergel and Albert because both of them has no SD02
How should i query it
Thanks
You can use conditional aggregation based filtering using GROUP BY with HAVING clause:
SELECT Name
FROM your_table_name
GROUP BY Name
HAVING
SUM(Code = 'da014') AND /* Has code `da014` */
NOT SUM(Code = 'sd02') /* Does not have code sd02 */
I generally prefer the group by approach for these types of problems. But if performance is an issue, then this might be faster with the right indexes:
select t.name
from t
where t.code = 'da014' and
not exists (select 1 from t t2 where t2.name = t.name and t2.code = 'sd02');
In particular, you want indexes on t(code, name) and t(name, code) -- one is for the outer query and one is for the subquery.
If you do use group by, then filtering before the aggregation can help performance:
select t.name
from t
where t.code in ('da014', 'sd02')
group by t.name
having sum(t.code = 'sd02') = 0;
You only have to check 'sd02'. If it is not there, then name must have the other code to be processed by the group by.

group_concat result not work in IN condition

Is there any possible way to put result of group_concat in IN condition of SQL query.
Here in network master table i have comma separated fields in industryId column.
Like,
userId industryId
123 3831
123 2832,3832
123 3833
Example:
select group_concat(industryId order by cId SEPARATOR ',') from
network_master where userId = 123
and it gives me this type of output 3831,2832,3832,3833
I got this type of output using upper query,
userId industryId
123 3831,2832,3832,3833
and now i done this things,
select * from industry_master where industryId in (select
group_concat(industryId order by cId SEPARATOR ',') from
network_master where userId =123 group by userId);
In this query result, I got details output of industryId=3831 only. I did not get other Ids output.
I need all the industryId output in this query. How to i achieve this things in mysql.
Any help would be appreciated.
I have tried above case but not working on my side. I just edited above answer and removed > 0 then I can see your expected output.
Can you try below code?
select * from industry_master where find_in_set(industryId, (select group_concat(industryId order by cId SEPARATOR ',')
from network_master where userId = 123 group by userId));
you don't need group_concat and IN clause you can use a join
select i.*
from industry_master i
INNER JOIN network_master n on i.industryId = n.industryId
AND n.userId =123
group_concat return a string but you need values so using the string in IN clause don't work correctly.
or if can work only an a string you could trying using field_in_set > 0
select * from industry_master
where find_in_set(industryId, select group_concat(industryId order by cId SEPARATOR ',')
from network_master where userId =123 group by userId) ;
or

MySQL select query using 2 conditions

My following query shows results only if dcno is exists in dc_detail table
but what i actually need is even then if dcno is not exists in dc_detail table it should define dcno as 0 and select that results also
Thanks in advance
SELECT p.po_no as id, DATE_FORMAT(p.po_date, '%d-%m-%Y')as po_date, p.customer, p.cust_po as po_no,p.tot_ord_qty,
DATE_FORMAT(p.delivery_date, '%d-%m-%Y')as delivery_date,p.dc_status,p.inv_status,p.tot_dc_qty,p.tot_inv_qty,
GROUP_CONCAT(distinct d.dc_no SEPARATOR ', ') as dc FROM po_header p, dc_details d
where p.cust_po=d.cust_po
group by p.cust_po;
You should use left join to include headers that have no details. That will give you rows with nulls for the detail fields that don't exist.
To get the value 0 instead of null you can use coalesce function.
The relevant parts of the query are
SELECT COALESCE(d.dc_no, 0),...
FROM po_header p LEFT JOIN dc_detail d ON p.cust_po=d.cust_po
Coalesce function returns the first non null argument.

Concatenate Sql column

SELECT DISTINCT a.assessement_group_id,
b.title as dataa
FROM assessment_group a
JOIN assessment_category b
WHERE a.assessement_group_id = b.group_id
I am using the join to display the data.the result are shown below
100 Partner Business Profile
99 Partner Activation
99 ajay test
100 ajaytest123
But i want this type of answer
100 Partner Business Profile,ajaytest123
99 Partner Activation,ajay test
You can use GROUP_CONCAT(DISTINCT ...) along with GROUP BY to get the output you want:
SELECT a.assessement_group_id,
GROUP_CONCAT(DISTINCT b.title)
FROM assessment_group a
INNER JOIN assessment_category b
ON a.assessement_group_id = b.group_id
GROUP BY a.assessement_group_id
SQLFiddle
By the way, I replaced your old-style implicit join syntax with an explicit INNER JOIN. It is generally considered bad practice now to put join conditions into the WHERE clause.
Try something as follows, hope this helps
select id, group_concat(`dataa` separator ',') as `dataa`
from
(
SELECT distinct a.assessement_group_id id, b.title as dataa
from assessment_group a JOIN assessment_category b
WHERE a.assessement_group_id=b.group_id
) tbl
group by id;
See the MySql GROUP_CONCAT() as part of a grouped query.
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html
You would add a GROUP_BY assessement_group_id to the end of the query, for example.
In mySql you can use the GROUP_CONCAT function, see here more details: http://dev.mysql.com/doc/refman/5.6/en/group-by-functions.html#function_group-concat.
In your initial query you need to add GROUP_CONCAT(b.title) in select clause and also a group by after assessement_group_id.
This one will help you
SELECT A.ASSESSEMENT_GROUP_ID, GROUP_CONCAT(B.TITLE SEPARATOR ' , ') AS DATAA FROM ASSESSMENT_GROUP A, ASSESSMENT_CATEGORY B WHERE A.ASSESSEMENT_GROUP_ID=B.GROUP_ID GROUP BY A.ASSESSEMENT_GROUP_ID;

Combining multiple lines of a ResultSet into a single line

I have a query like this:
SELECT DISTINCT
obl_books.book_id,
obl_books.long_title,
obl_books.short_title,
obl_authors.first_name
FROM
obl_books,
obl_authors,
books_authors
WHERE
obl_books.book_id = books_authors.book_id
AND
obl_authors.author_id = books_authors.author_id
AND
obl_books.short_title = 'SQL'
It gives me 2 separate rows for each author. I want it in a single row like this:
Book1| SQL REFERENCE | author1 | author2 | author3
How can I achieve it directly in SQL query or by doing something to ResultSet result? Kindly guide. Please tell me without any PLSQL mechanism.
If your are using MySQL try this:
SELECT DISTINCT
obl_books.book_id,
obl_books.long_title,
obl_books.short_title,
GROUP_CONCAT(obl_authors.first_name, ',')
ETC...
Change the separator (comma) with whatever you want
Depends which rdbms, but you might try
SELECT DISTINCT obl_books.book_id||obl_books.long_title|| etc...
Or look up 'Concat()' in the help
Use JOIN instead of the old SQL-89 implicit join WHERE syntax
Remove the DISTINCT
Add a GROUP BY
Use GROUP_CONCAT() to concatenate all the names in one column:
SELECT
b.book_id,
b.long_title,
b.short_title,
GROUP_CONCAT(a.first_name
ORDER BY a.first_name
SEPARATOR ', '
) AS first_names
FROM
obl_books AS b
JOIN
books_authors AS ba
ON b.book_id = ba.book_id
JOIN
obl_authors AS a
ON a.author_id = ba.author_id
WHERE
b.short_title = 'SQL'
GROUP BY
b.book_id,
b.long_title,
b.short_title ;