Invalid Identifier in Temp Table Left Join Clause - sql

Good Evening EST,
I am stuck on the below query, I keep getting an invalid identifier error at line 12, the first "DELETE FLAG". Not sure if I'm missing some syntax or doing something else silly. (I have very thoroughly checked I didn't misspell anything and all the fields exist)
Oracle SQL Developer V. 17.2 Any help would be greatly appreciated.
SELECT
CM.SUBJECT_PERSON_ID,
CM.COHORT_SCOPED_IDENTIFIER AS SUBJECT_STUDY_ID,
SA3.ALT_ID_VALUE AS GLOBAL_SUBJECT_ID,
V.PREFERRED_TERM AS MEMBER_STATUS,
CM.COHORT_ID,
SA2.ALT_ID_VALUE AS CRIN_ID
FROM GLENAPO.COHORT_MEMBER CM
LEFT JOIN GLENAPO.SUBJECT_ALT_ID SA1
ON SA2.SUBJECT_PERSON_ID =CM.SUBJECT_PERSON_ID
AND SA2.IDENTIFIER_TYPE_ID = 8 -- get SDG_ID
AND SA2.DELETE_FLAG = 'N'
LEFT JOIN GLENAPO.SUBJECT_ALT_ID SA1
ON SA2.SUBJECT_PERSON_ID =CM.SUBJECT_PERSON_ID
AND SA2.IDENTIFIER_TYPE_ID = 8 -- get SDG_ID
AND SA2.DELETE_FLAG = 'N'
LEFT JOIN GLENAPO.SUBJECT_ALT_ID SA2
ON SA2.SUBJECT_PERSON_ID =CM.SUBJECT_PERSON_ID
AND SA2.IDENTIFIER_TYPE_ID = 18 -- get CRIN ID
AND SA2.DELETE_FLAG = 'N'
JOIN GLENAPO.SUBJECT_ALT_ID SA3
ON SA3.SUBJECT_PERSON_ID =CM.SUBJECT_PERSON_ID
AND SA3.IDENTIFIER_TYPE_ID = 12 -- get Global Subject
AND SA3.DELETE_FLAG = 'N'
LEFT JOIN GLENAPO.VOCAB_TERM_VIEW V
ON V.TABLE_NAME = 'COHORT_MEMBER'
AND V.COLUMN_NAME = 'STATUS_CODE'
AND V.CONCEPT_ID = CM.STATUS_CODE
LEFT JOIN GLENAPO.FACILITY_REFERENCE FR ON FR.FACILITY_ID = CM.FACILITY_ID
WHERE CM.DELETE_FLAG = 'N'

This is the beginning of your FROM clause:
FROM GLENAPO.COHORT_MEMBER CM LEFT JOIN
GLENAPO.SUBJECT_ALT_ID SA1
ON SA2.SUBJECT_PERSON_ID = CM.SUBJECT_PERSON_ID AND
SA2.IDENTIFIER_TYPE_ID = 8 AND -- get SDG_ID
SA2.DELETE_FLAG = 'N'
A table alias needs to be defined before it is used. I think you intend SA1, rather than SA2:
FROM GLENAPO.COHORT_MEMBER CM LEFT JOIN
GLENAPO.SUBJECT_ALT_ID SA1
ON SA1.SUBJECT_PERSON_ID = CM.SUBJECT_PERSON_ID AND
SA1.IDENTIFIER_TYPE_ID = 8 AND -- get SDG_ID
SA1.DELETE_FLAG = 'N'

Related

wrongly totalling all rows and not just the ones with a certain value for a certain column [duplicate]

This question already has answers here:
Two SQL LEFT JOINS produce incorrect result
(3 answers)
Closed 9 months ago.
I may have messed up the joins or there may be another way of writing what I am trying to achieve.
My current query is this:
SELECT
i.SKI_NAME AS Description,
l.SKV_QUANTITY_IN_STOCK AS Qty,
SUM(CASE WHEN ad.AVN_STATUS = 'D' THEN li.AVL_QUANTITY ELSE '0' END) AS AdviceQty,
SUM(CASE WHEN con.HCT_STATUS = 'D' THEN item.HIT_QUANTITY ELSE '0' END) AS HireQty
FROM
TH_STOCK_LEVELS l
LEFT JOIN TH_STOCK_ITEMS i ON l.SKV_STOCK_NUMBER = i.SKI_STOCK_NUMBER
LEFT JOIN TH_ADVICE_NOTE_LINES li ON i.SKI_NAME = li.AVL_DESCRIPTION
LEFT JOIN TH_HIRE_ITEMS Item ON li.AVL_DESCRIPTION = Item.HIT_DESCRIPTION
LEFT JOIN TH_ADVICE_NOTES ad ON ad.AVN_ID = li.AVL_NOTE_NUMBER
LEFT JOIN TH_HIRE_CONTRACTS con ON con.HCT_CONTRACT_NUMBER = Item.HIT_CONTRACT_NUMBER
WHERE
l.SKV_DEPOT_ID = 7
GROUP BY i.SKI_NAME, l.SKV_QUANTITY_IN_STOCK;
This displays the following output:
Description
Qty
AdviceQty
HireQty
Some Item
2
400
100
Some Item
0
100
0
Which is incorrect, as it seems to be totalling all previous Advice's and Hire's and not just the ones with Status 'D'.
If I do the following to the query (comment some lines out):
SELECT
i.SKI_NAME AS Description,
l.SKV_QUANTITY_IN_STOCK AS Qty,
SUM(CASE WHEN ad.AVN_STATUS = 'D' THEN li.AVL_QUANTITY ELSE '0' END) AS AdviceQty
--SUM(CASE WHEN con.HCT_STATUS = 'D' THEN item.HIT_QUANTITY ELSE '0' END) AS HireQty
FROM
TH_STOCK_LEVELS l
LEFT JOIN TH_STOCK_ITEMS i ON l.SKV_STOCK_NUMBER = i.SKI_STOCK_NUMBER
LEFT JOIN TH_ADVICE_NOTE_LINES li ON i.SKI_NAME = li.AVL_DESCRIPTION
--LEFT JOIN TH_HIRE_ITEMS Item ON li.AVL_DESCRIPTION = Item.HIT_DESCRIPTION
LEFT JOIN TH_ADVICE_NOTES ad ON ad.AVN_ID = li.AVL_NOTE_NUMBER
--LEFT JOIN TH_HIRE_CONTRACTS con ON con.HCT_CONTRACT_NUMBER = Item.HIT_CONTRACT_NUMBER
WHERE
l.SKV_DEPOT_ID = 7
GROUP BY i.SKI_NAME, l.SKV_QUANTITY_IN_STOCK;
The output is correct, although I am now missing a column due to the comments. This also works if I comment out the Advice tables, the HireQty column would be correct.
Description
Qty
AdviceQty
Some Item
2
10
Some Item
0
0
How do I get this to display the correct data for both AdviceQty & HireQty without having to do them separately?
Sometimes, CASE statement doesn't work. You can try with IFs and see if it is working.
SELECT
i.SKI_NAME AS Description,
l.SKV_QUANTITY_IN_STOCK AS Qty, SUM(IF(ad.AVN_STATUS='D',li.AVL_QUANTITY,0)) AS AdviceQty, SUM(IF(con.HCT_STATUS='D',item.HIT_QUANTITY,0)) AS HireQty
FROM
TH_STOCK_LEVELS l
LEFT JOIN TH_STOCK_ITEMS i ON l.SKV_STOCK_NUMBER = i.SKI_STOCK_NUMBER LEFT JOIN TH_ADVICE_NOTE_LINES li ON i.SKI_NAME = li.AVL_DESCRIPTION LEFT JOIN TH_HIRE_ITEMS Item ON li.AVL_DESCRIPTION
= Item.HIT_DESCRIPTION LEFT JOIN TH_ADVICE_NOTES ad ON ad.AVN_ID = li.AVL_NOTE_NUMBER LEFT JOIN TH_HIRE_CONTRACTS con ON con.HCT_CONTRACT_NUMBER = Item.HIT_CONTRACT_NUMBER
WHERE
l.SKV_DEPOT_ID = 7
GROUP BY i.SKI_NAME, l.SKV_QUANTITY_IN_STOCK;

POSTGRES Update with left outer join is not working

Please suggest, what am I doing wrong.
UPDATE uc
SET uc.selected_value_id = cv.id, uc.fixed_value = NULL
FROM unit_characteristic uc
left JOIN characteristic_value cv ON uc.fixed_value like CONCAT(cv.value,'%')
WHERE cv.characteristic_id = 6
and uc.characteristic_id = 6
and uc.unit_id in (6313,6314)
Getting error
SQL Error [42P01]: ERROR: relation "uc" does not exist
Position: 8
org.postgresql.util.PSQLException: ERROR: relation "uc" does not exist
Position: 8
While this select is working fine
select count(uc.*)
FROM unit_characteristic uc
left JOIN characteristic_value cv ON uc.fixed_value like CONCAT(cv.value,'%')
WHERE cv.characteristic_id = 6
and uc.characteristic_id = 6
and uc.unit_id in (6313,6314)
you don't have to repeat the target table in the FROM clause; it is already in the range table
the target table can have an alias
but, the SET columnname = new_value line must not use this alias. It is implicit (since there is only one table reference to be updated)
UPDATE unit_characteristic uc
SET selected_value_id = cv.id
, fixed_value = NULL
FROM characteristic_value cv
WHERE uc.fixed_value like cv.value || '%'
AND cv.characteristic_id = 6
AND uc.characteristic_id = 6
AND uc.unit_id in (6313, 6314)
;
In Postgres, the reference in the update cannot refer to a table in the from. I suspect you want:
update unit_characteristic uc
set selected_value_id = cv.id,
fixed_value = NULL
from characteristic_value cv
where uc.fixed_value like cv.value || '%' and
cv.characteristic_id = 6 and
uc.characteristic_id = 6 and
uc.unit_id in (6313, 6314);
Note that your version of the query uses left join. But the where clause turns that into an inner join anway.

SQL query fails, no idea why

So I have just upgraded to MySQL 8 to test if it would be a viable option, but I have stumbled upon quiet the riddle.
It throws an error at 'table_product_features AS pf' but I can't seem to figure out why as in the documentation it is done exactly the same way: https://dev.mysql.com/doc/refman/8.0/en/join.html
Error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups ON pf.parent_id = groups.feature_id LEFT JOIN table_product_features_des' at line 1 (1064)
I also checked the bug tracker but that doesnt seem to have anything related to my issue.
What exactly am I doing wrong here?
P.S. I put it in a code snippet so that it would remain formatted.
SELECT pf.feature_id,
pf.company_id,
pf.feature_type,
pf.parent_id,
pf.display_on_product,
pf.display_on_catalog,
pf.display_on_header,
table_product_features_descriptions.description,
table_product_features_descriptions.lang_code,
table_product_features_descriptions.prefix,
table_product_features_descriptions.suffix,
pf.categories_path,
table_product_features_descriptions.full_description,
pf.status,
pf.comparison,
pf.position,
groups.position AS group_position,
table_product_features_values.value,
table_product_features_values.variant_id,
table_product_features_values.value_int
FROM table_product_features AS pf
LEFT JOIN table_product_features AS groups
ON pf.parent_id = groups.feature_id
LEFT JOIN table_product_features_descriptions
ON table_product_features_descriptions.feature_id = pf.feature_id
AND table_product_features_descriptions.lang_code = 'en'
INNER JOIN table_product_features_values
ON table_product_features_values.feature_id = pf.feature_id
AND table_product_features_values.product_id = 230
AND table_product_features_values.lang_code = 'en'
INNER JOIN table_ult_objects_sharing
ON ( table_ult_objects_sharing.share_object_id = pf.feature_id
AND table_ult_objects_sharing.share_company_id = 1
AND table_ult_objects_sharing.share_object_type =
'product_features' )
WHERE 1
AND pf.feature_type != 'G'
AND pf.status IN ( 'A' )
AND pf.display_on_product = 'Y'
AND ( pf.categories_path = ''
OR Isnull(pf.categories_path)
OR Find_in_set(203, pf.categories_path)
OR Find_in_set(215, pf.categories_path)
OR Find_in_set(216, pf.categories_path) )
GROUP BY pf.feature_id
ORDER BY group_position,
pf.position,
table_product_features_descriptions.description
If your not using aggregate functions I don't see why you need the GROUP BY.
Also groups is a reserved word, change it to tgroups or something else.
SELECT pf.feature_id,
pf.company_id,
pf.feature_type,
pf.parent_id,
pf.display_on_product,
pf.display_on_catalog,
pf.display_on_header,
tdesc.description,
tdesc.lang_code,
tdesc.prefix,
tdesc.suffix,
pf.categories_path,
tdesc.full_description,
pf.status,
pf.comparison,
pf.position,
tgroups.position group_position,
tvals.value,
tvals.variant_id,
tvals.value_int
FROM table_product_features pf
LEFT JOIN table_product_features tgroups ON pf.parent_id = tgroups.feature_id
LEFT JOIN table_product_features_descriptions tdesc ON tdesc.feature_id = pf.feature_id AND tdesc.lang_code = 'en'
INNER JOIN table_product_features_values tvals ON tvals.feature_id = pf.feature_id AND tvals.product_id = 230 AND tvals.lang_code = 'en'
INNER JOIN table_ult_objects_sharing tshar ON (tshar.share_object_id = pf.feature_id AND tshar.share_company_id = 1 AND tshar.share_object_type = 'product_features')
WHERE 1
AND pf.feature_type != 'G'
AND pf.status IN ('A')
AND pf.display_on_product = 'Y'
AND (pf.categories_path = '' OR Isnull(pf.categories_path) OR Find_in_set(203, pf.categories_path) OR Find_in_set(215, pf.categories_path) OR Find_in_set(216, pf.categories_path))
ORDER BY group_position, pf.position, tdesc.description

SQL Joining tables but getting skipped values

Hi i have joined to tables the code is below. Notice I have used A.Manad = B.Manad which joins data where the month of table A and B is equal. But sometimes table B dont have any data for that month. My code just skip the data, i would rather it just leave it empty or with a value of 0.
The Code takes a list of Orgnr which is swedish for company numbers and joins two tables where the orgnr is the same and the month is the same, but for some reason it doesnt join the data when the value is empty for one company. I still want the orgnr to show up in the joint table.
select Tillnr = A.tillnr, Orgnr = A.orgnr, Månad = A.Manad, Intrastat =
A.varde,Moms = B.vardeutf
into #Tabell1
From
IntrastatFsum A
left outer join
Momsuppg B
on A.Orgnr = B.Orgnr
where A.Orgnr in(
165563137933,165020456017,.......)
AND A.Ar = 2017
AND B.Ar = A.AR
AND A.Manad = 9
AND A.Manad = B.Manad
AND A.InfUtf = 'U'
You should move your WHERE clause AND A.Manad = B.Manad and AND B.Ar = A.AR to the LEFT JOIN clause.
In this way you will preserve all data from table IntrastatFsum:
select Tillnr = A.tillnr, Orgnr = A.orgnr, Månad = A.Manad, Intrastat =
A.varde,Moms = B.vardeutf
into #Tabell1
From
IntrastatFsum A
left outer join
Momsuppg B
on A.Orgnr = B.Orgnr
AND A.Manad = B.Manad
AND A.AR = B.Ar
where A.Orgnr in(
165563137933,165020456017,.......)
AND A.Ar = 2017
AND A.Manad = 9
AND A.InfUtf = 'U'

SQL joins returning multiple results

select
tmp.templatedesc Template
,sec.name Section
,q.questiontext Questions,
--,sum(case when q.responserequired = '0' then 1 else null end) as 'N/A'
--,sum(case when q.responserequired = '1' then 1 else null end) as Scored
--,count (case when (qr.weightedscore is not null and tmp.templatedesc = 'QA 30 Day Call Form' and
--sec.name = 'opening' and
--rv.reviewstatusid = 1 )then 1 else null end) as scored
----,(case when qr.weightedscore <> q.weight then rv.reviewid else null end) as fail
--count (case when qr.weightedscore is null then 1 else null end) NA,
--count (case when qr.weightedscore is not null then 1 else null end) scored,
sec.sequencenumber, q.questionnumber, qr.*
from
aqm.dbo.reviewtemplate tmp (nolock)
inner join aqm.dbo.section sec on sec.templateid =tmp.templateid
inner join aqm.dbo.sectionresult scr on scr.sectionid = sec.sectionid
inner join aqm.dbo.questionresult qr on qr.sectionresultid = scr.sectionresultid
inner join aqm.dbo.question q on q.questionid = qr.questionid
--inner join aqm.dbo.questiontype qt on qt.questiontypeid = q.questiontypeid
--left outer join aqm.dbo.questionoption qo on qo.questionid = q.questionid
inner join aqm.dbo.review rv on tmp.templateid = rv.templateid
inner join aqm.dbo.media md on md.mediaid = rv.mediaid
inner join aqm.dbo.iqmuser ut on md.userid = ut.userid
where
rv.reviewstatusid = 1 and
tmp.templatedesc = 'QA 30 Day Call Form'
and sec.name = 'opening' and
convert(varchar,dateadd(hh,-7,rv.reviewdate), 101) = '07/07/2014'
and ut.windowslogonaccount = 'name.name'
and q.questionnumber = 4
--group by
--tmp.templatedesc , sec.name, q.questiontext, sec.sequencenumber, q.questionnumber
order by
sec.sequencenumber, q.questionnumber
the questionresultid and sectionresultid are returning multiple values
how can i fix the joins so that it doesnt return multiple values?
i have it drilled down to a date and a person so that it should only return one row of results( but that obviously didnt work)
not sure what other data i can provide
update
i think it has to do with joins
inner join aqm.dbo.sectionresult scr on scr.sectionid = sec.sectionid
inner join aqm.dbo.questionresult qr on qr.sectionresultid = scr.sectionresultid
as those are the ones returning multiple results.
just dont know how to fix it
First, neither aqm.dbo.questiontype nor aqm.dbo.questionoption are used in your return fields or your where clause so get rid of them if they aren't required.
Second, you are OUTER JOINing on the aqm.dbo.review, but the reviewstatusid and reviewdate are required in the WHERE clause - so this should probably be an INNER JOIN.
Last, best way to debug issues like this is to comment out the COUNT statements and the GROUP BY clause - and see what raw data is being returned.