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
can't change password through OWA 2010
Access VBA - best way to get field value based on ID
PL/SQL - how to dynamically retrieve next 5 years from sysdate
Access / how many users connected?
Should Sys Admins (Domain Admins) also have user accounts?
Microsoft Outlook 2007 is pulling duplicate emails from free hotmail live server - how can I prevent this, its driving me mad.
How do I restore my Exchange 2003 Virtual Directory Settings back to Default
Documents Folder redirection on server 2008 R2 remote desktop services in SBS 2003 domain
2GB of RAM laptop only recognizing 1GB
Copy Folder / Files From Corrupt Disk In Linux (Kubuntu)