2

I have this MSSQL Query which works

SELECT c.CategoryName + ' (' + cast(count(ic.CategoryId) as varchar(255)) + ')' AS
 CategoryName, count(ic.CategoryId) AS NumPhotos,
  c.Slug, c.ParentCategoryId, c.Id
FROM Categories
 c LEFT JOIN
 ItemCategories ic
 on ic.CategoryId = c.Id
GROUP BY c.CategoryName, c.slug, c.ParentCategoryId, c.id
ORDER BY ParentCategoryId DESC

And I want to return only rows, WHERE NumPhotos>0 but temporary columns are not allowed on SQL WHERE clause

Mario
  • 13,941
  • 20
  • 54
  • 110

2 Answers2

2

The having clause is the canonical solution to your problem. However, I suspect that an inner join would also be appropriate:

SELECT c.CategoryName + ' (' + cast(count(ic.CategoryId) as varchar(255)) + ')' AS CategoryName,
       count(ic.CategoryId) AS NumPhotos,
       c.Slug, c.ParentCategoryId, c.Id
FROM Categories c INNER JOIN
     ItemCategories ic
     on ic.CategoryId = c.Id
GROUP BY c.CategoryName, c.slug, c.ParentCategoryId, c.id
ORDER BY ParentCategoryId DESC;

Without sample data, it is hard to be sure, but I'm pretty sure this does the same thing.

The advantage of an inner join is that it might be more efficient, because less (maybe only slightly less) data would be processed and the optimizer has more opportunities to pick the best join algorithm.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

Existing comments have given adequate answers, but here is another solution, using a "virtual table":

SELECT * FROM ( 
SELECT c.CategoryName + ' (' + cast(count(ic.CategoryId) as varchar(255)) + ')' AS
 CategoryName, count(ic.CategoryId) AS NumPhotos,
  c.Slug, c.ParentCategoryId, c.Id
FROM Categories
 c LEFT JOIN
 ItemCategories ic
 on ic.CategoryId = c.Id
GROUP BY c.CategoryName, c.slug, c.ParentCategoryId, c.id
) 
WHERE NumPhotos>0
ORDER BY ParentCategoryId DESC
Turophile
  • 3,367
  • 1
  • 13
  • 21
  • It seems that it also works with HAVING count... after the ORDER BY, and it's much faster. – Mario Dec 01 '15 at 22:22
  • running your query gives me this error: Msg 1033, Level 15, State 1, Line 11 The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified. – Mario Dec 01 '15 at 22:29