Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL replication over high latency link 10 67
SQL Database - Move Tables from one Database to Another 4 47
Level out logo and increase size? 16 25
return value based on substr 10 48
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

749 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