?
Solved

Select Count Subquery

Posted on 2011-09-12
3
Medium Priority
?
536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

765 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