I am currently working with this query
SELECT DISTINCT
se.src_aet AS "Ripped By",
CONVERT(varchar(32), s.created_time, 113) AS "Date/Time Sent",
p.pat_name AS "Patient Name",
p.pat_id AS "Patient ID",
s.accession_no AS "ACC #",
p.pat_birthdate AS "DOB",
s.mods_in_study AS "MOD",
CONVERT(varchar(32), s.study_datetime, 113) AS "Study Date",
s.study_desc AS "Study Desc",
s.study_custom1 AS "Inst Name"
FROM
dbo.patient AS p
INNER JOIN
dbo.study AS s ON s.patient_fk = p.pk
INNER JOIN
dbo.series AS se ON se.study_fk = s.pk
WHERE
accession_no like '%OUT%'
AND s.created_time >= CAST(getdate() as DATE)
AND s.created_time < CAST(DATEADD(day, 1, getdate()) as DATE)
ORDER BY
"Date/Time Sent" DESC
In the above example, it comes back with:
RIPPED BY DATE/TIME SENT PATIENT NAME PATIENT ID ACC# DOB
BC 18 Aug 2014 12:58:44:297 DOE^JOHN^^^ HOSP1234 OUT 19510101
I have taken the last few columns away (in the above example output) for simplicity sake here.
It is working correctly. It comes back with a list of studies that have the word 'OUT' in the accession_no column and that have been written to the DB 'today'.
What I am wanting to return are all of the unique values for 'pat_id' in the dbo.patient table for those found in the query. Some of those patients found the query above might already exist in the dbo.patient table if they have been to our site before, but they will have a different 'pat_id'.
What I want the output to look like is:
RIPPED BY DATE/TIME SENT PATIENT NAME PATIENT ID_OUT PATIENT ID_EXIST ACC# DOB STUDY DATE STUDY DESC INST NAME
the column 'PATIENT ID_OUT' is the patient ID that would be on the study that has the value of 'OUT' in the accession_no column and the column 'PATIENT ID_EXIST' is the existing patient ID, if it does exist. The value for 'PATIENT ID_EXIST' may be blank in some cases if the patient is new.
I was thinking I was needing to do a sub-query here, but maybe that is not the right thing to do in this case? Correct me if I am wrong.
EDIT: I have provided an example of the output. I am trying to figure out if the patient in the output has a different 'Patient ID' based on searching for the pat_name and pat_dob in the query results. If there is another or more than one patient ID in the pat_id column, then I would like them in the output.
I hope this is clear :)
I usually tackle tasks like this by treating the query as a table and joining to it.
SELECT p.*
FROM dbo.patient p
INNER JOIN (your big query) q
ON q.pat_id = p.pat_id
Related
In my query I am able to successfully extract the mass data, however it would be very helpful if I am able to drill down to only selecting the first project start date of the data set.
I've tried adding a (MIN) to my query but it errors out, any suggestions ?
For clarification, I am looking to select the first project start date on a particular contract. Any contract can have (n) # of projects. My goal is to detect the first of them.
Here is my query :
SELECT DISTINCT CONT.CONTRACTNUMBER as "Contract Number",
CS.PRODUCTID as "Product ID",
PROJ.PROJECTID as "Project ID",
PR.PRODDESCRIPTION as "Product Description",
PROJ.PROJECTNAME as "Project Name",
CS.STARTDT as "Contract Service/ Product Start DT",
CONT.CONTRACTStartDT as "Contract Start DT",
PROJ.PROJECTSTARTDT as "Project Start DT",
Datediff(DAY, CS.STARTDT, PROJ.PROJECTSTARTDT) as "# Day Difference"
FROM PRODUCTDATA.DBO.HHCONTRACTSERVICE CS WITH(NOLOCK)
LEFT OUTER JOIN COSTTRACKERDATA.DBO.OAPROJECT PROJ WITH(NOLOCK)
ON CS.DID = PROJ.EXTERNALID
LEFT OUTER JOIN PRODUCTDATA.DBO.HHCONTRACT CONT WITH(NOLOCK)
ON CS.CONTRACTDID = CONT.DID
LEFT OUTER JOIN FIGURES..DIMPRODUCT PR WITH(NOLOCK)
ON CS.PRODUCTID = PR.PRODUCTID
AND PR.SUBPRODUCTLINE IN ( 'S', 'S' )
WHERE CS.STARTDT BETWEEN '2020-01-01' AND '2021-11-11'
AND PROJ.PROJECTSTARTDT <> '1970-01-01 00:00:00.000'
order by [Contract Number], [Project Start DT]
First in the list or the earliest date in the list?
either way you can order your list the way you want and add limit 1 at the end. That will give you the first row only.
The LIMIT clause is used in the SELECT statement to constrain the number of rows to return.
https://www.mysqltutorial.org/mysql-limit.aspx
I am not new in access but not even pro. I really need help with this problem.
Key:
Ledger - Main Table;
Customer - Customer Table.
My Goal:
I am using access for accounting purpose, I want to show customer balance in daily accounting sheet.
What have I done
First I made a query (DailySheetQuery) out of Ledger and Customer which gives total of balances of each customer
Second I took Ledger again and DailySheetQuery to a new query (DailySheet).
Third I linked the customer foreign key in ledger to DailySheetQuery ID (which is customer ID originally) to link the balance in DailySheetQuery to the customer in ledger
Now the problem -
DailySheet (Query) doesn't show any error -
but when I bring it to report it shows this error "The specified field 'Ledger.ID' could refer to more than one table listed in the FROM clause of your SQL statement."
My Codes:
Query 1: DailySheetQuery
SELECT [Customer Database].ID, Ledger.Customer, Sum([Recable]-[Received]) AS Balance
FROM [Customer Database] RIGHT JOIN Ledger ON [Customer Database].ID = Ledger.Customer
GROUP BY [Customer Database].ID, Ledger.Customer;
Query 2: DailySheet
SELECT Ledger.ID, Ledger.[Transaction Type], Ledger.[Invoice Date], Ledger.Passenger, Ledger.Customer, Ledger.Employee, Ledger.Service, Ledger.Recable, Ledger.Received, Ledger.Payable, Ledger.Paid, Ledger.[Mode Of Payment], DailySheetQuery.ID, DailySheetQuery.Balance, IIf([Ledger].[Mode Of Payment]=2,[Ledger].[Received]-[Ledger].[Paid],0) AS CashInHand, [Ledger].[Recable]-[Ledger].[Payable] AS Profit, [Ledger].[Received]-[Ledger].[Paid] AS [Money]
FROM Ledger LEFT JOIN DailySheetQuery ON Ledger.Customer = DailySheetQuery.ID
WHERE (((Ledger.[Invoice Date])=Date()) AND ((Ledger.Employee)=[Employee #?]));
Please go to through my problem and send help.
P.S. I am new to forum question and answer, if there is something missing let me know.
You could alias Ledger in the query:
SELECT
L.ID, L.[Transaction Type], .. , [L].[Received]-[L].[Paid] AS [Money]
FROM
Ledger AS L
LEFT JOIN
DailySheetQuery ON L.Customer = DailySheetQuery.ID
WHERE
(((L.[Invoice Date])=Date()) AND ((L.Employee)=[Employee #?]));
I'm studying for my Database System exam tomorrow, and I'm working on an SQL question. This question is the only one from the paper that doesn't have an answer, but here's the question:
We use the following schema:
Professor(name, office, dept, age) (age is key)
Course(cno, title, dept) (cno stands for course number and is a key, title is the name of the course and dept is name of department offering the course)
Enrollment(cno, semester, inst_name, enrollment) (the key for this is (cno, semester)
Question: Write the following sql query: Output a table containing the single row "yes" in it if the age difference between the oldest and the youngest professors teaching the Database Systems course between 2000 and 2009 is at most 5 years
I amn't sure my approach is right, since we don't exactly want to output something from the table. Note that I think enrollment corresponds to when the instructor started teaching the course (which isn't the usual definition AFAIK).
My approach is as follows:
WITH dbsProfs AS (
SELECT P.age
FROM Professor P, Enroll E, Course C
WHERE P.name = E.inst_name AND C.cno = E.cno AND C.title = "Database Systems"
AND E.enrollment BETWEEN 2000 and 2009
)
SELECT "Yes"
FROM dbsProfs
WHERE MAX(dbsProfs.age) - MIN(dbsProfs.age) <= 5
I'm fairly confident with my temporary table. I'm doing a join on all 3 tables and filtering out to include only the ones relevant to my query. It's the other half I'm unsure about.
Any insight on whether this is correct/how to correct this would be much appreciated. I amn't convinced WHERE MAX(dbsProfs.age) - MIN(dbsProfs.age) <= 5 is valid SQL
With aggregate function you shoud use having
SELECT "Yes"
FROM dbsProfs
HAVING MAX(dbsProfs.age) - MIN(dbsProfs.age) <= 5
WITH dbsProfs AS (
SELECT MIN(P.age) as min_age,MAX(P.age) as max_age
FROM Professor P, Enroll E, Course C
WHERE P.name = E.inst_name AND C.cno = E.cno AND C.title = "Database Systems"
AND E.enrollment BETWEEN 2000 and 2009
)
SELECT CASE WHEN min_age<max_age THEN "Yes" END
FROM dbsProfs
It's true, you can't put aggregate functions in your WHERE statement. This question will take a bit more work.
This is the approach I would take:
SELECT CASE WHEN A.MaxAge - A.MinAge <=5 THEN "Yes" END
FROM
(
SELECT Max(Professor.age) AS MaxAge, Min(Professor.age) AS MinAge
FROM Professor INNER JOIN Enroll ON Professor.name = Enroll.inst_name
INNER JOIN Course ON Enroll.cno = Course.cno
WHERE Course.title = "Database Systems" AND Enroll.enrollment BETWEEN 2000 AND 2009
) AS A
This assumes enrollment is indeed the year the course was instructed (as you also assumed). You can use an iif statement if you're fond of SQL Server instead of the SELECT CASE (the latter being more t-SQL standards friendly).
Firstly i apologize for vague question title, i am not sure what to write there. Please let me know and i will update it.
Now here it goes..
I have 3
Table : target
Columns : target_id , target_employee_id, target_location_id, target_location_name, target_scheduled_on, target_exec_end_time, target_damaged, target_damaged_text
Table : employee
Columns : employee_id, employee_manager_id
Table : location
Columns : location_id, location_name
Little Explaination-> Managers role is to create a target for employee meaning a new target will have the location name and the scheduled on. Then employee will visit that location and report if there are any damages. This can happen multiple times in a month for same location.
What i need to show-> a list of targets having damages - between two dates (target_exec_end_time) and if there are multiple records of that same location then show the one having max date.
More Explaination-> There can be multiple entries of targets having same location but i need to show only one instance. target table image
I have tried my best to explain. Thanks in advance.
How about this:
select * from target join employee on target.target_employee_id = employee.employee_id
join location on target.target_location_name = location.location_name
where target_exec_end_time between <start_date> and <end_date>
and target_exec_end_time = (select max(target_exec_end_time) from location loc2
where loc2.location_name = target.location_name)
group by target.location_name;
Not sure about having both target_exec_end_time clauses in the where though. Maybe you just want the max value?
SELECT MAX(target_exec_end_time), *
FROM target
WHERE target_damaged=1
AND target_exec_end_time BETWEEN '2012-12-01' AND '2012-12-31'
GROUP BY target_location_id
select t.target_location_name,case when t.target_damaged >0
then t.target_damaged_text else 'Not Damaged' end target_damaged_text,
max(t.target_exec_end_time) from target t
inner join employee e on t.target_employee_id=e.employee_id
inner join location l on t.target_location_id=l.location_id
where t.target_exec_end_time between 'DATE1' and 'DATE2'
group by t.target_id,t.target_damaged_text
I have two tables in a mysql database. Memebers and ClassNumbers. Both tables have a paid year field and both have a code field. I want to update the paid year in the Members table with the values from the code numbers table Where code in members is the same as code in ClassNumbers.
I know this is probably nowhere near right but it might explain it a bit better.
UPDATE Members SET paidYear (THE VALUE OF PAID YEAR IN ClassNumbers) where cl_code == code
Members
id,
Paid year,
cl_code,
ClassNumbers
id,
paidyear,
code,
I came across this code when searching other answers I'm thinking it's something similar
UPDATE business b, people p
SET b.email = p.email
WHERE b.business_id = p.business_id
AND p.sort_order = '1'
AND b.email = ''
UPDATE Members JOIN ClassNumbers ON Members.cl_code = ClassNumbers.code
SET Members.paidyear = ClassNumbers.paidyear