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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:44:55+00:00 2026-05-29T23:44:55+00:00

I have a table tbl_charge id hotel_id start_date end_date charge_per_day ( in $) 1

  • 0

I have a table

tbl_charge

id hotel_id start_date  end_date    charge_per_day ( in $)  
1   6       2012-02-15  2010-02-15      20  
2   6       2012-02-16  2010-02-18      30  
4   6       2012-02-20  2010-02-25      50  

Note: if any date is not in the table then we set 25$ for each days (i.e. default charge)

now if someone wants to book a hotel from 2012-02-15 to 2012-02-22 , then I want to calculate the total charges for dates

Date   : 15+16+17+18+19+20+21+22
Charge : 20+30+30+30+25+50+50+50 = 285$

what i have done so far:

this query returns all rows successfully

SELECT * FROM `tbl_charge` WHERE 
start_date BETWEEN '2012-02-15' AND '2012-02-22' OR
end_date BETWEEN '2012-02-15' AND '2012-02-22' OR
( start_date <'2012-02-15' AND end_date > '2012-02-22')
HAVING property_id=6 

it returns all necessary rows but how do I sum the charges??

  • is ther any way to count days between given date range like last row is 20 -25 but i want only upto 22 then it return 3 days and we multiply charges by 3
  • is it good to create procedure for this or use simple query
  • 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-29T23:44:58+00:00Added an answer on May 29, 2026 at 11:44 pm

    I think this will do the trick:

    select sum(DayDifference * charge_per_day) + 
            (RealDayDifference - sum(DayDifference)) * 25 as TotalPerPeriod
    from (
      select charge_per_day, datediff(
          least(end_date, '2012-02-22'),
          greatest(start_date, '2012-02-15')) + 1 as DayDifference,
          datediff('2012-02-22', '2012-02-15') + 1 as RealDayDifference
      from t1
      where
        ((start_date between '2012-02-15' and '2012-02-22') or
        (end_date between '2012-02-15' and '2012-02-22') or
        (start_date < '2012-02-15' and end_date > '2012-02-22'))
        and hotel_id=6
    ) S1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data on a table tbl Admission : Date and Time
I have a table tbl_a, with ID as a primary key, with columnns GROUP_ID,DATE,LENGTH,VALID,CID
i have a table (tbl_world) which look like this id | first_name | last_name
I have a table that contains intervals: CREATE TABLE tbl ( user_id: INTEGER, start:
I have a table named tbl_Subjects_Taken which lists all the subjects taken by the
I have a table called tbl_loans which has the field loan_number . Loan numbers
I have a table in SQL Server 2008 Table Name : tbl_device Table Structure:
I have NH mapping public class TblContentMap : ClassMap<TblContent> { public TblContentMap() { Table(tbl_content);
I have a table with images and tags in it (called tbl_images ) image_file
Say I have this <table border=1 id=tbl> <tr> <td><input type=checkbox name=vehicle value=Bike /></td> <td>row

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.