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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:58:49+00:00 2026-05-15T02:58:49+00:00

EDIT 2 I have two tables reports and holidays . reports : (username varchar(30),activity

  • 0

EDIT 2

I have two tables reports and holidays.

reports: (username varchar(30),activity varchar(30),hours int(3),report_date date)

holidays: (holiday_name varchar(30), holiday_date date)

select * from reports gives

+----------+-----------+---------+------------+  
| username |  activity |  hours  |   date     |
+----------+-----------+---------+------------+  
| prasoon  |   testing |    3    | 2009-01-01 |
| prasoon  |   coding  |    4    | 2009-01-03 |
| gautam   |   coding  |    1    | 2009-01-04 |  
| prasoon  |   coding  |    4    | 2009-01-06 |
| prasoon  |   coding  |    4    | 2009-01-10 |
| gautam   |   coding  |    4    | 2009-01-10 |
+----------+-----------+---------+------------+

select * from holidays gives

+--------------+---------------+  
| holiday_name |  holiday_date |
+--------------+---------------+ 
| Diwali       |   2009-01-02  |
| Holi         |   2009-01-05  |  
+--------------+---------------+

EDIT

When I used the following query

 SELECT dates.date AS date,
  CASE 
    WHEN holiday_name IS NULL THEN COALESCE(reports.activity, 'Absent') 
    WHEN holiday_name IS NOT NULL and reports.activity IS NOT NULL THEN  reports.activity
  ELSE ''
    END 
  AS activity,
  CASE WHEN holiday_name IS NULL THEN COALESCE(reports.hours, 'Absent')
    WHEN holiday_name IS NOT NULL and reports.hours IS NOT NULL THEN reports.hours
    ELSE ''
    END 
  AS hours,
  CASE 
    WHEN holiday_name IS NULL THEN COALESCE(holidays.holiday_name, '')
    ELSE holidays.holiday_name
    END 
  AS holiday_name
  FROM dates 
  LEFT OUTER JOIN reports ON dates.date = reports.date 
  LEFT OUTER JOIN holidays ON dates.date = holidays.holiday_date
  where reports.username='gautam' and dates.date>='2009-01-01' and dates.date<='2009-01-09';

I got the following output

   +----------+-----------+---------+------------+  
   |  date    |  activity |  hours  |   holiday  |
   +----------+-----------+---------+------------+  
   |2009-01-04|   coding  |    1    |            |
   +----------+-----------+---------+------------+

but I expected this

   +----------+-----------+---------+------------+  
   |  date    |  activity |  hours  |   holiday  |
   +----------+-----------+---------+------------+  
   |2009-01-01|  Absent   | Absent  |            |
   +----------+-----------+---------+------------+
   |2009-01-02|           |         | Diwali     |
   +----------+-----------+---------+------------+
   |2009-01-03|  Absent   | Absent  |            |
   +----------+-----------+---------+------------+
   |2009-01-04|  Coding   |   1     |            |
   +----------+-----------+---------+------------+
   |2009-01-05|           |         | Holi       |
   +----------+-----------+---------+------------+
   |2009-01-06|  Absent   | Absent  |            |
   +----------+-----------+---------+------------+
   |2009-01-07|  Absent   | Absent  |            |
   +----------+-----------+---------+------------+
   |2009-01-08|  Absent   | Absent  |            |
   +----------+-----------+---------+------------+
   |2009-01-09|  Absent   | Absent  |            |
   +----------+-----------+---------+------------+

How can I modify the above query to get the desired output(for a particular user (gautam in this case))?

  • 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-15T02:58:50+00:00Added an answer on May 15, 2026 at 2:58 am

    Update 2: Here’s the answer to your new question. Note that you were using the CASE statements incorrectly – COALESCE(reports.activity, 'Absent') will return reports.activity if it is not null, and 'Absent' if it is.

    First you need a table dates with the dates you want to check, like so:

    CREATE TABLE dates (date date);
    

    and then fill it manually for now:

    date      
    ----------
    2009-01-01
    2009-01-02
    2009-01-03
    2009-01-04
    2009-01-05
    2009-01-06
    2009-01-07
    2009-01-08
    2009-01-09
    2009-01-10
    

    This can be created procedurally, but that’s another topic entirely.

    Here’s the query, using left outer joins and a nested select:

    SELECT dates.date AS date,
    CASE WHEN holiday_name IS NULL THEN COALESCE(user_reports.activity, 'Absent')
     ELSE '' END AS activity,
    CASE WHEN holiday_name IS NULL THEN COALESCE(user_reports.hours, 'Absent')
     ELSE '' END AS hours,
    COALESCE(holidays.holiday_name, '') AS holiday_name
    FROM dates
    LEFT OUTER JOIN 
     (SELECT * FROM reports WHERE reports.username='guatam') AS user_reports
     ON dates.date = user_reports.report_date 
    LEFT OUTER JOIN holidays ON dates.date = holidays.holiday_date
    WHERE dates.date>='2009-01-01' and dates.date<='2009-01-09';
    

    Which returns:

    date        activity    hours       holiday_name
    ----------  ----------  ----------  ------------
    2009-01-01  Absent      Absent                  
    2009-01-02                          Diwali      
    2009-01-03  Absent      Absent                  
    2009-01-04  coding      1                       
    2009-01-05                          Holi        
    2009-01-06  Absent      Absent                  
    2009-01-07  Absent      Absent                  
    2009-01-08  Absent      Absent                  
    2009-01-09  Absent      Absent       
    

    But the real problem is that you are doing something that’s very poor practice – you’re using SQL to format your information. This is bad! You should be using it only to retrieve your information, and then format it with HTML or whatever you’re pulling the data into. Your select should be a simple:

    SELECT * FROM reports WHERE username='guatam'
     AND date>='2009-01-01' AND date<='2009-01-9' 
    

    And a separate one for the holidays, if you need it:

    SELECT * from holidays
    

    And then use that information as you need.

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

Sidebar

Related Questions

I need to solve the following question which i can't get to work by
I want to get the header of a selected tab-item of a tab-control and

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.