Vraagje: als ik in één SQL statement naar SQLServer 7.0 wil GROUP-en op een CASE statement, dan moet ik in de GROUP BY het hele statement weer herhalen! Heeft iemand een handigere oplossing?
Voorbeeld:
SELECT CASE
WHEN tblBooks.Price < 10 THEN 'Cheap titles'
ELSE 'Expensive titles' END AS PriceRange,
COUNT(tblBooks.Title) AS Items
FROM tblBooks
GROUP BY PriceRange;
.. zou ik willen maar dat kan niet.
Wat wel werkt, maar omslachtig lijkt:
SELECT CASE
WHEN tblBooks.Price < 10 THEN 'Cheap titles'
ELSE 'Expensive titles' END AS PriceRange,
COUNT(tblBooks.Title) AS Items
FROM tblBooks
GROUP BY CASE WHEN tblBooks.Price < 10 THEN 'Cheap titles'
ELSE 'Expensive titles' END;
Response:
PriceRange Items
-------------------- --------
Cheap titles 45
Expensive titles 14
.. Het gewenste resultaat, maar ik moet het hele case statement in de group by herhalen! Kan dat niet handiger?
Voorbeeld:
SELECT CASE
WHEN tblBooks.Price < 10 THEN 'Cheap titles'
ELSE 'Expensive titles' END AS PriceRange,
COUNT(tblBooks.Title) AS Items
FROM tblBooks
GROUP BY PriceRange;
.. zou ik willen maar dat kan niet.
Wat wel werkt, maar omslachtig lijkt:
SELECT CASE
WHEN tblBooks.Price < 10 THEN 'Cheap titles'
ELSE 'Expensive titles' END AS PriceRange,
COUNT(tblBooks.Title) AS Items
FROM tblBooks
GROUP BY CASE WHEN tblBooks.Price < 10 THEN 'Cheap titles'
ELSE 'Expensive titles' END;
Response:
PriceRange Items
-------------------- --------
Cheap titles 45
Expensive titles 14
.. Het gewenste resultaat, maar ik moet het hele case statement in de group by herhalen! Kan dat niet handiger?