Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I group the filename coloumn so it is only showing 1 result row per filename?

Posted on 2011-02-22
2
Medium Priority
?
369 Views
Last Modified: 2012-05-11
I need to group the distinct filenames. Currently there is a record for every instance of a filename.
select x.*, (SELECT ratingsum / filenamecount) as divnumber
from (
SELECT  filename, rating,
                          (SELECT     SUM(rating) AS Expr1
                            FROM        mytable AS GV
                            WHERE      GV.filename = a.filename) AS ratingsum,
                          (SELECT     COUNT(*) AS Expr2
                            FROM          mytable AS GV
                            WHERE       GV.filename = a.filename) AS filenamecount
                          
FROM         mytable AS a
) x 
ORDER BY divnumber DESC

Open in new window

0
Comment
Question by:m2ew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 15

Accepted Solution

by:
pateljitu earned 2000 total points
ID: 34954790
Group by clause on filename will do the job.

But in this case there is 'rating' is your select statement, if ratings varies you would still get each rows with different 'rating' and same filename. If 'rating' is not required removing from select statement will work.
select x.filename, (SELECT ratingsum / filenamecount) as divnumber
from (
SELECT  filename, rating,
                          (SELECT     SUM(rating) AS Expr1
                            FROM        mytable AS GV
                            WHERE      GV.filename = a.filename) AS ratingsum,
                          (SELECT     COUNT(*) AS Expr2
                            FROM          mytable AS GV
                            WHERE       GV.filename = a.filename) AS filenamecount
                          
FROM         mytable AS a
) x 
group by filename
ORDER BY divnumber DESC

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 34955912
You can simply do like this.
select filename,sum(rating)*1.0/ count(*) divnumber
  from mytable 
 group by filename
 order by divnumber desc

Open in new window

In fact, you can use avg function.
select filename,avg(rating) divnumber
  from mytable 
 group by filename
 order by divnumber desc

Open in new window

0

Featured Post

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
What we learned in Webroot's webinar on multi-vector protection.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question