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

  • Home
  • SEARCH
  • 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 5963341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:14:14+00:00 2026-05-22T19:14:14+00:00

Currently I am using MS Sql2000, though we are discussing upgrading this to 2005

  • 0

Currently I am using MS Sql2000, though we are discussing upgrading this to 2005 (if that affects anything, Im guessing that it is pretty standard SQL that I need)

One of our products Tracks Sales from various departments around the country.
Currently, I run 3 Almost identical Stored Procedures in Query Analyzer, and then copy the results into excel, and spend an hour manipulating it all onto one spreadsheet.

What I’d like to do is to come up with a solution for combining all my stored procedures so that I end up with a single, combined Dataset I can just drop into excel and mail to the interested parties.

The solution I started to come up with involved a temp table, where I would first pull in all the sites, and then in turn update this with the results from each of the queries I currently run independently, but I got into all kinds of trouble, so I figured better to start again, and to ask for guidance and help from the off.

SQL is kind of paraphrased so please dont pick me up on simple syntax errors 🙂

A much simplified set of data would be;
table site {
id, location}

table salesEnquiry {siteId, custoemrId, 
DepositDate, BalancePaidDate, Status} 
--Status can be (0,1,2,3 for not started, deposit, completed,cancelled

And from that, I am thinking along the lines of;

--Set up the Whole Table
declare @tmp TABLE (id, Site, Deposits, Completed, Cancelled) 

insert into @tmp (id, site)
select id, site from site

update @tmp
set Deposits = (select count(customerId) from salesEnquiry
    where DepositPaidDate >= @startOfMonth and DepositPaidDate <= @endOfMonth and status = 1)  

** This is the point I get lost I think, as Im not sure how to relate total number of deposits taken against the tmpId's as not all departments will take a deposit each month.

I would then repeat the above methodology for Completed and Cancelled Customers so I have a final table like below.

SiteId    Location    TotalDeposits    TotalCompleted    TotalCancelled    
  1          a             3               0                  1
  2          b             0               0                  0
  3          c             1               17                 0

edited to add sample data + Outcome.
** Sorry, cant figure the formatting **

Ok, hope this helps.
site
id location
1 a
2 b
3 c

salesEnquiry
id   custId   deposit      complete     status
1    1        10/05/2011   null         1
2    2        11/04/2011   11/05/2011   2
1    3        12/05/2011   null         1
1    4        13/05/2011   13/05/2011   2
3    5        14/05/2011   null         3
3    6        13/02/2011   13/05/2011   3

would give

SiteId Location Deposits Completed Cancelled
1        a       2          1        0
2        b       0          1        0
3        c       0          0        2
  • 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-22T19:14:15+00:00Added an answer on May 22, 2026 at 7:14 pm

    I’m not sure what the “and so on” indicates, but up until that point the following should work:

    SELECT
        S.id AS SiteId,
        S.site AS Location,
        SUM(CASE WHEN SE.status = 1 THEN 1 ELSE 0 END) AS TotalDeposits,
        SUM(CASE WHEN SE.status = 2 THEN 1 ELSE 0 END) AS TotalCompleted,
        SUM(CASE WHEN SE.status = 3 THEN 1 ELSE 0 END) AS TotalCanceled
    FROM
        Sites S
    LEFT OUTER JOIN SalesEnquiry SE ON
        SE.customer_id = S.id AND
        (
            (
                SE.status IN (1, 3) AND
                SE.DepositPaidDate >= @startOfMonth AND
                SE.DepositPaidDate <= @endOfMonth
            ) OR
            (
                SE.status IN (2, 3) AND
                SE.BalancePaidDate >= @startOfMonth AND
                SE.BalancePaidDate <= @endOfMonth
            )
        )
    GROUP BY
        S.id,
        S.site
    ORDER BY
        S.id
    

    I had to guess a bit on the JOIN condition. You’re subquery in your question doesn’t seem to be relating the Site and SalesEnquiry tables. Was it really giving you the correct numbers? Join them on whichever column is relevant. I guessed the customer_id and id.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently using Windows Server 2008 Standard and have several Hyper V machines.
I'm currently using DPack as this adds a Collapse All Projects option to the
We are currently using a somewhat complicated deployment setup that involves a remote SVN
I'm currently using SQL 2000 (it's a vendor server - don't ask...), and I'm
currently using jq 1.4.2 And i have gone through this forum and other forums
I am currently using Watir with Firefox and it seems that when I try
Currently using System.Web.UI.WebControls.FileUpload wrapped in our own control. We have licenses for Telerik. I
Im currently using vs2008 with asp.net mvc framework for web development. Im missing a
I'm currently using VS2005 Profesional and .NET 2.0, and since our project is rather
I am currently using the following command to upload my site content: scp -r

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.