Solved

Select Count Subquery

Posted on 2011-09-12
3
522 Views
Last Modified: 2012-05-12
I need to get the [Opportunity Count] field to return a count based on the SELECT Count(*) subquery written.  

See code below and look for the [Opportunity Count] column.
/*
Type Codes
Account - 1
Appointmenet - 4201
Opportunity - 3
*/

SELECT  Subject,
        RegardingObjectIdName,
        Description,
        CreatedByName,
        ScheduledEnd,
        ActualEnd,
        CreatedOn,
        RegardingObjectTypeCode,
        [Regarding]=CASE WHEN RegardingObjectTypeCode='1' THEN 'Account' WHEN RegardingObjectTypeCode='4201' THEN 'Appointment' WHEN RegardingObjectTypeCode='3' THEN 'Opportunity' ELSE 'n/a' END, 
        [Opportunity Count]= SELECT COUNT(*) FROM dbo.Opportunity o WHERE StateCode='0' AND o.AccountId=RegardingObjectId
FROM    dbo.Appointment 
ORDER BY ScheduledEnd desc

Open in new window

0
Comment
Question by:r270ba
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 36523726
Please try the following:

/*
Type Codes
Account - 1
Appointmenet - 4201
Opportunity - 3
*/

SELECT  Subject,
        RegardingObjectIdName,
        Description,
        CreatedByName,
        ScheduledEnd,
        ActualEnd,
        CreatedOn,
        RegardingObjectTypeCode,
        CASE WHEN RegardingObjectTypeCode='1' THEN 'Account' WHEN RegardingObjectTypeCode='4201' THEN 'Appointment' WHEN RegardingObjectTypeCode='3' THEN 'Opportunity' ELSE 'n/a' END As [Regarding], 
        (SELECT COUNT(*) FROM dbo.Opportunity o WHERE StateCode='0' AND o.AccountId=RegardingObjectId) As [Opportunity Count]
FROM    dbo.Appointment 
ORDER BY ScheduledEnd desc

Open in new window

0
 
LVL 42

Expert Comment

by:dqmq
ID: 36523733
You need parens around the select:


        [Opportunity Count]= (SELECT COUNT(*) FROM dbo.Opportunity o WHERE StateCode='0' AND o.AccountId=dbo.Appointment.RegardingObjectId)
0
 
LVL 42

Expert Comment

by:dqmq
ID: 36523770
Or, performance-wise, better yet,

SELECT  Subject,
        RegardingObjectIdName,
        Description,
        CreatedByName,
        ScheduledEnd,
        ActualEnd,
        CreatedOn,
        RegardingObjectTypeCode,
        [Regarding]=CASE WHEN RegardingObjectTypeCode='1' THEN 'Account' WHEN RegardingObjectTypeCode='4201' THEN 'Appointment' WHEN RegardingObjectTypeCode='3' THEN 'Opportunity' ELSE 'n/a' END,
[Opportunity Count]
FROM    dbo.Appointment inner join
  (Select AccountID, COUNT(*) as [opportunity Count] FROM dbo.Opportunity
 WHERE StateCode='0'
  group by AccountID
  ) as O
on o.AccountId=RegardingObjectId
ORDER BY ScheduledEnd desc
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now