I know this can be done, but I’m having a luddite moment here.
create table dbo.Fruit (
ID int identity,
FruitName varchar(20) not null
)
INSERT INTO dboFruit VALUES (‘Banana’)
INSERT INTO dboFruit VALUES (‘Banana’)
INSERT INTO dboFruit VALUES (‘Grape’)
INSERT INTO dboFruit VALUES (‘Banana’)
INSERT INTO dboFruit VALUES (‘Orange’)
INSERT INTO dboFruit VALUES (‘Grape’)
INSERT INTO dboFruit VALUES (‘Banana’)
Thanks, but that won’t do it. It would order the fruit alphabetically, not by number of rows. (That probably wasn’t clear from my example, come to think of it). If I had 5 entries for oranges and 3 for bananas, I’d want oranges to come first.
And, contrary to my example, I don’t actually have an ID field, and there may well be duplicates, so grouping might cause problems.
SELECT Fruit_1.ID, Fruit_1.FruitName
FROM Fruit INNER JOIN Fruit AS Fruit_1 ON Fruit.FruitName = Fruit_1.FruitName
GROUP BY Fruit.FruitName, Fruit_1.ID, Fruit_1.FruitName
ORDER BY Count(Fruit.FruitName) DESC , Fruit_1.ID;
Zev nailed it. I tried all of the other suggestions, and for some reason they all failed. I burned a lot of time trying to make them work, but somehow they didn’t.