Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:43:33+00:00 2026-06-05T14:43:33+00:00

Sorry if the title is confusing, I’ll try to explain. I have a query

  • 0

Sorry if the title is confusing, I’ll try to explain. I have a query that needs to have a couple of aggregate fields, but I don’t know how to write one of them. Here’s the current script:

DECLARE @Date DATETIME;
DECLARE @FromDate VARCHAR(25);
DECLARE @ToDate VARCHAR(25);

SET     @Date = GETDATE();
SET     @FromDate = CONVERT(VARCHAR(25), DATEADD(dd, -(DAY(@Date) - 1), @Date),101);
SET     @ToDate = CONVERT(VARCHAR(25), DATEADD(dd, -(DAY(DATEADD(mm, 1, @Date))), DATEADD(mm,1,@Date)), 101);

SELECT  DISTINCT    [A].[Name],
                    COUNT([O].[Id]) AS [OpportunityCount],
                    (CASE WHEN (SUM([O].[Amount]) IS NULL) THEN 0.00 ELSE SUM([O].[Amount]) END) AS [OpportunityTotalAmounts]
                    --(SELECT COUNT(1) FROM [Opportunity] AS [O1] WHERE ([O1].[Id] = [O].[Id]) AND ([O1].[StageName] != 'Not Sold'))
FROM                [Account] AS [A]
                    JOIN [RecordType] AS [RT] ON ([A].[RecordTypeId] = [RT].[Id])
                    JOIN [Contact] AS [C] ON ([A].[Id] = [C].[AccountId])
                    JOIN [OpportunityContactRole] AS [OCR] ON ([C].[Id] = [OCR].[ContactId])
                    LEFT OUTER JOIN [Opportunity] AS [O] ON ([OCR].[OpportunityId] = [O].[Id])
WHERE               ([RT].[Name] = 'PHX')
                    AND ([A].[TypeMS__c] != 'Customer')
                    AND ([O].[CreatedDate] BETWEEN @FromDate AND @ToDate)
GROUP BY            [A].[Name];

You’ll notice a commented out line. On that line I want to get a count of all Opportunities that are not marked as 'Not Sold'. It works if I add [O].[Id] to the GROUP BY, but then the result set expands because the grouping gets messed up.

How can I get the count I want?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:43:34+00:00Added an answer on June 5, 2026 at 2:43 pm
    DECLARE @Date DATETIME;
    DECLARE @FromDate VARCHAR(25);
    DECLARE @ToDate VARCHAR(25);
    
    SET     @Date = GETDATE();
    SET     @FromDate = CONVERT(VARCHAR(25), DATEADD(dd, -(DAY(@Date) - 1), @Date),101);
    SET     @ToDate = CONVERT(VARCHAR(25), DATEADD(dd, -(DAY(DATEADD(mm, 1, @Date))), DATEADD(mm,1,@Date)), 101);
    
    SELECT  DISTINCT    [A].[Name],
                        COUNT([O].[Id]) AS [OpportunityCount],
                        (CASE WHEN (SUM([O].[Amount]) IS NULL) THEN 0.00 ELSE SUM([O].[Amount]) END) AS [OpportunityTotalAmounts],
                        SUM(CASE WHEN [O].[StageName] != 'Not Sold' THEN 1 ELSE 0 END) 
    FROM                [Account] AS [A]
                        JOIN [RecordType] AS [RT] ON ([A].[RecordTypeId] = [RT].[Id])
                        JOIN [Contact] AS [C] ON ([A].[Id] = [C].[AccountId])
                        JOIN [OpportunityContactRole] AS [OCR] ON ([C].[Id] = [OCR].[ContactId])
                        LEFT OUTER JOIN [Opportunity] AS [O] ON ([OCR].[OpportunityId] = [O].[Id])
    WHERE               ([RT].[Name] = 'PHX')
                        AND ([A].[TypeMS__c] != 'Customer')
                        AND ([O].[CreatedDate] BETWEEN @FromDate AND @ToDate)
    GROUP BY            [A].[Name];
    

    This is the trick, you already defined a join so you just need to Sum the StageName differents from Not Sold using a CASE statement

     SUM(CASE WHEN [O].[StageName] != 'Not Sold' THEN 1 ELSE 0 END) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if the title is confusing. Here is the query I have Select MONTH(DATE(TIMESTAMP)),
I'm sorry for the little confusing title but say I have a class with
Sorry about that confusing title :) I have a resource, ComatosePage (used in the
Sorry if my title is confusing. I have an assignment due that has to
Sorry if the title sounds confusing - but this is what I am trying
sorry for the confusing title, its really hard for me to explain what i
Sorry for the confusing title, but it basically says it all. Here's the structures
Sorry if the question title is confusing. Let me explain further. I am building
I'm sorry if the title is quite confusing but I am wondering if it's
Sorry for the confusing title. Basically I made a BinaryReader class that reads in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.