I am having issues pulling in null values in my query. I am looking for patients who have a specific document name in their chart but also want to show patients who do not have this specific document name as well. Right now my code is only pulling in the patients with the document name History and Physical (Transcription) but I need to see Null values as well. Below is my code:
snip of code
SELECT CV3ClientVisit.ClientDisplayName, CV3ClientVisit.CurrentLocation, CV3ClientVisit.IDCode, CV3ClientVisit.VisitIDCode, CV3ClientVisit.VisitStatus, CV3ClientVisit.TypeCode, CV3ClientDocumentCUR.DocumentName
FROM CV3ClientVisit INNER JOIN
CV3ClientDocumentCUR ON CV3ClientVisit.GUID = CV3ClientDocumentCUR.ClientVisitGUID
WHERE (CV3ClientVisit.VisitStatus = 'ADM') AND (CV3ClientVisit.TypeCode = 'INPATIENT ADMIT') AND (CV3ClientDocumentCUR.DocumentName = 'History & Physical (transcription)' OR CV3ClientDocumentCUR.DocumentName IS NULL )
Use a LEFT JOIN with the condition in the ON clause:
SELECT cv.ClientDisplayName, cv.CurrentLocation, cv.IDCode,
cv.VisitIDCode, cv.VisitStatus, cv.TypeCode, cd.DocumentName
FROM CV3ClientVisit cv LEFT JOIN
CV3ClientDocumentCUR cd
ON cv.GUID = cd.ClientVisitGUID AND
cd.DocumentName = 'History & Physical (transcription)'
WHERE cv.VisitStatus = 'ADM' AND
cv.TypeCode = 'INPATIENT ADMIT' ;
I also added table aliases to simplify the query.
I'm Trying to replicate queries on DOMO from microsoft acccess. I assumed I had got the correct syntax but now I'm getting a The database reported a syntax error. Not unique table/alias: 'ga_wip_due_within_one_week'
SELECT `egl_inv`.Category, `egl_inv`.StyleClass, `egl_inv`.StyleMaster,
`egl_inv`.StyleMasterDesc, `egl_inv`.Color, `egl_inv`.ColorDesc,
`egl_inv`.Size,`egl_inv`.GscSku, `egl_inv`.UPC, `egl_inv`.RedistributionFlag,
`egl_inv`.Date_Soldout, `egl_inv`.GGS, `egl_inv`.Core, `egl_inv`.CartonQty,
If(IsNull(`ga_inv`.SumOfQty=True),0,`ga_inv`.SumOfQty) AS GA_INV,
If(IsNull(`ga_wip_due_within_one_week`.SumOfQty_Size_Open=True),
0,`ga_wip_due_within_one_week`.SumOfQty_Size_Open) AS GA_WIP,
If(IsNull(`ga_wip_due_within_one_week`.SumOfQty_Size_Open=True)
,0,`ga_wip_due_within_one_week`.`SumOfQty_Size_Open`.SumOfQty_Size) AS CUT,
If(IsNull(`averageweeklyfcst`=True),0,`averageweeklyfcst`) AS SS,
If(IsNull(`averageweeklyfcst`=True),0,`averageweeklyfcst`.`Demand`) AS
Demand, If(IsNull(`openorder`.OpenOrders=True),0,`openorder`.OpenOrders) AS
OpenOrders, If(IsNull(`egl_inv`.SumOfQty=True),0,`egl_inv`.SumOfQty) AS
EGL_INV,
If(IsNull(`averageweeklyfcst`.Demand=True),0,`averageweeklyfcst`.Demand)
AS AvgWklyFcst
FROM `averageweeklyfcst`
RIGHT JOIN (OpenOrders RIGHT JOIN (((((`egl_inv`
LEFT JOIN `ga_inv`ON `egl_inv`.GscSku = `ga_inv`.GscSku)
LEFT JOIN `ga_wip_due_within_one_week` ON `egl_inv`.GscSku =
`ga_wip_due_within_one_week`.GscSku) LEFT JOIN `ga_wip_due_within_one_week`
ON `egl_inv`.GscSku = `ga_wip_due_within_one_week`.GscSku)
LEFT JOIN `averageweeklyfcst` ON `
egl_inv`.GscSku = `averageweeklyfcst`.GscSku)
LEFT JOIN Demand ON `egl_inv`.GscSku = `averageweeklyfcst`.GscSku) ON
`openorder`.GscSku = `egl_inv`.GscSku) ON
`averageweeklyfcst`.GscSku = `egl_inv`.GscSku
GROUP BY EGL_INV.Category, `egl_inv`.StyleClass, `egl_inv`.StyleMaster,
`egl_inv`.StyleMasterDesc, `egl_inv`.Color, `egl_inv`.ColorDesc,
`egl_inv`.Size, `egl_inv`.GscSku, `egl_inv`.UPC,
`egl_inv`.RedistributionFlag, `egl_inv`.Date_Soldout, `egl_inv`.GGS,
`egl_inv`.Core, `egl_inv`.CartonQty,
If(IsNull(`ga_inv`.SumOfQty]=True),0,`ga_inv`.SumOfQty),
If(IsNull(`ga_wip_due_within_one_week`.SumOfQty_Size_Open=True),
0,`ga_wip_due_within_one_week`..SumOfQty_Size_Open),
If(IsNull(`ga_wip_due_within_one_week`.SumOfQty_Size_Open=True)
,0,`ga_wip_due_within_one_week`.SumOfQty_Size),
If(IsNull(`averageweeklyfcst`=True),0,`averageweeklyfcst`),
If(IsNull(`averageweeklyfcst`.Demand=True),0,`averageweeklyfcst`),
If(IsNull(`openorder`.OpenOrders=True),0,`openorder`.OpenOrders),
If(IsNull(`egl_inv`.`SumOfQty`=True),0,`egl_inv`.`SumOfQty`),
If(IsNull(`averageweeklyfcst`.Demand=True),0,`averageweeklyfcst`.`Demand`);
The database reported a syntax error. Not unique table/alias: 'ga_wip_due_within_one_week'
You have in the middle following line
LEFT JOIN `ga_wip_due_within_one_week` ON `egl_inv`.GscSku =
`ga_wip_due_within_one_week`.GscSku) LEFT JOIN `ga_wip_due_within_one_week`
as you see you add both without an aliais for the second ad something like as
ga_wip_due_within_one_week_1 for the second
In an MS Access database I try to make a join on three tables with conditions.
My current approach does not work:
SELECT
P.[COL1],
P.[COL2],
P.[COL3],
P.[COL4],
P.[COL5],
P.[PRGUID],
P.[COL6],
P.[COL7],
ARTIKEL.[COL1],
ARTIKEL.[NUMBER],
P.[PST_NR],
RECHNUNGEN.[RG_STORNO]
FROM P, A, R
WHERE R.[TIMEST] > #{from:yyyy-MM-dd HH:mm:ss}#
AND R.[TIMEST] < #{until:yyyy-MM-dd HH:mm:ss}#
AND A.[NUMBER] = VAL(P.[PLUX])
AND R.[RGUID] = P.[PRGUID] "
;
because the datasets in table P that do not have a corresponding entry in table A (condition: A.[NUMMER] = VAL(P.[PLUX])) are not returned.
I tried an approach with JOIN but I cannot find the right syntax.
Thank you #Gustav
The GUI designer helped (didn't even know it is there - just used SQL so far).
SELECT P.ColumnX, A.ColumnY, R.ColumnZ
FROM (P LEFT OUTER JOIN A.[NUMBER]) = VAL(P.[PLUX]))
INNER JOIN R ON P.[PRGUID] = R.[RGUID]
WHERE R.[TIMEST] > #yyyy-MM-dd HH:mm:ss#
AND R.[TIMEST] < #yyyy-MM-dd HH:mm:ss#
I had a query which was working just fine:
#schedule = Schedule.find(params[:id])
#schedule_tasks = ScheduleTask.select("s.*, t.*, t.*, d.*, st.*").from("schedule_tasks st").
joins("left join schedules s ON s.id = st.schedule_id").
joins("left join tasks t ON t.id = st.task_id").
joins("right join days d ON d.id = st.day_id").
order("d.number, t.name").
group_by{|d| d.number}
I had to refine my search to only schedule_tasks with a specific schedule_id, so I edited the second line to:
joins("left join schedules s ON s.id = st.schedule_id AND s.id = ?", #schedule.id).
This has cause the following error:
unknown class: Fixnum
The error goes away if I take out the group_by - but I need that, and I have tried hard coding in the number instead of #schedule.id and that does not work either, a google search does not reveal a lot of details on this error.
For anyone coming here from Google, I used plain string interpolation to fix this issue. This method is vulnerable to SQL Injection, so make sure you type check your variables before using them.
In this case I would do
#schedule_id = #schedule.id
.
.
.
joins("left join schedules s ON s.id = st.schedule_id AND s.id = #{#schedule_id}")
Rather than following learning_to_swim's answer, which as noted is at risk of SQL injection, couldn't you cast your #schedule_id to a string?
#tasks = ScheduleTask.joins("left join [...] s.id = ?", #schedule.id.to_s)
I faced with an unusual query result while running a sql server query.
The important table to note is Code_Structure_Type.
It contains columns Code_Structure_Type_Id smallint, Name char(50) and few other columns that are not used here.
My Query
SELECT DISTINCT A1.A1_Id AS Code_Unit_Attribute_Id,
A1.Name AS Code_Unit_Attribute_Name,
Code_Structure_Type.A1_Name AS Code_Attribute_Type
--, Code_Structure_Type.Code_Structure_Type_Id
FROM Defect
INNER JOIN Defect_Code_Unit
ON Defect.Defect_Id = Defect_Code_Unit.Defect_Id
INNER JOIN Code_Unit
ON Defect_Code_Unit.Code_Unit_Id = Code_Unit.Code_Unit_Id
INNER JOIN A1
ON Code_Unit.A1_Id = A1.A1_Id
INNER JOIN ERA_Module
ON Defect.ERA_Module_Id = ERA_Module.ERA_Module_Id
INNER JOIN Code_Unit_Type
ON Code_Unit.Code_Unit_Type_Id = Code_Unit_Type.Code_Unit_Type_Id
INNER JOIN Code_Structure_Type
ON Code_Unit_Type.Code_Structure_Type_Id = Code_Structure_Type.Code_Structure_Type_Id
WHERE ERA_Module.ERA_Project_Id = 472
AND ERA_Module.Is_Active = 1
AND Defect.Is_Closed = 0
AND Defect_Code_Unit.Is_Active = 1
AND Code_Structure_Type.Name = 'Pega Rules'
--AND Code_Structure_Type.Code_Structure_Type_Id = 2
AND A1.A1_Id > 0
AND Code_Unit.Scope_Id = 1
This query returns me with a single row which is incorrent. The expected result of this query should be none because we do not have Defects for 'Pega Rules' Code_Structure_Type.
If I uncomment this line
--, Code_Structure_Type.Code_Structure_Type_Id
I get no results which is correct.
Also if I filter from the Code_Structure_Type_Id which is for 'Pega Rules' not with Code_Structure_Type.Name I get the correct result as well which is none.
To do this I uncomment this line
--AND Code_Structure_Type.Code_Structure_Type_Id = 2
and comment this line
AND Code_Structure_Type.Name = 'Pega Rules'
So basically I'm doing the same thing and there is no other name like 'Pega Rules' in the table Code_Structure_Type, not even close. Only five rows are there in that table.
I need to understand what is happening here.
Thanks in advance.
Links to Execution Plans
Files, when used 'pega rules' only - incorrect_original.xml when used 'pega rules' + select - correct_with_added_select.xml when used id - correct_with_Id.xml