473,386 Members | 1,793 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,386 software developers and data experts.

Filtering my results by date

Hi,
I have an exercise I need to give to my pupils (I'm a teacher!) and I am trying to get a query working preferably using the query design view, without having to edit the SQL. The query involves three tables; Admission, Ward & Patient. The query is to initially return a list of all the wards with the total number of patients on each one. This is fine, using the following SQL:

Expand|Select|Wrap|Line Numbers
  1. SELECT Wards.Name, Wards.[Number Bed], Count(Patients.[Pat-id]) AS [CountOfPat-id]
  2. FROM Wards INNER JOIN (Patients INNER JOIN Admissions ON Patients.[Pat-id] = Admissions.[Pat-id]) ON Wards.[Ward-id] = Admissions.[Ward-id]
  3. GROUP BY Wards.Name, Wards.[Number Bed];
The next step is to report only on patients in wards on that day. I have added to the two admissions date fields and set criteria thus:

Expand|Select|Wrap|Line Numbers
  1. SELECT Wards.Name, Wards.[Number Bed], Count(Patients.[Pat-id]) AS [CountOfPat-id], Admissions.[Start Date], Admissions.[End Date]
  2. FROM Wards INNER JOIN (Patients INNER JOIN Admissions ON Patients.[Pat-id] = Admissions.[Pat-id]) ON Wards.[Ward-id] = Admissions.[Ward-id]
  3. GROUP BY Wards.Name, Wards.[Number Bed], Admissions.[Start Date], Admissions.[End Date]
  4. HAVING (((Admissions.[Start Date])<Now()) AND ((Admissions.[End Date])>Now()));
Now, the results wnat to also group by the dates, so it doesn't return a single record for each ward, but multiple records for each ward depending on the dates.

Can anyone clear up how I can change this in design view to only return one record per ward regardless of the dates.

The end result will be to present this in a report. I have toyed with the idea of filtering the report using the initial query, but wnat to avoid overcomplicating things for the pupils.

I hope this makes sense and thanks for the help.

Cheers
Guy
Feb 7 '07 #1
3 1855
Rabbit
12,516 Expert Mod 8TB
For the dates you might want to be inclusive of the current day so use <= and >= instead.

As for the multiple records for wards. Do you need to show the date? If you don't show the date it should collapse the records. Grouping by date will show a record for every unique date.
Feb 7 '07 #2
MMcCarthy
14,534 Expert Mod 8TB
As Rabbit says ...

As long as you include the date in the query you can't return only one record per ward. You can retain Admissions in the join so as to relate the tables but just not return any fields from this table. Your HAVING statement would be better as a WHERE with a BETWEEN criteria.

See the following ...

Expand|Select|Wrap|Line Numbers
  1. SELECT Wards.Name, Wards.[Number Bed], Count(Patients.[Pat-id]) AS [CountOfPat-id]
  2. FROM Wards INNER JOIN (Patients INNER JOIN Admissions 
  3. ON Patients.[Pat-id] = Admissions.[Pat-id]) 
  4. ON Wards.[Ward-id] = Admissions.[Ward-id]
  5. WHERE Now() BETWEEN Admissions.[Start Date] 
  6. AND Admissions.[End Date]
  7. GROUP BY Wards.Name, Wards.[Number Bed];
  8.  
Feb 8 '07 #3
NeoPa
32,556 Expert Mod 16PB
I agree with everything that Mary says, but I would add that one should never use the Now() function to compare against dates in this fashion. The Date() function returns just the Date portion of the current time so will not cause problems when comparing with Date-only items in your database.

To see how this is reflected (and therefore how to achieve the same result) in design view, simply paste this SQL into a query and change the view to Design-View. Regard how the WHERE clause is handled (Notice the difference between the WHERE here and the HAVING of your previous query).

Let us know how this works for you :)
Feb 8 '07 #4

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

Similar topics

1
by: Alex Satrapa | last post by:
I have a table from which I'm trying to extract certain information. For historical reasons, we archive every action on a particular thing ('thing' is identified, funnily enough, by 'id'). So the...
1
by: Ken | last post by:
I wrote a function to use in queries that takes a date and adds or subtracts a certain length time and then returns the new value. There are times when my function needs to return Null values. ...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
8
by: Ragbrai | last post by:
Howdy All, I have a query that is used for filtering results to be used in a combo box. The query needs to test fields from both a table and then unbound text boxes on the form that also contains...
2
NeoPa
by: NeoPa | last post by:
CHAPTER 1 - TABLE OF CONTENTS (Including attached database) CHAPTER 2 - INTRODUCTION CHAPTER 3 - TABLE LAYOUT CHAPTER 4 - FORM LAYOUT CHAPTER 5 - FORM MODULE CHAPTER 6 - CODE DISCUSSION (FILTER...
4
by: roryok | last post by:
Hi, (Access 2002 & Windows XP Pro) I have a Select query and one of the columns has the following criteria: (from SQL view) HAVING (((order.orderDate)>=!! And (order.orderDate)<=!!)); This...
2
by: Big X | last post by:
Hi, I am having a little trouble with some data I have been sent. Seems they want me to remove all the date that have a 5 year expiry date. I receive the data in csv file so the properties of the...
2
by: poteatc | last post by:
I want to filter my reports according date and time. I am already successful with filtering dates and times, but not overnight. I want to filter according to a shift that starts for example,...
3
by: zandiT | last post by:
Hello I have an access report and im using a query to filter the report using Date parameters eg Start Date-12 May 2009 and End Date-30 September 2009. the query works perfectly. My problem is...
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: 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...
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...

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.