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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:02:37+00:00 2026-05-29T19:02:37+00:00

I have 3 columns doc_date , chem_date , st_date all 3 columns belong to

  • 0

I have 3 columns doc_date, chem_date, st_date

all 3 columns belong to different tables:

doc
doc_date      count(x)
01-02-2012      2
02-02-2012      3

chem
chem_date      count(x)
04-02-2012      1
06-02-2012      0

stock
st_date      count(x)
01-02-2012      1
03-02-2012      5

I want to write select clause like this

case doc_date 
    when '01' then count(x),
    case chem_date
    when '01' then count(x),
    case st_date 
    when '01' then count(x)
    end as '01',
case doc_date 
    when '02' then count(x),
    case chem_date
    when '02' then count(x),
    case st_date 
    when '02' then count(x)
    end as '02',

....up to 31 days

If some case statements have an entry on same date e.g if doc_date and st_date both exist on ’01-02-2012′ then their respective count should be added to the final count(x) means count(x) + count(x)

So, the answer should be:

01  02  03  04  05  06  07  08  09 10  11  12.. up to 31 days
3   3   5   1       0                          count(x) value for particular date

Explanation of the output: Here, the starting value for date 01 is 2 for doc table and for stock table the value is 1. So, the final value will become addition of them which is 3

The same rule will be applicable for others.

Any suggestions how can I achieve this output in SQL? Or any other way?

  • 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-29T19:02:39+00:00Added an answer on May 29, 2026 at 7:02 pm

    Here’s a pseudo code showing how it can be done:

    select
    
    case when date_format(doc_date,"%d") = '01' then sum_x else null end as '01',
    case when date_format(doc_date,"%d") = '02' then sum_x else null end as '02',
    --and etc
    
    from (
    
        select doc_date, sum(doc_x) as sum_x              --sum the x value
        from
        (
    
            select doc_date,doc_x                         --combine all 3 table stuff
            from doc a
    
            union all
    
            select chem_date,chem_x
            from chem a
    
            union all
    
            select st_date,st_x
            from st a
        ) h
        group by doc_date               --use first col name as the union's col name
    
    ) g
    limit 1,1       
    

    If the doc, chem and st are combined using joins and have different schema, you may need to do pivot, either using multiple join to itself or use case... when...:

    select
    
    case when date_format(doc_date,"%d") = '01' then sum_x else null end as '01',
    case when date_format(doc_date,"%d") = '02' then sum_x else null end as '02',
    --and etc
    
    from (
    
            select doc_date,                               
                   a_x + b_x + c_x as sum_x            --sum them according to date
            from (select ... from doc where ...) a
            left join (select ... from chem where ...) b on a.doc_date=b.doc_date and --some join condition
            left join (select ... from st where ...) c on a.doc_date=c.doc_date and --some join condition
    
    ) g
    limit 1,1       
    

    I am working with an ambiguous question here, so clarify as needed,

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

Sidebar

Related Questions

I have 3 tables with same columns, I need to get count of rows
I'm using MySQL, all my tables are using InnoDB engine. I have some columns
When I have three divs that all have float left I want the sections
I have two tables: tweets (a.o. containing column tweet_id) and references (containing columns tweet_id
I'm running Oracle 10g and have columns with Type_Name TIMESTAMP(6) WITH TIME ZONE When
I have 62 columns in a table under SQL 2005 and LINQ to SQL
I have three columns in my table - company_name, first_name, last_name . In the
I have 12 columns separated by a tab. How can I join them side-by-side?
I have two columns in Excel, like this: A B 0.5 0.4 0.6 0.59
I have two columns. ColA and ColB contains char(10) with data 20090520 and 20090521.

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.