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: - SELECT Wards.Name, Wards.[Number Bed], Count(Patients.[Pat-id]) AS [CountOfPat-id]
-
FROM Wards INNER JOIN (Patients INNER JOIN Admissions ON Patients.[Pat-id] = Admissions.[Pat-id]) ON Wards.[Ward-id] = Admissions.[Ward-id]
-
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: - SELECT Wards.Name, Wards.[Number Bed], Count(Patients.[Pat-id]) AS [CountOfPat-id], Admissions.[Start Date], Admissions.[End Date]
-
FROM Wards INNER JOIN (Patients INNER JOIN Admissions ON Patients.[Pat-id] = Admissions.[Pat-id]) ON Wards.[Ward-id] = Admissions.[Ward-id]
-
GROUP BY Wards.Name, Wards.[Number Bed], Admissions.[Start Date], Admissions.[End Date]
-
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 overcomplicatin g things for the pupils.
I hope this makes sense and thanks for the help.
Cheers
Guy
3 1869 Rabbit 12,516
Recognized Expert Moderator MVP
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.
MMcCarthy 14,534
Recognized Expert Moderator MVP
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 ... -
SELECT Wards.Name, Wards.[Number Bed], Count(Patients.[Pat-id]) AS [CountOfPat-id]
-
FROM Wards INNER JOIN (Patients INNER JOIN Admissions
-
ON Patients.[Pat-id] = Admissions.[Pat-id])
-
ON Wards.[Ward-id] = Admissions.[Ward-id]
-
WHERE Now() BETWEEN Admissions.[Start Date]
-
AND Admissions.[End Date]
-
GROUP BY Wards.Name, Wards.[Number Bed];
-
NeoPa 32,577
Recognized Expert Moderator MVP
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 only way to
find out the current state of a particular combination of attributes is
to "select distinct on (id, ...) ... order by date desc".
In the examples below, I've taken real output from psql and done a
global search/replace on various...
|
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.
Function DateCalc (blah...) As Variant
Do Stuff...
If Not IsNull(varNewDate) Then
DateCalc = varNewDate
End If
End Function
|
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 want
to look at that data and filter it based on what is in it.
I know that this could have been done with data sets and data views in
asp.net 1.1 but how is this done now in asp.net 2.0?
|
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 the combo box. The fields I am working with are date fields that are formated as Short Date. I have written an IIF statement and placed it within the Criteria section of the Date field that I am trying to filter by. The current IIF statement works...
|
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 CONTROLS)
CHAPTER 7 - CODE DISCUSSION (THE REST)
--------------------------------------------------------------------------------
| |
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 works OK when I'm using the Date Filter form with two unbound boxes which ask for the start and end dates. The query dynaset appears correctly.
I now want to introduce a form (lets call it Results) between the Date filter form (above) and the...
|
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 fields I can set in access to text or date which ever is needed. Atm I ran a sql query not like "*2013" which give me every thing before that year. I was wondering in the criteria box in access in expiry date field how to do this. I would like to be...
|
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, 10/19/2008 Date From
10/20/2008 Date To
17:00 Start Time
7:00 End Time
(How do I get all of the results in between ?)
The results...
|
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 recently i formated the date values so they appear as May 2009 instead of 12 May 2009. When i tried to use the filtering query again it did not work.
Instead the result set from the query only showed records from the two dates in the parameter, ...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |