Solved

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

Posted on 2011-02-22
2
360 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
2 Comments
 
LVL 15

Accepted Solution

by:
pateljitu earned 500 total points
Comment Utility
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 40

Expert Comment

by:Sharath
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now