logical calculation within Access Query - ms-access

I have a Access DB table that have the following data.
Room-Bed
Status
100-A
Available
100-B
Occupied
101-A
Available
101-B
Available
102-A
Occupied
102-B
Occupied
Having the room logic calculation as follows
In one bed, there are two beds, if any one bed is occupied, the room status is occupied.
If both beds are not occupied, the room status is available
If both beds are occupied, the room status is occupied
Likewise, is there are rooms with two beds or more, the room status is occupied as long as one of the bed is occupied.
Is there a way to design a query such that it will computed a room-level result as follows
Room
Status
100
Occupied
101
Available
102
Occupied

Try this:
Select
CStr(Val([Room-Bed])) As Room,
Max([Status]) As RoomStatus
From
Rooms
Group By
CStr(Val([Room-Bed]))
Result:
Room
RoomStatus
100
Occupied
101
Available
102
Occupied

As far as I see it, the logic can be simplified to the following rule:
For each room, if any of the beds are occupied, the room is occupied.
Otherwise the room is available.
The logic below is to count, for each room, the number of beds occupied (done below by giving an occupied bed a value of 1, otherwise it's 0 - then take the SUM).
Note that the below assumes you have a way to make a table room_bed_list with the columns room, room-bed, and room-bed-status. This is similar to your original table, but adds a room column - I'm assuming you already have a link from room-bed to this in another table.
Room
Room-Bed
Room-Bed-Status
100
100-A
Available
100
100-B
Occupied
101
101-A
Available
101
101-B
Available
102
102-A
Occupied
102
102-B
Occupied
SELECT room_bed_status.room,
IIF(Sum(IIf([room_bed_list].[room-bed-status] = "Occupied",1,0)) > 0, "Occupied", "Available") AS room_status
FROM room_bed_list
GROUP BY room_bed_status.room;

Related

Master table update returning output in 2 rows instead of 1

I have written a query to return tax differences for a particular tax between what was collected at the sale and what was billed.
The Tax master table is updated whenever there is an update to the tax.
The query comprises of 3 union queries:
Tax calculated but not billed
Tax calculated and billed
Tax not calculated but is billed
In one of the cases, which falls under the second union the query is returning 2 records for one tax and the reason is that the tax was updated after it was billed. So, when the tax was billed it was processed under tax sequence 1 and later due to the tax update, the new tax sequence has become 2.
TAX TABLE
The 2 union query joint is a full outer join and as it finds 2 tax sequences it is returning 2 records which is incorrect.
I get 2 record lines, one with billed tax and another one for calculated whereas I expected only 1 record which can then calculate the difference between billed and calculated
Incorrect Output
Expected Output

Subtracting Totals in SSRS

I have a report with a grouping of Category which each present their own totals lines. I would like to subtract the total of one group from the total of a separate group.
Group Clients Revenue ATC
Called 1000 50000 50.00
Control 100 1000 10.00
Here is what I want to do:
Variance 900 49000 40.00
Keep in mind that the called and control are already set as a grouping and there is underlying data that can be expanded to show each store's data.
Any suggestions would be helpful.
Thanks,
Scott
If you want to get the variance I assume you only have two groups.
Try this for Clients:
=Sum(IIF(Fields!Group.Value="Called",Fields!Clients.Value,0))
-
Sum(IIF(Fields!Group.Value="Control",Fields!Clients.Value,0))
This for Revenue:
=Sum(IIF(Fields!Group.Value="Called",Fields!Revenue.Value,0))
-
Sum(IIF(Fields!Group.Value="Control",Fields!Revenue.Value,0))
And this for ATC:
=Sum(IIF(Fields!Group.Value="Called",Fields!ATC.Value,0))
-
Sum(IIF(Fields!Group.Value="Control",Fields!ATC.Value,0))
Let me know if this helps.

Access Calculated Field

I am having difficulty trying to make a calculated field that I need. So here is what I am trying to do:
I have a query that combines the information based on three tables. The most important fields that for the application are as follows:
Family Income Age Patient
15,000 18 Yes
28,000 25 No
30,000 1 Yes
From here I want to make a calculated field that gives the correct program the patient was enrolled in. based on these fields ie:
Program Minimum Income Maximum Income Minimum Age Maximum Age Patient
Children's 0 20,000 1 19 Yes
Adult 0 12,000 19 65 No
Non Patient 0 20,000 1 19 No
Adult 2 12,000 50,000 19 65 No
Etc.
to create:
Family Income Age Patient Program
15,000 18 Yes Children's
28,000 25 No Adult 2
30,000 1 Yes Children's 2
I know I can use IIf to hard code it in to the field, but then it will be really difficult for other people to update the information as the guidelines change. Is it possible to have the information stored in a table? and use the information on the table form etc, or will I need to use IIf
Any Ideas? is it possible to dynamically create the IIf in SQL using VBA while pulling the information from the table?
EDIT:::
Thank you for your response and for formatting my tables, I still have no idea how you changed it, but it looks amazing!
I tried to add the SQL you added down below, but I was not able to make it work. I'm not sure if I made a mistake so I included the SQL of my Query. The query currently returns 0 values, so I think I messed something up. (The real Query is embarassing...I'm sorry for that). Unfortunately, I have done everything in my power to avoid SQL, and now I am paying the price.
SELECT qry_CombinedIndividual.qry_PrimaryApplicant.[Application Date],
qry_CombinedIndividual.qry_PrimaryApplicant.[Eligibility Rep],
qry_CombinedIndividual.qry_PrimaryApplicant.Name,
qry_CombinedIndividual.qry_PrimaryApplicant.Clinic,
qry_CombinedIndividual.qry_PrimaryApplicant.Outreach,
qry_CombinedIndividual.qry_PrimaryApplicant.[Content Type ID],
qry_CombinedIndividual.qry_PrimaryApplicant.[Application Status],
qry_CombinedIndividual.qry_PrimaryApplicant.Renewal,
qry_CombinedIndividual.qry_Enrolled.EthnicityEnr,
qry_CombinedIndividual.qry_Enrolled.GenderEnr, qry_CombinedIndividual.AgeAtApp,
qry_CombinedIndividual.[Percent FPL], tbl_ChildrensMedical.MinPercentFPL,
tbl_ChildrensMedical.MaxPercentFPL, tbl_ChildrensMedical.MinAge,
tbl_ChildrensMedical.MaxAge, tbl_ChildrensMedical.Program
FROM qry_CombinedIndividual
INNER JOIN tbl_ChildrensMedical ON qry_CombinedIndividual.qry_Enrolled.Patient = tbl_ChildrensMedical.Patient
WHERE (((qry_CombinedIndividual.AgeAtApp)>=[tbl_ChildrensMedical].[MinAge]
And (qry_CombinedIndividual.AgeAtApp)<[tbl_ChildrensMedical].[MinAge])
AND ((qry_CombinedIndividual.[Percent FPL])>=[tbl_ChildrensMedical].[MinPercentFPL]
And (qry_CombinedIndividual.[Percent FPL])<[tbl_ChildrensMedical].[MaxPercentFPL]));
Also there are many different programs. Here is the real Children's Table (eventually I would like to add adults if possible)
*Note the actual table uses FPL (which takes family size into account, but is used the same as income). I am again at a total loss as to how you formated the table.
Program Patient MinPercentFPL MaxPercentFPL MinAge MaxAge
SCHIP (No Premium) No 0 210 1 19
SCHIP (Tier 1) No 210 260 1 19
SCHIP (Tier 2) No 260 312 1 19
Newborn No 0 300 0 1
Newborn (Patient) Yes 0 300 0 1
Children's Medical Yes 0 200 1 19
CHIP (20 Premium) Yes 200 250 1 19
CHIP (30 Premium) Yes 250 300 1 19
Do I have the correct implementation for the table I have? Or should I be changing something. I can also send more information/sample data if that would help.
Thank you again!
I just created some tables with your sample data and used the following SQL. Your 3rd 'patient' doesn't match any of the ranges (Age 1, Income $30K)
SELECT tblPatient.PatName, tblPatient.FamInc, tblPatient.Age, tblPatient.Patient,
tblPatientRange.Program, tblPatientRange.MinInc, tblPatientRange.MaxInc, tblPatientRange.MinAge,
tblPatientRange.MaxAge, tblPatientRange.Patient
FROM tblPatient INNER JOIN tblPatientRange ON tblPatient.Patient = tblPatientRange.Patient
WHERE (((tblPatient.FamInc)>=[tblPatientRange]![MinInc] And (tblPatient.FamInc)<=[tblPatientRange]![MaxInc])
AND ((tblPatient.Age)>=[tblPatientRange]![MinAge] And (tblPatient.Age)<=[tblPatientRange]![MaxAge]));

Database design price changes for every day

I got a table (tableA) with an auto current_timestamp column.
All entries will be counted and at the moment multiplied by a constant to get the price.
Lets say the count is 1000 and the constant is 2.00. That makes a price of 1000 * 2.00 = 2$
Now i want to change this constant whenever I need to.
I probably have to create a table (tableB) with the values
date pricefactor
2014-09-01 0.002
2014-09-20 0.001
2014-10-01 0.003
When i request now the price for all of the entries in tableA i want to get the value by multiplying all entries times the pricefactor in tableB.
So i would maybe got a count of 100 till the 20th, 1000 till the 1st of october and 200 after the 1st of october.
That would make:
100 * 0.002 = 0.200$
1000 * 0.001 = 1.000$
200 * 0.003 = 0.600$
---------------------
total = 1.800$
Is this thought ok till yet? or is there something i should change?
I just want to do it right so I ask for your opinion ;)
Add one more column named count to table B. You can write a trigger for Table A. Whenever there is a change in table A (add or delete), increment/decrement the count in table B for the row containing the time-stamp. Finally multiply count*respective price from table B and aggregate everything.
The best idea I would suggest is to create an api to retrieve live price.

Summing Values in Visible Rows Only

I have a report with one group of which the visibility is toggled based on if the [total] is greater than 5. I want to make another row, that adds up only the visible information from each column. The first column is the owner name, The 3rd column [total] gets divided by the second column to get the 4th [avg] column. Right now it's counting everything, including the hidden rows.
Also, columns 2-4 are calculated based on expressions
Example of what I want it to look like
Owner Count Total AVG
Bob 3 12 4
Jane 1 9 9
Marcos 2 24 12
TOTAL: 6 45 7.5
I also need to count the visible Owners total, and divide that by the total amount of active owners, which I pull from a second dataset.
Example if we had 12 owners total
Total listed owners: 3
% of owners: 25%
For the total displayed owners, just SUM on the expression you use to hide the row:
=SUM(IIF(Fields!Total.Value > 5, 1, 0))
then to get the percent, divide this by the COUNT of the total rows, including the hidden ones (which you already have).