3

I have one requirement and i am totally confused how to write the query. I am a fresher in this company so please help me

User will select the following Dates:

Ex: fromDate: 2013-08-02 toDate: 2013-09-03 (YYYY-MM-DD)

id  resort_id   room_id Date1       Date2       price
5   35      45          2013-11-01  2013-11-30  3000.00
6   35      50          2013-07-25  2013-08-25  2000.00
7   35      541         2013-07-25  2013-08-25  4000.00
8   35      541         2013-08-26  2013-09-26  4000.00

Now i should get price based result for each date or sum of the price for both the date group by room id

Expected result

id  resort_id   room_id     Date1       Date2       price
6   35      50              2013-07-25  2013-08-25  2000.00
7   35      541             2013-07-25  2013-08-25  4000.00
8   35      541             2013-08-26  2013-09-26  4000.00

OR

id  resort_id      room_id  price
6   35             50       2000.00
7   35             541      8000.00
11
  • can you pls explain what is Date1 and Date2? Commented Aug 7, 2013 at 11:01
  • this IS a complicated query Commented Aug 7, 2013 at 11:03
  • 1
    @bew I think those are the date ranges for that price Commented Aug 7, 2013 at 11:04
  • Date1 and Date 2 similar to from_date and to_date. price should be shown in the result if the user provided date between date1 and date2 Commented Aug 7, 2013 at 11:04
  • @SomeshMukherjee yes you are correct Commented Aug 7, 2013 at 11:05

6 Answers 6

3
SELECT * FROM lr_price_peak_rates  WHERE  `from_date`>='2014-04-08' AND `to_date`<='2014-04-30' 

try the above query it can be working fine

Sign up to request clarification or add additional context in comments.

Comments

2

Try this query

SELECT * FROM `rooms` WHERE "2013-08-02" between Date1 AND Date2 UNION 
SELECT * FROM `rooms` WHERE "2013-09-03" BETWEEN Date1 AND Date2

Its a single query. Spread it on two lines for easier readability. I am pretty sure this query is missing something. I'll need a larger test set to verify. This won't select the middle cases. Change the first date with your user's selected dates.

3 Comments

@SomeshMukerjee This query works fine and i am getting first expected result.
Myself written one query and i am getting 2nd expected result. SELECT resort_id, room_id, SUM( test_price.price ) FROM test_price WHERE '2013-08-02' BETWEEN from_date AND to_date OR '2013-09-03' BETWEEN from_date AND to_date GROUP BY room_id
careful though this might not work if the date falls between january 2013 and October 2013. It won't select the rooms in between, but you should be able to build from here
1

Try this query

SELECT
  id,
  resort_id,
  room_id,
  Date1,
  Date2,
  SUM(price) AS Total_Price
FROM
  MyTable
WHERE Date1> '2013-02-08' AND Date2 < '2013-02-09'

2 Comments

@SomeshMukherjee Thank you for your comment. Crescent Can you provide me interval based select query?
@SomeshMukherjee i have provided yyyy-mm-dd format. Thanks in advance
1

hey try this (sql server )

SELECT room_id,SUM(price) FROM @table WHERE date1>='07/25/2013' AND date2<='09/26/2013' GROUP BY room_id

1 Comment

This is working fine with your given date. But user may select any date range.
0
select * from table_name where date1 > '08/02/2013' AND date2 < '09/02/2013' ORDER BY date1 ASC, price ASC

1 Comment

user dates should be between date1 and date2. for ex: date: 08/02/2013 is applicable with rowid: 6 and 7, date: 09/02/2013 is applicable with row id 8. Using your query i am getting 0 results.
0

SELECT * FROM rooms WHERE date between Date1 AND Date2 UNION SELECT * FROM rooms WHERE date BETWEEN Date1 AND Date2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.