次のようなクエリがあります。
orp CategoryId Category City
999 1 bouquets city-a
999 2 arrengmnt city-b
999 4 chocolate city-c
各行に、次のカテゴリのショップの数を追加します。
orp CategoryId Category City NoShops
999 1 bouquets city-a 10
999 2 arrengmnt city-b 14
999 4 chocolate city-c 3
CategoryId(@CategoryId)を取得する必要があるので、次のようなことを考えていました(明らかに機能していません)。
SELECT Categories.OrderInPage as orp, CategoriesInLanguages.CategoryName, @CategoryId = Categories.Id
,(
SELECT COUNT(Shops.Id) as NoShops
FROM Cities INNER JOIN
CitiesInLanguages ON Cities.Id = CitiesInLanguages.CityId INNER JOIN
ShopsInCities ON Cities.Id = ShopsInCities.CityId INNER JOIN
CategoriesInLanguages INNER JOIN
Categories ON CategoriesInLanguages.CategoryId = Categories.Id INNER JOIN
ProductstInCategories ON Categories.Id = ProductstInCategories.CategoryId
WHERE (Products.IsEnable = 1) AND ( Categories.Id = @CategoryId)) AS Z
)
FROM Cities INNER JOIN
CitiesInLanguages ON Cities.Id = CitiesInLanguages.CityId INNER JOIN
ShopsInCities ON Cities.Id = ShopsInCities.CityId INNER JOIN
CategoriesInLanguages INNER JOIN
Categories ON CategoriesInLanguages.CategoryId = Categories.Id INNER JOIN
ProductstInCategories ON Categories.Id = ProductstInCategories.CategoryId
WHERE (Products.IsEnable = 1)