Solved

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

Posted on 2011-02-22
2
365 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 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

732 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