Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Subquery - get # of active accounts

Author  Topic 

tiss0183
Starting Member

18 Posts

Posted - 2008-01-22 : 15:33:38
I don't understand why this subquery doesn't work. If I replace the subquery with a View it works. I am trying to determine the number of "active accounts" in a group of transactions during December. What am I missing?


SELECT salesrun_id, Count(account_id) FROM
(SELECT salesrun_id, account_id FROM Trades t
WHERE t.date > '2007-12-01'
GROUP BY t.salesrun_id, t.account_id)

Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'.

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-01-22 : 15:41:30
Try adding an alias to the subquery and then complete your aggregate with a final group by clause:

SELECT salesrun_id, Count(account_id) FROM
(SELECT salesrun_id, account_id FROM Trades t
WHERE t.date > '2007-12-01'
GROUP BY t.salesrun_id, t.account_id) a
GROUP BY salesrun_id
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-22 : 23:51:58
quote:
Originally posted by tiss0183

I don't understand why this subquery doesn't work. If I replace the subquery with a View it works. I am trying to determine the number of "active accounts" in a group of transactions during December. What am I missing?


SELECT salesrun_id, Count(account_id) FROM
(SELECT salesrun_id, account_id FROM Trades t
WHERE t.date > '2007-12-01'
GROUP BY t.salesrun_id, t.account_id)a
GROUP BY a.salesrun_id

Go to Top of Page
   

- Advertisement -