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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:44:57+00:00 2026-05-27T19:44:57+00:00

I am having some real trouble with this, below is the code, I am

  • 0

I am having some real trouble with this, below is the code, I am try to get counts from two different tables then group them by date.

declare @fromdate as date
declare @todate as date
set @fromdate = CONVERT(date,GETDATE()-3,101)
set @todate = CONVERT(date,GETDATE(),101)

USE database
SELECT
    CONVERT(date,created_date,101) as OrdDate,
    col1=   (SELECT COUNT(distinct cust_id) 
        FROM table1 
        WHERE created_date BETWEEN @fromdate and @todate),
    col2=   (SELECT COUNT(distinct cust_id) 
        FROM table1 
        WHERE [status] LIKE 'P%' and created_date BETWEEN @fromdate and @todate),
    col3=   (SELECT COUNT(distinct cust_id) 
        FROM table2 
        WHERE created_date BETWEEN @fromdate and @todate),
    col4=   (SELECT COUNT(distinct cust_id) 
        FROM table2 
        WHERE created_date between @fromdate and @todate and result_error like 'FAIL%')

FROM table1
WHERE created_date BETWEEN @fromdate and @todate

GROUP BY CONVERT(date,created_date,101)
ORDER BY CONVERT(date,created_date,101)

It is counting all of the days together, I have tried adding Group by to each expression, no go. Any suggestions?

OrdDate     col1    col2    col3    col4
2011-12-19  5000    4000    3000    2000
2011-12-20  5000    4000    3000    2000
2011-12-21  5000    4000    3000    2000

What I want is something like this

OrdDate     col1    col2    col3    col4
2011-12-19  1500    1500    500     750
2011-12-20  2500    750     1000    1000
2011-12-21  1000    1750    1500    250
  • 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-27T19:44:57+00:00Added an answer on May 27, 2026 at 7:44 pm

    You have to generate a list of dates first, then JOIN back to that for each count.

    This will remove issues with different row counts for each aggregate

    SELECT
        base.TheDate AS OrdDare,
        ISNULL(T1.C1, 0) AS col1, 
        ISNULL(T2.C2, 0) AS col2, 
        ISNULL(T3.C3, 0) AS col3, 
        ISNULL(T4.C4, 0) AS col4
    FROM
        ( -- UNION will also DISTINCT
         SELECT CONVERT(date,created_date,101) AS TheDate FROM table1
         WHERE created_date BETWEEN @fromdate and @todate
         UNION
         SELECT CONVERT(date,created_date,101) FROM table2
         WHERE created_date BETWEEN @fromdate and @todate
        ) base
        LEFT JOIN
        (
         SELECT CONVERT(date,created_date,101) AS TheDate, COUNT(distinct cust_id) AS C1
            FROM table1 
            WHERE created_date BETWEEN @fromdate and @todate
            GROUP BY CONVERT(date,created_date,101)
        ) T1 ON base.TheDate = T1.TheDate 
        LEFT JOIN
        (
          SELECT CONVERT(date,created_date,101) AS TheDate, COUNT(distinct cust_id) AS C2
            FROM table1 
            WHERE WHERE [status] LIKE 'P%' and created_date BETWEEN @fromdate and @todate
            GROUP BY CONVERT(date,created_date,101)
        ) T2 ON base.TheDate = T2.TheDate 
        LEFT JOIN
        (
         SELECT CONVERT(date,created_date,101) AS TheDate, COUNT(distinct cust_id) AS C3
            FROM table2
            WHERE created_date BETWEEN @fromdate and @todate
            GROUP BY CONVERT(date,created_date,101)
        ) T3 ON base.TheDate = T3.TheDate 
        LEFT JOIN
        (
         SELECT CONVERT(date,created_date,101) AS TheDate, COUNT(distinct cust_id) AS C4
            FROM table2
            WHERE created_date BETWEEN @fromdate and @todate and result_error like 'FAIL%'
            GROUP BY CONVERT(date,created_date,101)
        ) T4 ON base.TheDate = T4.TheDate 
    ORDER BY
         base.TheDate;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having real trouble with this and I was wondering I could get
I'm having some real confusion about events in c#... if I have this code
I'm having some real trouble with timing a function from within an instance of
I'm having some trouble sending a file by ftp from my BizTalk 2006RC. The
I am having real trouble trying to deserialize some XML and was hoping someone
I'm playing with some code for a blackjack program but I'm having trouble finding
I'm having real trouble getting my head around this issue. As the title suggests,
I'm having some trouble pulling up relationed objects from my database using Doctrine2 in
First time cake user and I'm having real apache problems. For some reason the
Having some trouble with what should be a very simple scenario. For example purposes,

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.