How do I simply hard code multiple rows in a subselect?
I know I could do (How to select several hardcoded SQL rows?):
SELECT x.id, SUM(ISNULL(OtherTable.count_column,0))
FROM (SELECT 12 AS id
UNION
SELECT 21 AS id
UNION
SELECT 101 AS id
/*AND so on */
) AS x
LEFT JOIN OtherTable ON x.id = OtherTable.id
Group BY x.id
Is there a less awkward and verbose way to do this?
What I am really looking for is:
SELECT id, SUM(ISNULL(count_column,0)) FROM OtherTable
WHERE id IN (12, 21, 101,/*And So On*/)
GROUP BY id
In this case it does not include the sum of 0 for ids that do not exist. Is there a way to include the id's that were not found?
I noticed PIVOT
for SQL Server, but I am not sure if that makes it simpler/less verbose.
I guess I am just asking is there a better way?