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 7623395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:41:47+00:00 2026-05-31T04:41:47+00:00

Using SqlServer 2008, I have a temp table (#tmpAllSales) that captures all sales for

  • 0

Using SqlServer 2008, I have a temp table (#tmpAllSales) that captures all sales for a year. I would like to create a SELECT statement that contains sales for the year as well as sales for the last quarter.

Something like this (@StartDate & @EndDate are defined as the start/end of a quarter):

--QTD Sales By Company
SELECT
    Company,
    SUM(Call) as TotalCallsQTD,
    SUM(Call) - SUM(SoldCall) as FreeCallsQTD,
    SUM(SalableCall) as SalesRelatedCallsQTD,
    SUM(SoldCall) as SoldCallsQTD
FROM
    #tmpAllSales
WHERE
    CalledOn between @StartDate and @EndDate
GROUP BY
    Company

--COMBINED WITH BASED ON Company

--YTD Sales By Company
SELECT
    Company,
    SUM(Call) as TotalCallsYTD,
    SUM(Call) - SUM(SoldCall) as FreeCallsYTD,
    SUM(SalableCall) as SalesRelatedCallsYTD,
    SUM(SoldCall) as SoldCallsYTD
FROM
    #tmpAllSales
GROUP BY
    Company
ORDER BY
    Company

Each separate query gives the results I want but I don’t know how to combine into one result set.

EDIT

I didn’t make it clear in my original post that I would like to get all the results on one row per company. I am using this in an Excel macro so it would be great if all the info was on one row.

  • 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-05-31T04:41:48+00:00Added an answer on May 31, 2026 at 4:41 am
    SELECT
        Period = 'QTD',
        Company,
        SUM(Call) as TotalCallsQTD,
        SUM(Call) - SUM(SoldCall) as FreeCallsQTD,
        SUM(SalableCall) as SalesRelatedCallsQTD,
        SUM(SoldCall) as SoldCallsQTD
    FROM
        #tmpAllSales
    WHERE
        CalledOn between @StartDate and @EndDate
    GROUP BY
        Company
    UNION ALL
    SELECT
        Period = 'YTD',
        Company,
        SUM(Call) as TotalCallsYTD,
        SUM(Call) - SUM(SoldCall) as FreeCallsYTD,
        SUM(SalableCall) as SalesRelatedCallsYTD,
        SUM(SoldCall) as SoldCallsYTD
    FROM
        #tmpAllSales
    GROUP BY
        Company
    ORDER BY
        Period, Company;
    

    If you want them as a single row for each Company, then:

    ;WITH q AS
    (
        SELECT
            Company,
            SUM(Call) as TotalCallsQTD,
            SUM(Call) - SUM(SoldCall) as FreeCallsQTD,
            SUM(SalableCall) as SalesRelatedCallsQTD,
            SUM(SoldCall) as SoldCallsQTD
        FROM
            #tmpAllSales
        WHERE
            CalledOn between @StartDate and @EndDate
        GROUP BY
            Company
    ), y AS 
    (
        SELECT
            Company,
            SUM(Call) as TotalCallsYTD,
            SUM(Call) - SUM(SoldCall) as FreeCallsYTD,
            SUM(SalableCall) as SalesRelatedCallsYTD,
            SUM(SoldCall) as SoldCallsYTD
        FROM
            #tmpAllSales
        GROUP BY
            Company
    )
    SELECT q.Company, qTotalCallsYTD = q.TotalCallsYTD, etc...,
        yTotalCallsYTD = y.TotalCallsYTD, etc...
    FROM q INNER JOIN y
    ON q.Company = y.Company
    ORDER BY q.Company;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQLServer 2008. Say I have a recursive hierarchy table, SalesRegion, whit SalesRegionId
Using SQLServer 2008 I currently have a function that implements functionality as shown below
I'm using SQL Server 2008 and I have a table with three columns: Length
I have a database that I have created using SQL Server Developer 2008. I
I have developed a s/w using acces and sqlserver 2008 and now trying to
We are using SQL Server 2008 Enterprise version. We have a large table FooTable
Using SQL Server 2008, I have two stored procedures - create procedure [dbo].[SH_Export_data] (@unit
I am using JPA 2.0 and SQL Server 2008 and I have determined that
I'm using SQL server 2008 I have the following SQL getting all dates between
I have a table in SqlServer 2008 with data of the form UserID StartWeek

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.