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 ;
Related
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;
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
I need to do a correlated SQL Query and for that purpose i need to provide an alias to outer query which in which I perform an inner join. I am not able to do the alias
SELECT DISTINCT(name)
FROM PERSON
INNER JOIN M_DIRECTOR AS dira
ON (dira.PID = M_DIRECTOR.PID) as dira
WHERE 9 > (
SELECT COUNT(MID) FROM M_DIRECTOR WHERE name = dira.name
) ;
I didn't really understand what you want to do, but I guess
select
distinct p.name,
count(d.MID) cnt
from
hindi2_PERSON p
inner join
hindi2_M_DIRECTOR d
on
p.PID = d.PID
group by
p.name
having count(d.MID) > 9
;
would do what you want
I dont know what you are asking and what you mean by make an alias to an eniter result ?
but you are doing
select distinct(name) as othername
which is you are selecting name and you are giving here othername as an alias
then you retrieve it in result
$row['othername']
There's still something missing. From what you write, there is a field name in the M_DIRECTOR table?
Please show all the tables and attributes involved, use an SQL Fiddle to prepare an example.
SELECT DISTINCT(name)
FROM PERSON as p
INNER JOIN (
SELECT COUNT(MID), PID FROM M_DIRECTOR WHERE name = dira.name
) as d
ON (p.PID = d.PID) ;
I am stuck and I am trying to find a solution, so please help!
I have 3 tables:
Books (id, title, ...)
Authors (id, name, surname, ...)
book_authors (bookID, authorID, whatisdoing)
Now I want to retrieve all books depending on the title (user search) and all the other info (author name, surname). But, I want the book titles to be unique with only the first occurrence of the book_authors.whatisdoing to be shown.
In MS Access I achieved that with first function, but now first does not work and with min I didn't get the results I want.
Any help would be appreciate.
The query in Access was:
SELECT
First(book_authos.whatisdoing) AS FirstOfidiothta_ID,
First(authors.name) AS onoma,
First(authors.surname) AS eponymo,
books.ID, books.title, books.photoLink
FROM (books
INNER JOIN book_authors ON books.ID = book_authors.book_ID)
INNER JOIN authors ON book_authors.author_ID = authors.ID
GROUP BY
books.ID, books.titlos, books.photoLink, books.active
HAVING
(((books.titlos) Like '%" & textString & "%') AND
((books.active)=True) AND ((First(authors.active))=True))
ORDER BY
First(book_authos.whatisdoing), books.title
EDIT: Changed based on OP comments.
EDIT 2: Revised to correct flaws.
You might give this a try.
SELECT aba.name, aba.surname, aba.whatisdoing,
b.ID, b.title, b.photoLink
FROM Books b
JOIN
(
SELECT ba.bookID, ba.whatisdoing, a.name, a.surname,
ROW_NUMBER() OVER (PARTITION BY ba.bookID ORDER BY ba.whatisdoing) AS sequence
FROM book_authors ba
JOIN Authors a ON ba.authorID = a.id
WHERE a.active = 1
) AS aba ON b.id = aba.bookID
WHERE b.title LIKE '%' + #textString + '%'
AND b.active = 1
AND aba.sequence = 1
The ROW_NUMBER function assigns a row number to the row based on the groups defined in the PARTITION BY clause and the order defined by the ORDER BY clause.
#textString is a SQL Server variable. I'm not sure how you would assign this in your situation.
The boolean data type is BIT in SQL Server and the values are 0 for false and 1 from true.
I am having following database schema, I want to fetch name of all categories with no of quotes related to that category . The query that i wrote giving me one row only can u please tell me the resource efficient query.
SELECT SC.Name, Count(*) AS Quotes
FROM status_categories AS SC
INNER JOIN status_quotes AS SQ ON SC._id = SQ._category_id
GROUP BY SC.Name
SELECT status_categories.NAME, COUNT(status_quotes.category_id)
FROM status_categories JOIN status_quotes ON status_categories._id = status_quotes.category_id
GROUP BY status_categories._id;
Try the following:
SELECT `c`.`name`, COUNT(*) AS `Number of quotes`
FROM `status_categories` AS `c`
INNER JOIN `status_quotes` AS `q`
ON `q`.`category_id` = `c`.`_id`
GROUP BY `c`.`_id`;
EDIT
Feel free to leave out the ` character. But that is the safe way of doing it, even though it looks a bit nasty.