I am trying to join multiple tables and the below query works well.
select
e.ENCNTR_ID e_id
, p.PERSON_ID p_id
, e.REG_DT_TM admitted
from
episode e
, patient p
where (e.reg_dt_tm > '01/JAN/2018'
and e.reg_dt_tm < '02/JAN/2018'
and e.active_ind = 1
and e.encntr_type_cd = 123.00
and e.ACTIVE_STATUS_CD = 1234
and e.person_id = p.person_id)
But when I change it and add more tables it gives me the error
"SQL Command not properly ended"
I need to add conditions on the first table(episode) as otherwise the query runs very slow.
select
e.ENCNTR_ID e_id
, p.PERSON_ID p_id
, e.REG_DT_TM admitted
, ce.EVENT_ID event_id
from
ENCOUNTER e
, person p
where (e.reg_dt_tm > '01/JAN/2018'
and e.reg_dt_tm < '02/JAN/2018'
and e.active_ind = 1
and e.encntr_type_cd = 7113.00
and e.ACTIVE_STATUS_CD = 22223
and e.person_id = p.person_id)
left join CLINICAL_EVENT ce on ce.ENCNTR_ID = e.ENCNTR_ID
and ce.EVENT_CD in (1235764
,22161234
)
and ce.valid_until_dt_tm > sysdate
left join CE_BLOB cb on ce.EVENT_ID = cb.EVENT_ID
and cb.valid_until_dt_tm > sysdate
order by e.REG_DT_TM, ce.PERFORMED_DT_TM, ce.CLINICAL_EVENT_ID
The query should look like this:
select e.ENCNTR_ID as e_id, p.PERSON_ID as p_id, e.REG_DT_TM as admitted, ce.EVENT_ID as event_id
from ENCOUNTER e join
person p
on e.person_id = p.person_id left join
CLINICAL_EVENT ce
on ce.ENCNTR_ID = e.ENCNTR_ID and
ce.EVENT_CD in (1235764, 22161234) and
ce.valid_until_dt_tm > sysdate left join
CE_BLOB cb
on ce.EVENT_ID = cb.EVENT_ID and
cb.valid_until_dt_tm > sysdate
where e.reg_dt_tm > date '2018-01-01' and
e.reg_dt_tm < date '2018-01-02' and
e.active_ind = 1 and
e.encntr_type_cd = 7113.00 and
e.ACTIVE_STATUS_CD = 22223
order by e.REG_DT_TM, ce.PERFORMED_DT_TM, ce.CLINICAL_EVENT_ID;
Notes:
Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
The WHERE clause goes after the FROM clause.
JOIN is an operator in the FROM clause, so all JOINs need to be before the WHERE.
Use the keyword DATE for date constants.
Related
Is their a way to order this by the Time column? I am not sure how to do this. The time is the schedule and I just need it to go from the morning to the evening.
Can I just nest another select statement and use that?
Thank you.
SELECT
DoseLevel,
LastName,
FirstName,
DOB,
EMPLID,
Time,
(
SELECT v.ColorCode
FROM ABCDocumentation cd1
LEFT JOIN ABCDocumentation cd2ON cd1.ABCDocumentationID = cd2.PairID
LEFT JOIN Medicine v ON v.MedicineID = cd1.MedicineID
LEFT JOIN Manufacturers mfg ON v.MFG_Seq = mfg.MFG_Seq
WHERE cd2.ABCDocumentationID = dt.ABCDocumentationID
) AS ParentColorCode,
(
SELECT mfg.Description
FROM ABCDocumentation cd1
LEFT JOIN ABCDocumentation cd2 ON cd1.ABCDocumentationID = cd2.PairID
LEFT JOIN Medicine v ON v.MedicineID = cd1.MedicineID
LEFT JOIN Manufacturers mfg ON v.MFG_Seq = mfg.MFG_Seq
WHERE cd2.ABCDocumentationID = dt.ABCDocumentationID
) AS ParentManuDesc
FROM
(
SELECT
cd.DoseLevel,
e.LastName,
e.FirstName,
e.DOB,
cvse.EMPLID,
cvse.AdminScheduleSlotsEmployeeID,
cd.ABCDocumentationID,
cvss.Time,
cd.ModifyDate AS 'StartTime'
FROM ABCAdminSchedule cvs
LEFT JOIN ABCAdminScheduleSlots cvss ON cvs.AdminScheduleID = cvss.AdminScheduleID
LEFT JOIN ABCAdminScheduleSlotsEmployee cvse ON cvss.AdminScheduleSlotsID = cvse.AdminScheduleSlotsID
LEFT JOIN ABCDocumentation cd ON cvse.AdminScheduleSlotsEmployeeID = cd.AdminScheduleSlotsEmployeeID
LEFT JOIN Employee e ON cvse.EmplID = e.EMPLID
WHERE CAST(TIME AS Date) = CAST(GETDATE() AS Date) AND CampusID = '06'
AND cvse.AdminScheduleSlotsEmployeeID IS NOT NULL
) dt
First off, there is no need for the derived table dt, as you are not doing any further processing.
Secondly, you can combine the two correlated subqueries into one with an APPLY.
Thirdly, conversions on columns can cause performance issues, so you can change the date check to a half-open interval, converting just GETDATE().
Finally you can add at the end an ORDER BY clause to sort.
SELECT
cd.DoseLevel,
e.LastName,
e.FirstName,
e.DOB,
cvse.EMPLID,
cvss.Time,
Parent.ColorCode,
Parent.Description
FROM ABCAdminSchedule cvs
LEFT JOIN ABCAdminScheduleSlots cvss ON cvs.AdminScheduleID = cvss.AdminScheduleID
LEFT JOIN ABCAdminScheduleSlotsEmployee cvse ON cvss.AdminScheduleSlotsID = cvse.AdminScheduleSlotsID
LEFT JOIN ABCDocumentation cd ON cvse.AdminScheduleSlotsEmployeeID = cd.AdminScheduleSlotsEmployeeID
LEFT JOIN Employee e ON cvse.EmplID = e.EMPLID
OUTER APPLY
(
SELECT v.ColorCode, mfg.Description
FROM ABCDocumentation cd1
LEFT JOIN ABCDocumentation cd2ON cd1.ABCDocumentationID = cd2.PairID
LEFT JOIN Medicine v ON v.MedicineID = cd1.MedicineID
LEFT JOIN Manufacturers mfg ON v.MFG_Seq = mfg.MFG_Seq
WHERE cd2.ABCDocumentationID = dt.ABCDocumentationID
) AS Parent
WHERE TIME >= CAST(GETDATE() AS Date) AND TIME < CAST(DATEADD(day, 1, GETDATE()) AS DATE)
AND CampusID = '06'
AND cvse.AdminScheduleSlotsEmployeeID IS NOT NULL
ORDER BY TIME
please
declare #tm table (id int identity, timee time(7))
insert into #tm (timee) values ('01:05:45'),
('10:15:18'),
('14:18:59'),
('09:15:10'),
('18:19:21'),
('21:05:17')
this is a default
select * from #tm order by id
this is a, what do you need
select tm.*,
iif(tm.part_time = 1, 'morning', 'evening') m_e from (select
case
when timee between '09:00:00' and '19:00:00' then 1
else 2 end part_time,
*
from #tm) tm
order by part_time, timee
Is there a way to multiply an aggregate function? I need to multiply SUM(ma.gewight) for every article (an article is, for example, H114972 which is iron and is 32,1 meters) so the GROUP BY groups all same articles and for every different article I need to multiply a different number (the column that I am using to multiply is e.best_wert which is the meters, mentioned above). So basically I need to multiply the SUM(ma.gewicht) with e.best_wert - but it doesn't work.
PS. Gewicht = weight
PSS. e.best_wert = weight value/meters
SELECT m.artikel, COUNT(ei.bestell_po) bestell_po_menge, SUM(ma.gewicht) AS summe_gewicht--, SUM(e.best_wert*ma.gewicht) AS summe_kg
FROM MATV030 m
INNER JOIN EINV030 e ON e.BESTELL_NR = m.BESTELL_NR
INNER JOIN EINV035 ei ON e.bestell_nr = ei.bestell_nr
INNER JOIN MATV010 ma ON m.ARTIKEL = ma.ARTIKEL
WHERE e.lieferant = '6000176' AND m.menge_buch <> 0
--AND m.artikel = 'H114972'
AND m.bs = 'WE' AND m.budatum >= '20190101' AND m.budatum < '20190201'
GROUP BY m.artikel, ma.gewicht
ORDER BY m.artikel ASC
Try like this
SELECT m.artikel, COUNT(ei.bestell_po) bestell_po_menge, SUM(ma.gewicht) AS summe_gewicht, (e.best_wert * SUM(ma.gewicht)) AS summe_kg
FROM MATV030 m
INNER JOIN EINV030 e ON e.BESTELL_NR = m.BESTELL_NR
INNER JOIN EINV035 ei ON e.bestell_nr = ei.bestell_nr
INNER JOIN MATV010 ma ON m.ARTIKEL = ma.ARTIKEL
WHERE e.lieferant = '6000176' AND m.menge_buch <> 0
--AND m.artikel = 'H114972'
AND m.bs = 'WE' AND m.budatum >= '20190101' AND m.budatum < '20190201'
GROUP BY m.artikel, e.best_wert
ORDER BY m.artikel ASC
Try this,
;With CTE as
(
SELECT ma.ARTIKEL, SUM(ma.gewicht) AS summe_gewicht--, SUM(e.best_wert*ma.gewicht) AS summe_kg
FROM MATV030 m
INNER JOIN MATV010 ma ON m.ARTIKEL = ma.ARTIKEL
WHERE m.menge_buch > 0
--AND m.artikel = 'H114972'
AND m.bs = 'WE' AND m.budatum >= '20190101' AND m.budatum < '20190201'
GROUP BY m.ARTIKEL
--ORDER BY m.artikel ASC
)
SELECT m.artikel, COUNT(ei.bestell_po) bestell_po_menge,summe_gewicht, summe_gewicht*e.best_wert AS summe_kg
FROM CTE C
inner join MATV030 m on m.artikel=c.artikel
INNER JOIN EINV030 e ON e.BESTELL_NR = m.BESTELL_NR
INNER JOIN EINV035 ei ON e.bestell_nr = ei.bestell_nr
WHERE e.lieferant = '6000176'
GROUP BY m.artikel
ORDER BY m.artikel ASC
You can create A CTE (Common Table Expression) table with your aggregate values and then multiply both the tables using CTE.
I have query to select data from related tables.
SELECT
s.id,
CASE
WHEN count(DISTINCT e.id) <> 0
THEN count(DISTINCT o.id) / count(DISTINCT e.id)
END OrdersAverageNumber
FROM
[School] s
JOIN
[SchoolStore] ss ON ss.SchoolId = s.Id
JOIN
[Event] e ON e.SchoolId = ss.SchoolId
AND e.IsDeleted = 0
AND e.Status = 1
AND e.Date >= #startDate
AND e.Date <= #endDate
JOIN
[Order] o ON o.EventId = e.id
AND o.OrderStatus = 1
AND o.CreatedDate >= #startDate
AND o.CreatedDate <= #endDate
GROUP BY
s.id;
But I can't understand what I need to change to update all OrdersAverageNumber records in School table with values from selection above.
You can use update:
with q as (< your query here >)
update s
set OrdersAverageNumber = q.OrdersAverageNumber
from school s join
q
on s.id = q.id;
I pulled person_nbrs that have never had an EventType1 before or after an EventType2. I need to pull person_nbrs that have not had an EventType1 prior to having an EventType2. If they had an EventType1 after an EventType2, than it is to be ignored. Here is my query that pulls person_nbrs that have never had an EventType1 before or after EventType2.
SELECT
person_nbr, enc_nbr, enc_timestamp
FROM
person p
JOIN
patient_encounter pe ON p.person_id = pe.person_id
JOIN
patient_procedure pp ON pe.enc_id = pp.enc_id
WHERE
enc_timestamp >= '20170101'
--EventType2
AND code_id LIKE '2'
-- EventType1
AND person_nbr NOT IN (SELECT person_nbr
FROM person p
JOIN patient_encounter pe ON p.person_id = pe.person_id
JOIN patient_procedure pp ON pe.enc_id = pp.enc_id
WHERE code_id LIKE '1')
GROUP BY
person_nbr, enc_nbr, enc_timestamp
ORDER BY
person_nbr ;
You can do this with aggregation and a HAVING clause:
SELECT p.person_nbr
FROM person p JOIN
patient_encounter pe
ON p.person_id = pe.person_id JOIN
patient_procedure pp
ON pe.enc_id = pp.enc_id
GROUP BY p.person_nbr
HAVING SUM(CASE WHEN pp.code_id = 2 THEN 1 ELSE 0 END) > 0 AND -- has code 2
(MAX(CASE WHEN pp.code_id = 1 THEN pe.timestamp END) IS NULL OR
MAX(CASE WHEN pp.code_id = 1 THEN pe.timestamp END) < MIN(CASE WHEN pp.code_id = 2 THEN pe.timestamp END)
) ;
The HAVING clause has two parts:
The first specifies that the person has a code = 2.
The second specifies one of two conditions. The first is that there is no code = 1. The second alternative is that the latest c = 1 timestamp is less than the earliest code = 2 timestamp.
I need to add a where condition for a SQL query , means when the dept id become 9 only i need that where condition , else i dont required.
i have written the below query , is the approach is correct?
SELECT
b.DeptId,
b.DeptName,
a.SurveyID,
a.SurveyName,
a.Status,
a.AllUsers,
IsNull(a.SelectedUsers,'') as SelectedUsers,
a.OpenDate,
a.CloseDate,
e.Role as RoleName
from Surveys a
inner join Departments b
on a.deptid=b.deptid
left outer join
surveyUsers c
on c.surveyid=a.SurveyID
and c.empCode= 9902
left outer join [360HRSurveyEmployee] d
on d.surveyid=a.SurveyID
left outer join [360HRSurvey] e
on e.sempid = c.empCode
and e.empid = d.empid
where ( c.empCode= 9902 or a.AllUsers = 1 )
and a.status in (1)
and a.OpenDate <= '6/9/2015'
and a.CloseDate >= '6/9/2015'
and CASE WHEN DeptId == 9
THEN e.Role IS NOT NULL END
order by b.DeptID,a.SurveyID
Note the last three lines in the above query where i added the case :
and CASE WHEN DeptId == 9
THEN e.Role IS NOT NULL END
order by b.DeptID,a.SurveyID
I am getting a syntax error also
Incorrect syntax near '='.
If I understand you correctly, you only need rows where DeptId is not 9, or DeptId is not null. Also, your gross disregard for consistency in your capitalization hurts me. What is this beast?!
SELECT
b.DeptID, b.DeptName,
a.SurveyID, a.SurveyName, a.Status, a.AllUsers,
ISNULL(a.SelectedUsers,'') as SelectedUsers,
a.OpenDate, a.CloseDate, e.Role as RoleName
FROM
Surveys AS a
INNER JOIN Departments AS b
ON a.DeptID = b.DeptID
LEFT OUTER JOIN SurveyUsers AS c
ON (c.SurveyID = a.SurveyID AND c.EmpCode = 9902)
LEFT OUTER JOIN [360HRSurveyEmployee] AS d
ON d.SurveyID = a.SurveyID
LEFT OUTER JOIN [360HRSurvey] AS e
ON (e.EmpID = c.EmpCode AND e.EmpID = d.EmpID)
WHERE
(
c.EmpCode = 9902
OR a.AllUsers = 1
)
AND a.Status = 1
AND a.OpenDate <= '6/9/2015'
AND a.CloseDate >= '6/9/2015'
AND (
a.DeptID != 9
OR e.Role IS NOT NULL
)
ORDER BY
a.DeptID,
a.SurveyID;
and CASE WHEN DeptId == 9
You only need one = sign for this comparison.
You need to use single =
CASE WHEN DeptId = 9
THEN e.Role END
What exactly do you want to achive with this line? THEN e.Role IS NOT NULL END