473,385 Members | 2,274 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,385 software developers and data experts.

How to structure query to obtain the needed data

20
Database consists of records of visitors who have stayed at various contracted locations. Have ADATE and DDATE. Need to isolate all those who have stayed any days in October. So, in some cases they may have begun their stay in September and left during October or they may have begun their stay in October and not yet left. I further, need to be able to count the number of days each visitor has stayed during October.

I cannot seem to get what I need. I have specified ADATE Between 10/01/2010 And 10/31/2010 or DDATE Between 10/01/2010 and 10/31/2010 and have done DateDiff to get days. However, DateDiff is including all days in the count, not just those in October and is not counting October days at all for those visitors who have not departed yet. Can you please help me with how to structure this query?

Thanks so much
cambar
Nov 5 '10 #1

✓ answered by NeoPa

OK. Adding one is pretty simple I would guess, but :
Cambar:
it will not provide the number of October days
doesn't tell me what it does do. Does it give an error? Zero maybe?

Anyway, try this for now :

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2.      , DateDiff('d',
  3.                 IIf([ADate] < #10/1/2010#, #10/1/2010#, [ADate]),
  4.                 IIf(Nz([DDate], Date()) > #10/30/2010#, #10/30/2010#, Nz([DDate], Date()))) + 1 AS OctDays
  5. FROM   [Table]
  6. WHERE  ([ADate] <= #10/30/2010#)
  7.   AND  (Nz([DDate], Date()) >= #10/1/2010#)

5 2084
NeoPa
32,556 Expert Mod 16PB
You probably want to look at something with a structure similar to :
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2.      , DateDiff('d',
  3.                 IIf([ADate] < #10/1/2010#, #10/1/2010#, [ADate]),
  4.                 IIf([DDate] > #10/30/2010#, #10/30/2010#, [DDate])) AS OctDays
  5. FROM   [Table]
  6. WHERE  ([ADate]<=#10/30/2010#)
  7.   AND  ([DDate]>=#10/1/2010#)
Nov 6 '10 #2
cambar
20
NeoPa response does not count October days if there is no DDATE. If the visitor arrived in late October and has not yet departed, it will not provide the number of October days. And if the visitor arrived and left the same day, the day count is 0 when it needs to be 1.
Nov 6 '10 #3
NeoPa
32,556 Expert Mod 16PB
OK. Adding one is pretty simple I would guess, but :
Cambar:
it will not provide the number of October days
doesn't tell me what it does do. Does it give an error? Zero maybe?

Anyway, try this for now :

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2.      , DateDiff('d',
  3.                 IIf([ADate] < #10/1/2010#, #10/1/2010#, [ADate]),
  4.                 IIf(Nz([DDate], Date()) > #10/30/2010#, #10/30/2010#, Nz([DDate], Date()))) + 1 AS OctDays
  5. FROM   [Table]
  6. WHERE  ([ADate] <= #10/30/2010#)
  7.   AND  (Nz([DDate], Date()) >= #10/1/2010#)
Nov 6 '10 #4
cambar
20
Thank you very much for your help. This works.
camgar
Nov 6 '10 #5
NeoPa
32,556 Expert Mod 16PB
I'm pleased that was helpful Cambar :-)
Nov 7 '10 #6

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

Similar topics

6
by: JT | last post by:
Hello all, I've been using sql and mysql in particular for several years now, but I'm having some trouble coming up with a query that will pull the data that I need from a mysql table without using...
0
by: Mike N. | last post by:
Hello to all: First let me apologize for the length of this question, I've made an attempt to include as much information as is needed to help with the question. I am having problems putting...
2
by: Adam Nemitoff | last post by:
I have two tables populated during the use of an application to log user events and application states. They are named "EventTable" and "StateTable" and the structures follow: EventTable: ID...
4
by: MLH | last post by:
I apologize in advance to forum readers noticing this somewhat redundant post. I fear that my Subject Heading was ill-chosen in earlier post I made on this topic. Am hoping that a clearer Subject...
5
by: Norma | last post by:
I am trying to make a query pull data from between the dates I enter in the parameter but also look back 'in time' to see where 2 other fields have null values, and only pull data into the query if...
1
by: JellyMan | last post by:
I am trying to do something with an Access SQL query that I have never done. I can see how it SHOULD work in my head, but can't seem to make it work in reality. I think I have it close, but my SQL...
1
by: Luqman | last post by:
I have created a Insert Query in Sql Data Source using Oracle Database, with the parameters, and its connected with DetailView Control. When I try to Insert through DetailView Control, Illegal...
14
by: hotadvice | last post by:
hi there i think this is an off topic question but i think u folks ,,,here know the best.... so .... case 1)
0
by: Alexandre Brisebois | last post by:
I have been trying to use Linq o SQL to query an XML data column I currently have in my DB, but it seems that Linq to SQL sees a typed xml column as an XElement, I don't want to get the full...
3
by: NatRoger | last post by:
Hi, We are using DB2 V7 (MVS) in our shop, which dont have the luxury of the 'WITH clause' - CTE support to accomplish recursive queries. Here is my problem - Table1 From-ID To-ID A1 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.