Microsoft
Software
Hardware
Network
Question : Subquery count distinct rows + sum
In Table A1 I have the column MyNum(int). I would like to change this sproc so that instead of counting the distinct a1.a1Id I would like to find the sum of the column MyNum for all distinct a1.a1Id. How would I do that?
SELECT MyText, MyId,
NumCount=(SELECT COUNT(distinct a1.a1Id) FROM a1
JOIN a2 ON a1.a1Id=a2.A1Id
WHERE a2.TId=MyTable.TId)
FROM MyTable
Related Solutions:
Subquery count distinct rows
Answer : Subquery count distinct rows + sum
Hi johnkainn,
Try this and see if it accomplishes what you want.
SELECT MyText, MyId,
NumCount=(
SELECT SUM(a1.MyNum) FROM a1
WHERE a1.a1Id IN (
SELECT DISTINCT a1.a1Id FROM a1
JOIN a2 ON a1.a1Id=a2.a1Id
WHERE a2.TId=MyTable.TId
)
)
FROM MyTable
Random Solutions
How do I properly setup "Internet Address" and install SSL certificate in SBS 2008?
WCF hosted on IIS: Why doesn't it show me the WCF method name and it's signature?
Builidng a new computer
Gateway for Server automatically being removed after restart
Sync Access database contacts with Exchange Contacts list
store comma separated multiple numbers in a mysql database field database and query them efficiently
Windows 7 Group Policy Bug?
Oracle database password file
Need a 40" or larger touchscreen computer
Pervasive SQL 9.5 - odbc SELECT Query - DATE = '0000-00-00'