Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ecupirate1104
Starting Member
1 Post |
Posted - 2011-07-14 : 21:43:32
|
| Somewhat of a newbie when it comes to T-SQL so I'll try my best to explain :). I have the following columns in a table (along with sample data):Vendor | Title | Version | Pic | InstallCountMicrosoft Office 2010 1.jpg 12Microsoft Excel 2010 1.jpg 7Microsoft Office 2007 1.jpg 11Microsoft Word 2010 1.jpg 4I want to return distinct titles and order them by installcount. I keep getting duplicate titles returned with using DISTINCT and ORDER BY. Here's my SQL statement...SELECT DISTINCT title, pic, vendor, installcount FROM table ORDER BY installcountIt returns:OfficeOfficeExcelWordI want:OfficeExcelWordI know that DISTINCT returns unique rows, hence the two "Office" values. But I'm not sure where to go from here. Please help. I can provide more info if needed.Thanks in advance |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2011-07-14 : 22:27:12
|
| "Office" has two different InstallCounts (12 and 11), which one do you want?DISTINCT works across all selected columns so for this combination of columns you are getting distinct ROWS (title, pic, vendor, installcount)Be One with the OptimizerTG |
 |
|
|
parody
Posting Yak Master
111 Posts |
Posted - 2011-07-15 : 08:35:16
|
| Perhaps you mean to group?Something likeSELECTTitle,sum(InstallCount)FROM tableGROUP BY TitleORDER BY sum(InstallCount)Of course you could use min,max or whatever if preferred. |
 |
|
|
|
|
|