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
event ID 39 - 40 -41- 42- 47 winmgmt
Java/J2EE class loading architecture
64-bit version of Acrobat PDF Add-in for Office 2010?
VS2008 - Help with preferences
easy transfer software xp to xp
Change primary smtp addresses for 29 users using powershell for exchange
xp sp2 clients wont log or ping server 2003
Critera Help for query in access if a field is null then ignore crireria
Doing an INSERT or Update based on if the record already exists or not while importing data from a text file to SQL SERVER 2005.
copy a defined user profile when user logs on domain for first time. Giving identical initial profile at logon. Each user can then modify this profile as required. over time keeping settings