I'm doing a query to show a count of members' uploads to my site.
So far I've got the basic count:
SELECT member_id,COUNT(*)
FROM uploads
GROUP BY member_id
...which, when echoed, displays the member_id and the amount of uploads they have.
However, what I'd like to do is display the member's firstname and lastname instead of just their id.
To do this I need to join the members and uploads table where members.member_id = uploads.member_id
I know I need to select members.member_firstname, members.member_lastname, members.member_id and uploads.member_id where members.member_id = uploads.member_id and throw in the count and group by. I'm just stumped by the syntax. Can anyone help me out?
SELECT members.member_firstname, members.member_lastname, COUNT(*)
FROM uploads
JOIN members
ON uploads.member_id = members.member_id
GROUP BY uploads.member_id
Try that.
Related
Please I need your help. I am talking about MySQL Database.
This is my query :
SELECT llx_facturedet.rowid, llx_societe.nom, llx_societe_extrafields.prof, llx_facture.ref, llx_facture.date_valid, llx_product.label, llx_facturedet.qty, llx_facturedet.subprice, llx_facturedet.total_ht FROM llx_societe CROSS JOIN llx_societe_extrafields ON llx_societe.rowid = llx_societe_extrafields.fk_object CROSS JOIN llx_facture ON llx_societe.rowid = llx_facture.fk_soc CROSS JOIN llx_facturedet ON llx_facture.rowid = llx_facturedet.fk_facture CROSS JOIN llx_product ON llx_product.rowid = llx_facturedet.fk_product ORDER BY rowid;
Here is the result :
enter image description here
You can notice that, patient6 and patient8 took the same date the items Product1 and Service1.
enter image description here
Hence, their invoice references and names are included in each item added.
How can I solve this without ignoring any ordered items?
may anybody help me with this task...
persistence provider is eclipselink 2.6.
i want to retrieve a list of users that may have 0 or n documents. because both tables have a few columns i want to use SELECT NEW Entity (userId, amountDocuments), i only need the user-id and the amount of documents for this task. if the user hasn't any documents yet, "0" should be shown, e.g.:
UserId: 1 2 3 4
AmountDocs: 0 1 0 3
Mapping for Documents in Entity User is as follows:
#OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL,mappedBy = "user", targetEntity = UserDocument.class)
#OrderBy("sortOrder ASC")
#JoinFetch(JoinFetchType.OUTER)
protected List<UserDocument>documents;
Mapping for User in Entity UserDocument is as follows:
#ManyToOne(cascade=CascadeType.ALL)
protected User user;
and here is the jpa-query:
SELECT DISTINCT
NEW user.entity.User(u.id,count(doc.user)) FROM User u
LEFT JOIN u.documents doc ON doc.user = u
AND doc.active = 't'
GROUP BY u.id
Problem is, that i only retrieve those two users who have documents that match doc.active='t'.
I also tried it with SIZE(u.documents) which also just returns two users and additionally wrong document-count values.
What is wrong here?
Thanks in advance!
finally after spending hours with that simple stuff, the right solution came with:
SELECT DISTINCT
NEW user.entity.User(u.id,count(doc)) FROM User u
LEFT JOIN u.documents doc ON doc.user = u AND doc.active = 't'
GROUP BY u.id
i have to count the left joined documents itself not the users.
I have a table called 'Artists' which has columns Artist_ID, Artist_Name, Artist_Genre and a table called 'Albums' with Artist_ID, Album_Id, Album_Name, ALbum_Number_Songs etc.
I join these two tables via Artist_Id. Now I want to know all Artists who have never had an album with over 12 songs.
I have tried this:
SELECT Distinct Artist_Name
FROM Artists
INNER JOIN Albums
ON Albums.Artist_ID=Artists.Artist_Id
WHERE Album_Number_Songs < 12
however this just checks all instances and output will be just on whether or not the album has < 12 not the artist overall...does anyone know?
Please try this query:
SELECT Artist_Name FROM Artists
WHERE Artists.Artist_ID NOT IN
(SELECT Albums.Artist_ID FROM Albums WHERE Albums.Artist_ID = Artists.Artist_ID AND Albums.ALbum_Number_Songs > 12)
This also includes the artists with no albums which I think corresponds better to the initial task.
Try this:
select r.artist_name
from artists r
left join albums l on r.artist_id = l.artist_id
group by r.artist_name
having max(l.album_number_songs) <= 12
Note: I have tested this on MS SQL Server, but I think this syntax should work for Oracle and MySQL too.
thanks in advnace for reading this. I'm not really good with SQL so please pardon any stupid mistake...
Here is the deal, I have four tables (i'm only going to give the basic fields, and dependencies between tables, for the sake of simplicity):
Company: companyId, companyName
User: userId, userName
Project: projectId, projectUserId, projectCompanyId, projectDate
Study: studyProjectId
The dependencies are like so:
A project is for a client (projectUserId) and carried out by a company (projectCompanyId)
There can be many studies for the same project, but each study is for one project (studyProjectId)
Here is the kind of request I'd like to write, but it doesn't work right now:
SELECT
project.projectId,
company.companyName,
user.userName,
COUNT( study.studyId ) AS numberStudies
FROM project, company, user, study
WHERE company.companyId = project.projectCompanyId,
AND user.userId = project.projectUserId,
AND study.studyProjectId = project.projectId
ORDER BY company.companyId, user.userId, project.projectDate;
It returns one record for which numberStudies equals the total number of studies. If I remove the COUNT from the SELECT, then I get the type of result I want, but without the column numberStudies (of course). Hoping you understand what I'm trying to get, what am I doing wrong here?
Thanks again in advance :)
EDIT: If possible, I'd like the request to show records even when numberStudies is 0.
As in the comments, you need a GROUP BY clause when you want to have aggregate results (like it seems you want: "Number of Studies per project, company and user"). So, the first thing to do is add:
GROUP BY project.projectId, company.companyName, user.userName
Notice that the three columns are exactly the three that you have (unaggregated) in the SELECT list.
SELECT
project.projectId,
company.companyName,
user.userName,
COUNT(study.studyId) AS numberStudies
FROM project, company, user, study
WHERE company.companyId = project.projectCompanyId,
AND user.userId = project.projectUserId,
AND study.studyProjectId = project.projectId
GROUP BY project.projectId, company.companyName, user.userName
ORDER BY company.companyId, user.userId, project.projectDate ;
This will show what you want but there are still a few issues:
First, you are using the old (SQL-89) syntax of implicit joins with the conditions in the WHERE clause. This syntax is not deprecated but the new (well, 20 years old SQL-92) syntax with the JOIN keyword has several advantages.
We can add aliases for the tables for readability.
There may be two companies or users with same name so we should group by their IDs, not only their names.
One advantage of explicit JOIN syntax is that it's easy to have results when there are no rows to join (as you want to show when there is no studies for a project). Just LEFT JOIN the study table.
So, the query becomes:
SELECT
p.projectId,
c.companyName,
u.userName,
COUNT(s.studyId) AS numberStudies
FROM
project AS p
JOIN
company AS c ON c.companyId = p.projectCompanyId
JOIN
user AS u ON u.userId = p.projectUserId
LEFT JOIN
study AS s ON s.studyProjectId = p.projectId
GROUP BY
c.companyId,
u.userId,
p.projectId,
c.companyName, u.userName
ORDER BY
c.companyId,
u.userId,
p.projectDate ;
you probably need a LEFT JOIN between study and project
I have 3 table customer (customerid,name), customerbooking(bookingid,customerid), transact(transacted,bookingid,typeoftransaction)
I want to fetch the name of the ‘customer name’ who has the maximum typeoftransact=’current’. Customer table is linked to customerbooking via customerid, and customerbooking is linked to transact via bookingid. Using join I am able to get the individual records, but unable to get the Max value
Please try this to meet your scenerio
SELECT
C.Name
, Count(BookingID)
FROM Customer C
INNER JOIN customerbooking CB ON CB.CustomerID = C.customerId
INNER JOIN transact T ON T.bookingid = CB.BookingId
WHERE T.Typeoftransaction='current'
GROUP BY C.Name
Hope this helps