So I am creating a booking table. I have two drop down lists called Date and Time and the data for these is being retrieved from a table called "DateTime" with a field called Date with a load of dates in it and same with time.
This booking system is booking time and a date with a Doctor. I'm lost at this point in terms of how do I get times to only appear for a doctor that the user wants to choose an appointment with to avoid double bookings. If a user has booked 9:15am on a certain date, I don't want that time to appear for another user for that date and doctor.
UPDATE Once a user has selected their date time and doctor, this is stored in a table called Booking. So i was originally going to delete that time and date out of that DateTime but this will then not appear for another doctor if selected.
HELP PLEASE. :'(
At least you should share your own table design.
I think there will be two table.
One Master table (booking master table)
--date
--time
--isactive
Another one
doctor booking table
--date
--time
--doctorid
--patientid
--isActive (0,1)-- 0 in cae of cancel
So in GUI I select Doctor and Date,I pass this doctorid and date to my proc
It fetch me date,time etc from proc.which help me to populate calender.
I populate calender for each doctorid and date.
So I have written this on doctor and date change both.
Now while creating Calendar,if I get already book flag then I will show this time,but it will be disable.
My query will be like this
select bm.date,bm.time
,case when db.date=dbm.date then 1 else 0 end isBookedDate
from BookingMaster BM
left join DoctorBookingMapping DBM
on bm.date=dbm.date
where dbm.isactive=1
and db.isactive=1
and dbm.doctorid=#Doctorid
and db.date=#Date
You can do that with a query without deleting data from your DateTime table.
SELECT dt.[Date], dt.[Time], dt.Doctor
FROM dbo.[DateTime] dt
WHERE NOT EXISTS ( SELECT 1
FROM dbo.Booking b
WHERE
b.[Date] = dt.[Date]
AND b.[Time] = dt.[Time]
AND b.Doctor = dt.Doctor)
The query selects all the dates and times that does not currently exist in your booking table.
Hope this helps.
Marius
Related
I have a SQL query where I need to find out if any date in a table relating to a specific client is between another date in a different table and that date plus 5 days
I have a table with a list of client details (client table) with the fields clients name, address and ID (PK) in it
I have a table with the annual assessments details where we review the clients account (assessment table) with the fields assessment date and assessment ID (pk) in it
I a separate table with a list of the dates we have spoken with the client (meeting table) with the fields Meeting name (eg meeting type 1, meeting type 2, meeting type 3) meeting date, meeting ID (PK)
We can see the clients a lot of times during the year for different meetings, however I need a query that will tell me Yes or No, has the client been spoken to with 5 days of the annual assessment.
So I need something like the below
Select
Client_ID (PK) (from client table)
,Client_Name (from client table)
,Assessment_ID (PK) from assessment table
,Assessment_date (from assessment table)
Then here I need something here that will say, CASE WHEN from all of the dates in the meetings table relating to this client, are any of them within 5 days of the assessment date if so 'Y' if not 'N'
I'm fine with joining all the relevant table as and when required it's just the query for any date in the meeting table between assessment date and assessment date + 5 that I am struggling with.
Any help, greatly appreciated.
Something like this.
SELECT CustomerData
,CASE
WHEN DATEDIFF(DAY, MeetingDate, GETDATE()) < 5 THEN 'Yes'
ELSE 'No'
END AS CustomerConsultedLastFiveDays
FROM CustomerMeetingTable
WHERE Conditions = Conditions;
it's interesting to pay attention to some points:
If the Customers table has a relationship with the evaluation table, if so you can make an inner join between the two, finding what evaluations have for an X customer.
If you already have this relationship now, do an inner join, Exists or IN to validate if Client X exists in the Meeting table.
If it is not possible to make this relationship by PK, it is a problem regarding the structure of the data, since without reference to the customer in the meeting table, I did not find a way to make this relationship in your text.
Assuming you've done everything I mentioned above just make a CASE for your code
for example:
Note: I didn't quite understand the filter point related to the dates of the two tables, so I left the WHERE like this,
Graciously.
For a database still in design, asking for best way to design resources table. I want to calculate the labor cost of projects. I receive timesheet data and I maintain a resources table. If employees never got raises, I would determine the cost as:
SELECT t.proj_id,
t.person,
t.hours,
t.hours * r.rate AS cost
FROM timesheet t
INNER JOIN resources r ON t.person = r.person;
This gets complicated if Bob Smith gets a raise on June 1. Now I need to maintain the cost of Bob's old work at the old rate and his newer work at the new rate. What is the best way to design the resources table to make this work?
I believe the right way to do this is by adding start and end fields to the resource table and adding a second record for Bob. But what is the best way to make sure my query captures the correct rate for Bob?
You have to maintain a history table. For example your table
t_b_employee_rate_hist ( employee_id int,rate decimal(10,2),start_date date,end_Date date) last active record will have end_date null.
Then you read table with current rates.
merge into t_b_employee_rate_hist h
using(select employee_id,rate from current_rate_table) c
h.employee_id=c.employee_id and end_date is null and h.rate<>c.rate
when matched then update set h.end_date=current_date - 1 day
This will close active record then you have to insert new ones
insert into t_B_t_b_employee_rate_hist
select employee_id,rate,current date,cast(null as date)
from current_rate_table
where employee_id in
(select employee_id from t_B_t_b_employee_rate_hist
where end_date current_date - 1 day);
You can create this as job at that will run daily, date functions will vary according to database.
I have created a scheduling application for work and I have run into a problem which I've spent many days trying to figure out with no luck. The application pulls information from a database with a Patients table, Clinician table, and Appointments table.
Currently, when a user selects a Patient from a dropdown list in the application, a query is executed that returns all Clinicians which a user can pick to assign the Appointment to. The problem is that there are hundreds of Clinicians and Patients usually only see the same 3-4 Clinicians.
What I would like to do is create a query that pulls all Clinicians but displays those Clinicians who have had appointments with a specific Patient in the past FIRST and then lists every other Clinician afterwards.
Basically, the query needs to check the Appointments table for a specific PatientID, pull all the clinicians who have had appointments with this client in the past, and then under this list all clinicians who have not seen this patient.
Then best I have come up with looks like this but I don't really know what else to try at this point.
SELECT Clinician.att_id, Clinician.att_name,
Case When Clinician.att_id = Appointments.att_id THEN 1 ELSE 0 END as common
FROM Clinician inner join
Appointments
on Clinician.att_id = Appointments.att_id
order by common desc;
If you want all clinicians, then you want a left join or some other construct.
SELECT c.att_id, c.att_name
FROM Clinician
ORDER BY (CASE WHEN EXISTS (SELECT 1
FROM Appointments a
WHERE c.att_id = a.att_id AND
a.patient_id = #patient_id
)
THEN 1 ELSE 2
END)
I have two tables (simplified version):
create table Schedule(
Id int,
ScheduledData datetime,
UserId int)
create table User(
Id int,
Name varchar(50))
In the first table I store all scheduled meetings , linking it to an user.
What I want to is retrieve all the free times an user has. It doesn't have to be very detailed.
For example, if a user doesn't have a meeting scheduled for 07/02/2014 morning (earlier than 12:59:59), display a row with the user's name and date. The same if he has a free afternoon.
What I've tried so far and didn't work is to create a temporary table and fill it with all the days of the month and all the users in my DB. That worked well using a CTE:
create table #Temp(
StartData datetime,
EndDate datetim,
UserId int)
Then, I did this to display the rows:
select U.Name, X.ScheduledDate
from #Temp T
left outer join
(select S.UserId
from Schedules S
where ScheduledData between #X and #Y) X on T.UserId = S.UserId
left outer join User U on T.UserId = U.Id
where S.ScheduledDate between T.StartDate and T.EndDate
It didn't work well and I can't make sense of it. I've struggling with this all day and this is the best I've got so far.
The problem could use a little more specification:
What does a row in 'Schedule' mean? The 'ScheduledData' column is just one instant in time; is this the start time of the meeting? How long is the meeting?
What does it mean to display "all of a user's free times?" Time is a continuous (well, sort of) quantity. How should the result be represented?
The spirit of your example seems to be, given a block of time, return a true/false value on whether a meeting occurs in that time. Let's say you have a table called 'Users' containing each user's ID. How about creating a table 'time_segments' with three columns 'id', 'start_time' and 'end_time'; you could define any time segment you want for example your 'morning' block, and then fetch the free times with something like:
select UserId,all_slots.id
from
( select
TimeSegments.id, UserId
from
TimeSegments
cross join Users ) all_slots
left outer join
( select
TimeSegments.id, UserId
from
Schedule
cross join TimeSegments
where
Schedule.datetime between TimeSegments.start_time and TimeSegments.end_time
) booked using(id, UserId)
where
booked.UserID is null
I'm a little bit stumped as to how to do this. I want to select records from a table "agency" joined to a table "notes" on an id column that the two tables share.
Table structure:
create table notes (
notes_id varchar2(5),
agency_gp_id varchar2(5),
call_date date,
call_note varchar2(4000)
);
create table agency(
agency_id varchar2(5),
agency_name varchar2(5),
street varchar2(75),
city varchar2(50)
);
alter table notes add constraint "fk_group_notes_agency_id" foreign key(agency_gp_id)
references agency(agency_id) enable;
-Each table has auto-numbering, "before-insert" triggers so the id numbers stay in synch (along with other stuff in the case of adding a note to a newly created agency) - everything I need it to do (the databse), it does.
-Each record from the agency table has a distinct name/address combo (with different branches in different cities) and each record from the notes table has a date entry corresponding to each agency.
-Each agency can have multiple notes (multiple note details from subsequent visits)
What I am attempting to do is select each (distinct agency,street,city) that has not had a note added to it within the past four months.
This is the query I came up with:
SELECT count(a.agency_name) as number_of_visits,
a.agency_name,
(a.street||', '||a.city) as "Location",
n.call_date,
ROUND(TRUNC(sysdate - call_date)) AS days_since_visit
FROM notes n, agency a
WHERE (sysdate - n.call_date) > 120
AND n.agency_gp_id = a.agency_id
--AND a.city = 'München' --not necessary, used for limiting number of results
GROUP BY n.call_date,a.agency_name,a.street, a.city
ORDER BY a.agency_name ASC, n.call_date desc;
It kind of works...I can see what I want but I also see what I DO NOT want (e.g. the multiple notes on each agency). The only thing I want to see is the last entry (most recent, according to the WHERE clause) of each agency. The picture I want to create is: For whichever agency that has not been annotated within 120 days of the last note, display the address and name and the last note date.
(Instead of showing the number of days since EACH visit, I want to show the number of days that have past since the LAST visit - per distinct agency,street,city).
This is for an app that will help a sales executive schedule her sales calls and is run twice a week. I have been unable to figure this out. Also, bear in mind that the actual tables used are much more descriptive - what I have used here are only the parts I need to describe the question.
I would appreciate any suggestions on how to solve this problem.
Thanks!
If I understand your problem correctly, changing call_date to MAX(call_date) (and removing it from the GROUP BY statement) should get you what you want int terms of data, but would also pull in false positives, namely any agency that had notes older than 120 days, regardless of the most recent note. If we filter those agencies out in a NOT EXISTS subquery, that should get you where you need to go.
SELECT count(a.agency_name) as number_of_visits,
a.agency_name,
(a.street||', '||a.city) as "Location",
MAX(n.call_date),
ROUND(TRUNC(sysdate - MAX(call_date))) AS days_since_visit
FROM notes n, agency a
WHERE (sysdate - n.call_date) > 120
AND n.agency_gp_id = a.agency_id
AND NOT EXISTS (SELECT 1 FROM notes n2
WHERE n2.agency_gp_id = a.agency_id
AND (sysdate - n2.call_date) <= 120)
--AND a.city = 'München' --not necessary, used for limiting number of results
GROUP BY a.agency_name,a.street, a.city
ORDER BY a.agency_name ASC, MAX(n.call_date) desc;