473,411 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

booked days per month query

Hi - long time viewer, first time poster

i have a car booking system that stores the startdate and the enddate of a booking as well as car/customer details.
i want to produce an efficiency report that shows how many days the car is booked per month.

i am having trouble getting the results - the column that sums the days between the booking start and end date works ok if the start and enddate are in the the same month, but i cant figure out how to get it to work for bookings that last longer than a month

Expand|Select|Wrap|Line Numbers
  1. SELECT CC.reg, CBB.bookst, CBB.bookend, CC.carid, CBB.courtcarbookid,tran.trandesc, CC.model, case when month( CBB.bookst ) <> month( CBB.bookend )then concat(sum(datediff( makedate( month( CBB.bookend ), 1 ), CBB.bookst )+ 1)) else concat(sum(datediff( CBB.bookend, CBB.bookst )+ 1)) end as days, month(CBB.bookst) as month, count(cc.carid) as count FROM courtcar AS CC LEFT JOIN (SELECT CB.bookst, CB.bookend, CB.carid, CB.courtcarbookid, CB.cusid FROM courtcarbook AS CB WHERE (cb.bookst BETWEEN '20070101' AND '20071231') OR (cb.bookend BETWEEN '20070101' AND '20071231') OR (cb.bookst <= '20070101') AND (cb.bookend >= '20071231')) AS CBB ON CC.carid = CBB.carid LEFT JOIN tran ON CC.tranid = tran.tranid LEFT JOIN cus ON cus.cusid = CBB.cusid WHERE display = 0 GROUP BY CC.carid, month(CBB.bookst) ORDER BY CC.carid ASC, CC.makeid ASC, month(CBB.bookst) ASC 
Jul 5 '07 #1
7 1796
code green
1,726 Expert 1GB
Not sure why you are doing this
Expand|Select|Wrap|Line Numbers
  1. sum(datediff
DATEDIFF returns the difference in days between two dates. Make sure the fields are of type DATE or DATETIME
Jul 5 '07 #2
hi thanks for your reply -
the sum(datediff) is to provide a total of the number of days that are booked
for that month
i know how many days are in each month so i can work out a percentage

however, the sql doesnt take into account wether the booking stretches over two [or more] months so the results arent accurate, it would need to sum the days booked for the month, then if the rest of the booking is in another month
it would have to sum for the next month

if im not being clear please let me know

heres the sql again in a better format

Expand|Select|Wrap|Line Numbers
  1. SELECT CC.reg, CBB.bookst, CBB.bookend, CC.carid, CBB.courtcarbookid,tran.trandesc, CC.model, case when month( CBB.bookst ) <> month( CBB.bookend )then concat(sum(datediff( makedate( month( CBB.bookend ), 1 ), CBB.bookst )+ 1)) else concat(sum(datediff( CBB.bookend, CBB.bookst )+ 1)) end as days, 
  2. month(CBB.bookst) as month, count(cc.carid) as count 
  3. FROM courtcar AS CC 
  4. LEFT JOIN (SELECT CB.bookst, CB.bookend, CB.carid, CB.courtcarbookid, CB.cusid FROM courtcarbook 
  5. AS CB 
  6. WHERE (cb.bookst BETWEEN '20070101' AND '20071231') 
  7. OR (cb.bookend BETWEEN '20070101' AND '20071231') 
  8. OR (cb.bookst <= '20070101') AND (cb.bookend >= '20071231')) 
  9. AS CBB 
  10. ON CC.carid = CBB.carid 
  11. LEFT JOIN tran ON CC.tranid = tran.tranid 
  12. LEFT JOIN cus ON cus.cusid = CBB.cusid 
  13. WHERE display = 0 
  14. GROUP BY CC.carid, month(CBB.bookst) 
  15. ORDER BY CC.carid ASC, CC.makeid ASC, month(CBB.bookst) ASC
  16.  
Jul 5 '07 #3
...also the fields bookst and bookend are date format
Jul 5 '07 #4
code green
1,726 Expert 1GB
the sum(datediff) is to provide a total of the number of days that are booked
for that month
Please refer to my quote
DATEDIFF returns the difference in days between two dates
I think you only want the results for a particular month. But you still don't need sum. Can you change the DATEDIFF so date1 = a date in the record and date2 = date at end of or beginning of month?
Jul 5 '07 #5
hi - thanks for your reply, can you give me an example - i am new to this
Jul 6 '07 #6
code green
1,726 Expert 1GB
I really don't like using aliases unless absolutely neccesary, that is a table is being JOINed twice.
It makes your query difficult to read.
If your date data is in DATETIME format why are you using this?
Expand|Select|Wrap|Line Numbers
  1. concat(sum(datediff( makedate( month( CBB.bookend ), 1 ), CBB.bookst )+ 1)) 
I am sorry but I can't figure out the reasoning behind this.
Using the MONTH() function is casting the result to number of months.
And why MAKEDATE() when the fields are already in DATETIME format?
DATEDIFF(). This function takes two dates and returns the difference between the two in days.
The date can be as read from a record or text in the DB standard format . ie 2007-07-06. Please explain what is wrong with this?
Expand|Select|Wrap|Line Numbers
  1. DATEDIFF(CBB.bookst,CBB.bookend)
Jul 6 '07 #7
thanks for your reply - have changed the sql and your right it makes no difference to the result!
i am still stuck as what to do if the booking runs over a month because the query would need to keep totals for the next month as well as the current month

Expand|Select|Wrap|Line Numbers
  1. SELECT CC.reg, CBB.bookst, CBB.bookend, CC.carid, CBB.courtcarbookid,tran.trandesc, CC.model, 
  2. sum(datediff( CBB.bookend, CBB.bookst )+ 1) as days, 
  3. month(CBB.bookst) as month 
  4. FROM courtcar AS CC 
  5. LEFT JOIN (SELECT CB.bookst, CB.bookend, CB.carid, CB.courtcarbookid, CB.cusid 
  6. FROM courtcarbook AS CB WHERE (cb.bookst BETWEEN '20070101' AND '20071231') 
  7. OR (cb.bookend BETWEEN '20070101' AND '20071231') OR (cb.bookst <= '20070101') 
  8. AND (cb.bookend >= '20071231')) 
  9. AS CBB ON CC.carid = CBB.carid 
  10. LEFT JOIN tran ON CC.tranid = tran.tranid 
  11. LEFT JOIN cus ON cus.cusid = CBB.cusid 
  12. WHERE display = 0 
  13. GROUP BY CC.carid, month(CBB.bookst) 
  14. ORDER BY CC.carid ASC, CC.makeid ASC, month(CBB.bookst) ASC 
  15.  
Jul 6 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
2
by: David Mitchell | last post by:
I have tried using the following code to count the specific number of each weekday but get a compile error "User defined type not defined" which I think relates to the first line of the function: -...
3
by: Karen | last post by:
I need to run a query that will give me the average number of days it takes a cutomer to pay. I have done this in a select query by subtracting payment date from invoice date, then averaging,...
6
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns...
19
by: Ricardo Perez Lopez | last post by:
Hello everyone: I'm a PostgreSQL newbie, working now with dates, times, timestamps and intervals. I have three questions about the above: FIRST: --------
2
by: bufbec | last post by:
I have worked on this for hours and can't come up with a solution. Hope someone can help me. I have a table called TMBS_HMAUDIT_PARMS. this table contains data to tell me how often a person is...
9
by: simba | last post by:
Hi, I am doing an online booking project and was wondering if anyone has any ideas on how I can display rooms that are not booked on a certain date. The query that I have that displays all rooms...
1
by: Del | last post by:
I have a parameter query that requires the user to enter a Start Date: and End Date: and pull data between that date range. I am currently using the following parameter; Select * From mytable...
5
FishVal
by: FishVal | last post by:
IMHO, the following is not a how-to-do instruction to solve a particular problem but more a concept-proof stuff demonstrating possibilities of SQL. So, let us say the problem is to calculate...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.