How To Use WHERE clause with LEFT JOIN - mysql

Here is my query:
SELECT 2016step2a1.id_step2a1, 2016step2a1.camperLast, 2016step2a1.camperFirst,
2016step2a1.camperGender, 2016step2a1.camperAge, 2016step2a1.guardName,
2016step2a1.guardCellphone, 2016step2a1.camperAddress, 2016step2a1.camperApartment,
2016step2a1.camperCity, 2016step2a1.camperState, 2016step2a1.camperZip,
2016step2a1.guardEmail, 2016step2a1.camperDiag, 2016step2a6.shirtT
FROM 2016step2a1
LEFT JOIN 2016step2a6 ON 2016step2a1.user_id = 2016step2a6.user_id
LEFT JOIN 2016step2a4 ON 2016step2a1.user_id = 2016step2a4.user_id
LEFT JOIN 2016step2a5 ON 2016step2a1.user_id = 2016step2a5.user_id
WHERE 2016step2a1.whichCamp = CampIAM
Can someone please help me figure out why this wont run?

Change your where clause to properly quote the right hand side value.
WHERE 2016step2a1.whichCamp = 'CampIAM'

Related

DOMO - MYSQL Transformation Database table error

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

"The select statement includes a reserved word or an argument name that is misspelled"

I am unable to figure out why this message keeps appearing.
The select statement includes a reserved word or an argument name that is misspelled.
However, I am unable to find the problem in my code.
Can anyone help and identify what am I missing?
SELECT DISTINCT
A.CINBR, SUM(C.EAANU) AS EAANU, SUM(C.MPRPQ) AS ONORD,
IIF(C.ITCLS = "DPAR","DPAR",""),
IIF(C.ITCLS = "CONE","CONE",""),
IIF(C.ITCLS = "PART","PART",""),
IIF(C.ITCLS = "SPDY","SPDY",""),
FROM AMFLIB.PSTDTL A LEFT OUTER JOIN
AMFLIB.ITEMBL C ON A.PINBR = C.ITNBR RIGHT OUTER JOIN
INTonSHELF D ON A.CINBR = D .ITNBR
WHERE C.EAANU > 0
GROUP BY A.CINBR, C.ITCLS
ORDER BY A.CINBR
Disregard what Access is saying, most of the time, it is unhelpful.
That being said the following are the problems:
SELECT DISTINCT A.CINBR
,SUM(C.EAANU) AS EAANU
,SUM(C.MPRPQ) AS ONORD
,IIF(C.ITCLS = "DPAR", "DPAR", "")
,IIF(C.ITCLS = "CONE", "CONE", "")
,IIF(C.ITCLS = "PART", "PART", "")
,IIF(C.ITCLS = "SPDY", "SPDY", "")
FROM AMFLIB.PSTDTL A
LEFT JOIN AMFLIB.ITEMBL C ON A.PINBR = C.ITNBR
RIGHT JOIN INTonSHELF D ON A.CINBR = D.ITNBR
WHERE C.EAANU > 0
GROUP BY A.CINBR
,C.ITCLS
ORDER BY A.CINBR
Specifically, this line was causing issues: IIF(C.ITCLS = "SPDY","SPDY",""), There was an extra comma attached to the SELECT statement.
Additionally Access does not support LEFT OUTER JOIN or RIGHT OUTER JOIN as functions

Left Join not returning Null or Non-matching values

I want to return all values from PF_OUTPUT where the token ID equals "AdmissionInfo" as well as the matching results from PF_RESULTS just for PAT_SEQ = 105566168.
I've looked at some other questions and I have included the null statement within the Left join where clause, however it still is just returning matching values.
SELECT CCDEV.PF_OUTPUT.TOKEN_NAME,
CCDEV.PF_OUTPUT.DISPLAY_SEQ,
CCDEV.PF_OUTPUT.RES_SEQ,
CCDEV.PF_OUTPUT.LABEL_SEQ,
CCDEV.PF_OUTPUT.DISPLAY_RESULT_NAME,
CCDEV.PF_OUTPUT.OUTPUT_RESULT_NAME,
CCDEV.PF_OUTPUT.OUTPUT_FIELD_NAME,
PF_RESULTS.PERFORM_DDT,
PF_RESULTS.CHART_DDT,
PF_RESULTS.STAFF_SEQ,
PF_RESULTS.RESULT_VALUE,
PF_RESULTS.STATUS,
PF_RESULTS.PF_RESULTS_SEQ,
PF_RESULTS.PAT_SEQ,
PF_RESULTS.PF_RESULT_SEQ,
PF_RESULTS.RECORD_VERSION,
PF_RESULTS.LABEL_SEQ AS LABEL_SEQ1
FROM CCDEV.PF_OUTPUT
LEFT JOIN PF_RESULTS
ON CCDEV.PF_OUTPUT.LABEL_SEQ = PF_RESULTS.LABEL_SEQ
AND CCDEV.PF_OUTPUT.RES_SEQ = PF_RESULTS.RES_SEQ
WHERE (CCDEV.PF_OUTPUT.TOKEN_NAME = 'AdmissionInfo'
AND PF_RESULTS.PAT_SEQ = 105566168)
OR (CCDEV.PF_OUTPUT.TOKEN_NAME = 'AdmissionInfo'
AND PF_RESULTS.PAT_SEQ IS NULL)
ORDER BY CCDEV.PF_OUTPUT.TOKEN_NAME,
CCDEV.PF_OUTPUT.DISPLAY_SEQ
Move PF_RESULTS.PAT_SEQ = 105566168 line from your where condition to your ON clause of the join.
SELECT CCDEV.PF_OUTPUT.TOKEN_NAME,
CCDEV.PF_OUTPUT.DISPLAY_SEQ,
CCDEV.PF_OUTPUT.RES_SEQ,
CCDEV.PF_OUTPUT.LABEL_SEQ,
CCDEV.PF_OUTPUT.DISPLAY_RESULT_NAME,
CCDEV.PF_OUTPUT.OUTPUT_RESULT_NAME,
CCDEV.PF_OUTPUT.OUTPUT_FIELD_NAME,
PF_RESULTS.PERFORM_DDT,
PF_RESULTS.CHART_DDT,
PF_RESULTS.STAFF_SEQ,
PF_RESULTS.RESULT_VALUE,
PF_RESULTS.STATUS,
PF_RESULTS.PF_RESULTS_SEQ,
PF_RESULTS.PAT_SEQ,
PF_RESULTS.PF_RESULT_SEQ,
PF_RESULTS.RECORD_VERSION,
PF_RESULTS.LABEL_SEQ AS LABEL_SEQ1
FROM CCDEV.PF_OUTPUT
LEFT JOIN PF_RESULTS
ON CCDEV.PF_OUTPUT.LABEL_SEQ = PF_RESULTS.LABEL_SEQ
AND CCDEV.PF_OUTPUT.RES_SEQ = PF_RESULTS.RES_SEQ
AND PF_RESULTS.PAT_SEQ = 105566168
WHERE (CCDEV.PF_OUTPUT.TOKEN_NAME = 'AdmissionInfo')
OR (CCDEV.PF_OUTPUT.TOKEN_NAME = 'AdmissionInfo'
AND PF_RESULTS.PAT_SEQ IS NULL)
ORDER BY CCDEV.PF_OUTPUT.TOKEN_NAME,
CCDEV.PF_OUTPUT.DISPLAY_SEQ
When you include a column from the RIGHT table of your LEFT join you essentially eliminate the LEFT join and make it an inner join. Unless you are saying where RIGHTTable.column IS NULL.
It looks like you're using the left-joined table in the where clause. This effectively makes it an inner join since any record that doesn't exist wil always yield false.
Unless of course the check in the where clause includes an 'is null' check.
You should move the PF_RESULTS.PAT_SEQ = 105566168 check to the on clause of the left join to fix this.

mysql select from a view with where condition gives different result than executing the view definition with where condition

I have a view with the following definition:
select
`cb_trans_detail`.`numero_partida` AS `numero_partida`,
`cb_trans_head`.`fecha_partida` AS `fecha_partida`,
`cb_trans_detail`.`concepto_partida` AS `concepto_partida`,
`cb_cuenta`.`nombre_cuenta` AS `nombre_cuenta`,
`cb_cuenta`.`codigo_mayor` AS `codigo_mayor`,
`cb_mayor`.`nombre_mayor` AS `nombre_mayor`,
`cb_mayor`.`categoria` AS `categoria`,
`cb_categoria`.`nombre` AS `nombre`,
`cb_categoria`.`presentacion` AS `presentacion`,
`cb_trans_detail`.`codigo_cuenta` AS `codigo_cuenta`,
sum(`cb_trans_detail`.`debito_partida`) AS `Debitos`,
sum(`cb_trans_detail`.`credito_partida`) AS `Creditos`,
`cb_cuenta`.`saldo_inicial` AS `saldo_inicial`,
((`cb_cuenta`.`saldo_inicial` +
sum(`cb_trans_detail`.`debito_partida`)) -
sum(`cb_trans_detail`.`credito_partida`)) AS `Saldo`,
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`)
AS `Codigo`
from
((((`cb_trans_detail` join `cb_cuenta`
on(((`cb_trans_detail`.`codigo_cuenta` = `cb_cuenta`.`codigo_cuenta`)
and (`cb_trans_detail`.`categoria` = `cb_cuenta`.`categoria`)
and (`cb_trans_detail`.`codigo_mayor` = `cb_cuenta`.`codigo_mayor`))))
join `cb_mayor` on(((`cb_cuenta`.`codigo_mayor` = `cb_mayor`.`codigo_mayor`)
and (`cb_cuenta`.`categoria` = `cb_mayor`.`categoria`))))
join `cb_categoria` on(((`cb_mayor`.`categoria` = `cb_categoria`.`categoria`)
and (`cb_trans_detail`.`categoria` = `cb_categoria`.`categoria`))))
left join `cb_trans_head` on((`cb_trans_detail`.`numero_partida` =
`cb_trans_head`.`numero_partida`)))
where
(`cb_categoria`.`presentacion` = '1')
group by concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,
`cb_trans_detail`.`codigo_cuenta`)
If I select from this view as follows:
SELECT
`balance_general_view`.`numero_partida`,
`balance_general_view`.`fecha_partida`,
`balance_general_view`.`concepto_partida`,
`balance_general_view`.`nombre_cuenta`,
`balance_general_view`.`codigo_mayor`,
`balance_general_view`.`nombre_mayor`,
`balance_general_view`.`categoria`,
`balance_general_view`.`nombre`,
`balance_general_view`.`presentacion`,
`balance_general_view`.`codigo_cuenta`,
`balance_general_view`.`Debitos`,
`balance_general_view`.`Creditos`,
`balance_general_view`.`saldo_inicial`,
`balance_general_view`.`Saldo`,
`balance_general_view`.`Codigo`
FROM
`balance_general_view`
WHERE
`balance_general_view`.`fecha_partida` BETWEEN '2014-01-01' AND '2014-01-31'
this yields a different result than if I execute the query as follows:
select
`cb_trans_detail`.`numero_partida` AS `numero_partida`,
`cb_trans_head`.`fecha_partida` AS `fecha_partida`,
`cb_trans_detail`.`concepto_partida` AS `concepto_partida`,
`cb_cuenta`.`nombre_cuenta` AS `nombre_cuenta`,
`cb_cuenta`.`codigo_mayor` AS `codigo_mayor`,
`cb_mayor`.`nombre_mayor` AS `nombre_mayor`,
`cb_mayor`.`categoria` AS `categoria`,
`cb_categoria`.`nombre` AS `nombre`,
`cb_categoria`.`presentacion` AS `presentacion`,
`cb_trans_detail`.`codigo_cuenta` AS `codigo_cuenta`,
sum(`cb_trans_detail`.`debito_partida`) AS `Debitos`,
sum(`cb_trans_detail`.`credito_partida`) AS `Creditos`,
`cb_cuenta`.`saldo_inicial` AS `saldo_inicial`,
((`cb_cuenta`.`saldo_inicial` + sum(`cb_trans_detail`.`debito_partida`)) - sum(`cb_trans_detail`.`credito_partida`)) AS `Saldo`,
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`) AS `Codigo`
from
((((`cb_trans_detail` join `cb_cuenta` on(((`cb_trans_detail`.`codigo_cuenta` = `cb_cuenta`.`codigo_cuenta`) and (`cb_trans_detail`.`categoria` = `cb_cuenta`.`categoria`) and (`cb_trans_detail`.`codigo_mayor` = `cb_cuenta`.`codigo_mayor`)))) join `cb_mayor` on(((`cb_cuenta`.`codigo_mayor` = `cb_mayor`.`codigo_mayor`) and (`cb_cuenta`.`categoria` = `cb_mayor`.`categoria`)))) join `cb_categoria` on(((`cb_mayor`.`categoria` = `cb_categoria`.`categoria`) and (`cb_trans_detail`.`categoria` = `cb_categoria`.`categoria`)))) left join `cb_trans_head` on((`cb_trans_detail`.`numero_partida` = `cb_trans_head`.`numero_partida`)))
where
(`cb_categoria`.`presentacion` = '1') and `cb_trans_head`.`fecha_partida` BETWEEN '2014-01-01' and '2014-01-31'
group by
concat(`cb_mayor`.`categoria`,`cb_cuenta`.`codigo_mayor`,`cb_trans_detail`.`codigo_cuenta`)
My question is: How to get the result I need using the view and filtering programatically instead of hard-coding the where condition? Thank you. If you need the individual table definitions let me know. Much appreciated.
If you use a left join to a table X and a condition in the WHERE-clause to this table X, you change your left join to an inner one. If you want to restrict the results by the values of this left joined table, you must use this condition in the ON clause of the left join instead:
...
LEFT JOIN
cb_trans_head
ON
cb_trans_detail.numero_partida = cb_trans_head.numero_partida
AND
cb_trans_head.fecha_partida BETWEEN '2014-01-01' and '2014-01-31'
...
If there's another one that I overlook, tread it the same way.

MYSQL IF Function

I am using two joins on the same table for two different select statements. I am wanting to implement an IF function, due to the fact that the table may contain a zero, I am wanting to substitue the zero for the word 'None'
Here is my SQL that works:
SELECT
CONCAT(`payments`.`AssignedTo`," - ",`people2`.`FName`," ",`people2`.`LName`) AS `AssignedTo`,
CONCAT(`payments`.`PersonID`," - ",`people`.`FName`," ",`people`.`LName`) AS `PersonName`
FROM `mb_payments` as `payments`
LEFT JOIN `mb_people` AS `people` ON `people`.`PersonID` = `payments`.`PersonID`
LEFT JOIN `mb_people` AS `people2` ON `people2`.`PersonID` = `payments`.`AssignedTo`
Here is my SQL with the IF function that does not work:
SELECT
IF(AssignedTo IS 0,'None', CONCAT(`payments`.`AssignedTo`," - ",`people2`.`FName`," ",`people2`.`LName`)),
CONCAT(`payments`.`PersonID`," - ",`people`.`FName`," ",`people`.`LName`) AS `PersonName`
FROM `mb_payments` as `payments`
LEFT JOIN `mb_people` AS `people` ON `people`.`PersonID` = `payments`.`PersonID`
LEFT JOIN `mb_people` AS `people2` ON `people2`.`PersonID` = `payments`.`AssignedTo`
You can use CASE statement in this case or if you want to use IF statement then your statement is wrong.
It should be:
SELECT
IF(AssignedTo = 0,'None', CONCAT(`payments`.`AssignedTo`," - ",`people2`.`FName`," ",`people2`.`LName`)) AS Result,
CONCAT(`payments`.`PersonID`," - ",`people`.`FName`," ",`people`.`LName`) AS `PersonName`
FROM `mb_payments` as `payments`
LEFT JOIN `mb_people` AS `people` ON `people`.`PersonID` = `payments`.`PersonID`
LEFT JOIN `mb_people` AS `people2` ON `people2`.`PersonID` = `payments`.`AssignedTo`
Check out this blog: http://timmurphy.org/2009/08/13/inline-if-and-case-statements-in-mysql/
I think you want ".. = 0", not "... IS 0". You use IS when comparing with NULL.