Ambigously defined column in a subquery - sql

I've the following subquery in an sql query:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, NOMBRE AS NOMBREUNIDAD FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY ID_PLAN, ID_CURSO, NEDICION) ASIS
Problem I have I believe lies in that both table ALUMNOS and UNIDADES have a column named 'NOMBRE' so if I attempt to execute the query I obtain:
00000 - "column ambiguously defined"
To avoid that I thought about changing NOMBRE AS NOMBREUNIDAD to:
UNIDADES.NOMBRE AS NOMBREUNIDAD
But if I do that I get a:
00000 - "not a GROUP BY expression"
So, I don't know what to do so that subquery executes properly.
What should I change to properly execute query without changing the column name?

Aliases are pretty useful, if you use them. The simplify queries and make them easier to read and maintain. I'd suggest you to do so, as it'll also help query to work because Oracle doesn't know which table you actually meant when you selected those 4 columns - which tables do they belong to?
This is just a guess as I don't know your tables so you'll have to fix it yourself. Also, I literally JOINed tables; try to avoid comma-separating them in FROM clause and doing join in WHERE clause as it is supposed to filter data.
GROUP BY, as already commented, is probably useless. If you wanted to fetch distinct set of values, then use appropriate keyword: distinct.
SELECT DISTINCT n.id_plan,
s.id_curso,
u.nedicion,
u.nombre
FROM asisten n
JOIN alumnos s ON n.cod = s.cod
JOIN unidades u
ON u.idestructura = s.idestructura
AND u.cdundorg = s.cdundorg
WHERE UPPER (TRANSLATE (u.nombre, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'

I managed to solve my problem:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, UNIDADES.NOMBRE AS NOMBREUNIDAD
FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY UNIDADES.NOMBRE,ID_PLAN, ID_CURSO, NEDICION
)

Related

How to write an Open SQL statement with substring in the JOIN ON condition? [duplicate]

I have the following select statement in ABAP:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
INTO corresponding fields of table GT_INSTMUNIC_F
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN EVER AS EV on
MUNIC~POD = EV~VREFER(9).
"where EV~BSTATUS = '14' or EV~BSTATUS = '32'.
My problem with the above statement is that does not recognize the substring/offset operation on the 'ON' clause. If i remove the '(9) then
it recognizes the field, otherwise it gives error:
Field ev~refer is unknown. It is neither in one of the specified tables
nor defined by a "DATA" statement. I have also tried doing something similar in the 'Where' clause, receiving a similar error:
LOOP AT gt_instmunic.
clear wa_gt_instmunic_f.
wa_gt_instmunic_f-mandt = gt_instmunic-mandt.
wa_gt_instmunic_f-bis = gt_instmunic-bis.
wa_gt_instmunic_f-ab = gt_instmunic-ab.
wa_gt_instmunic_f-zzelecdate = gt_instmunic-zzelecdate.
wa_gt_instmunic_f-ZZCERTDATE = gt_instmunic-ZZCERTDATE.
wa_gt_instmunic_f-CONSYEAR = gt_instmunic-CONSYEAR.
wa_gt_instmunic_f-ZDIMO = gt_instmunic-ZDIMO.
wa_gt_instmunic_f-ZZONE_M = gt_instmunic-ZZONE_M.
wa_gt_instmunic_f-ZZONE_T = gt_instmunic-ZZONE_T.
wa_gt_instmunic_f-USAGE_M = gt_instmunic-USAGE_M.
wa_gt_instmunic_f-USAGE_T = gt_instmunic-USAGE_T.
temp_pod = gt_instmunic-pod.
SELECT vrefer
FROM ever
INTO wa_gt_instmunic_f-vrefer
WHERE ( vrefer(9) LIKE temp_pod ). " PROBLEM WITH SUBSTRING
"AND ( BSTATUS = '14' OR BSTATUS = '32' ).
ENDSELECT.
WRITE: / sy-dbcnt.
WRITE: / 'wa is: ', wa_gt_instmunic_f.
WRITE: / 'wa-ever is: ', wa_gt_instmunic_f-vrefer.
APPEND wa_gt_instmunic_f TO gt_instmunic_f.
WRITE: / wa_gt_instmunic_f-vrefer.
ENDLOOP.
itab_size = lines( gt_instmunic_f ).
WRITE: / 'Internal table populated with', itab_size, ' lines'.
The basic task i want to implement is to modify a specific field on one table,
pulling values from another. They have a common field ( pod = vrefer(9) ). Thanks in advance for your time.
If you are on a late enough NetWeaver version, it works on 7.51, you can use the OpenSQL function LEFT or SUBSTRING. Your query would look something like:
SELECT munic~mandt VREFER BIS AB ZZELECDATE ZZCERTDATE CONSYEAR ZDIMO ZZONE_M ZZONE_T USAGE_M USAGE_T M2MC M2MT M2RET EXEMPTMCMT EXEMPRET CHARGEMCMT
FROM ZCI00_INSTMUNIC AS MUNIC
INNER JOIN ever AS ev
ON MUNIC~POD EQ LEFT( EV~VREFER, 9 )
INTO corresponding fields of table GT_INSTMUNIC_F.
Note that the INTO clause needs to move to the end of the command as well.
field(9) is a subset operation that is processed by the ABAP environment and can not be translated into a database-level SQL statement (at least not at the moment, but I'd be surprised if it ever will be). Your best bet is either to select the datasets separately and merge them manually (if both are approximately equally large) or pre-select one and use a FAE/IN clause.
They have a common field ( pod = vrefer(9) )
This is a wrong assumption, because they both are not fields, but a field an other thing.
If you really need to do that task through SQL, I'll suggest you to check native SQL sentences like SUBSTRING and check if you can manage to use them within an EXEC_SQL or (better) the CL_SQL* classes.

ORA-00904: "CTN_QUANTITY": - "%s: invalid identifier"

I try to select rows where CTN_QUANTITY is not empty. My query
select distinct SDB.CTN_MAIN as S_CTN_MAIN,
SDB.SUBS_KEY as S_SUBS_KEY,
SDB.BAN_KEY as S_BAN_KEY,
count(SDB.CTN_MAIN) as CTN_QUANTITY,
FPCN.BAN_KEY as BAN_KEY
from STG_SDB_LOAD SDB, FCT_PREP_CHARGES_N FPCN
where FPCN.business_service_key = 33006
and CTN_QUANTITY <> ''
group by SDB.CTN_MAIN,
SDB.SUBS_KEY,
SDB.BAN_KEY,
FPCN.BAN_KEY;
I get error
ORA-00904. - "%s: invalid identifier"
What do I need to change?
CTN_QUANTITY is defined as the alias for an expression in the SELECT clause. But you want to use it in the WHERE clause. That will not work - WHERE is processed before SELECT. You will have to use count(SDB.CTN_MAIN) in the WHERE clause, not its alias.
Then: The count is always a number, but you are comparing it to the empty string. Which in Oracle is NULL, but regardless, it doesn't make sense. And, the COUNT may be zero, it should never be NULL.
You probably need to write WHERE count(....) <> 0.
Edit: And, of course, as discussed in Comments below... a COUNT filter does not belong in the WHERE clause, it should be in a HAVING clause.
You must use IS NOT NULL in place of <> . Also you cannot use alias in and clause.
select a.* from
(SELECT DISTINCT SDB.CTN_MAIN AS S_CTN_MAIN,
SDB.SUBS_KEY AS S_SUBS_KEY,
SDB.BAN_KEY AS S_BAN_KEY,
COUNT (SDB.CTN_MAIN) AS CTN_QUANTITY,
FPCN.BAN_KEY AS BAN_KEY
FROM STG_SDB_LOAD SDB, FCT_PREP_CHARGES_N FPCN
WHERE FPCN.business_service_key = 33006
GROUP BY SDB.CTN_MAIN,
SDB.SUBS_KEY,
SDB.BAN_KEY,
FPCN.BAN_KEY ) a
WHERE a.CTN_QUANTITY IS NOT NULL
Things to analyze : Is it really useful to have such condition a.CTN_QUANTITY IS NOT NULL. May be you can learn more by reseraching on this and considering #mathguy suggestion.

Missing Keyword ORA-00905

please tell me what is the syntax problem in this query
SELECT sde
FROM TABLE_EW sde , CASE_W spr, DOCUMENT swp
JOIN swp.id, swp.YEAR ON (swp.id = sde.ID_DOCUMENT)
JOIN spr.ID, spr.STATE, spr.NUMBER ON (spr.ID_DOCUMENT = swp.ID)
WHERE sde.IDENT_TABLEEW LIKE '122337456464'
AND swp.YEAR LIKE 2015;
SQLDeleoper point problem to From Line
I think this is the query you meant to write:
SELECT sde.*,swp.id, swp.YEAR,spr.ID, spr.STATE
FROM TABLE_EW sde
JOIN DOCUMENT swp ON (swp.id = sde.ID_DOCUMENT)
JOIN CASE_W spr ON (spr.ID_DOCUMENT = swp.ID)
WHERE sde.IDENT_TABLEEW = '122337456464'
AND swp.YEAR = 2015;
As mentioned in the comments, you have A LOT of errors in your SQL code.
You use implicit and explicit joins together, AVOID the use of implicit joins syntax and use only the proper syntax like my example.
Also, only in the select you can specify the columns you want, I'm guessing what you've been trying to do is
JOIN spr.ID, spr.STATE -> wanted this columns.
You should write them in the select part.
Another problem is the join condition, you either use implicit joins, (from table,table2,table3..) and then the join condition is in the where clause or you use explicit joins and then the condition is in the ON clause. You can't use both!
Another problem is the unnecessary use of LIKE . When comparing to an exact match, use EQUALS sign.

PostgreSQL view won't work - column doesnt exist

Hi I am trying to migrate an access database into postgresql and everything was going well until i tried this view. I am wanting it to create a new column called 'CalculatedHours'. And as Im new to postgresql I am slightly confused. Heres the code that I keep putting into pgAdmin and getting the error...
SELECT "SessionsWithEnrolmentAndGroups"."SessionID",
"Assignments"."Staff",
"SessionsWithEnrolmentAndGroups"."groups",
"SessionsWithEnrolmentAndGroups"."SessionQty",
"SessionsWithEnrolmentAndGroups"."Hours",
"SessionsWithEnrolmentAndGroups"."Weeks",
"Assignments"."Percentage",
"Assignments"."AdditionalHours",
Round((coalesce(("groups"),1)*("SessionQty")*("Hours")*("Weeks")
*("Percentage"))) AS CalculatedHours,
(CalculatedHours)+coalesce(("AdditionalHours"),0) AS "TotalHours"
FROM "SessionsWithEnrolmentAndGroups"
INNER JOIN "Assignments"
ON "SessionsWithEnrolmentAndGroups"."SessionID" = "Assignments"."SessionID";
You cannot access column aliases in the same select where they are defined. I would suggest a subquery:
SELECT t.*,
(CalculatedHours)+coalesce(("AdditionalHours"), 0) AS "TotalHours"
FROM (SELECT eag."SessionID", a, eag."groups", eag."SessionQty",
eag."Hours", eag."Weeks", a."Percentage", a."AdditionalHours",
Round((coalesce(("groups"),1)*("SessionQty")*("Hours")*("Weeks")*("Percentage"))) AS CalculatedHours
FROM "SessionsWithEnrolmentAndGroups" eag INNER JOIN
"Assignments" a
ON eag."SessionID" = a."SessionID"
) t;
Your queries would also be much more readable using table aliases and getting rid of the escape characters (double quotes) unless they are really, really needed.

"FROM keyword not found where expected" (Oracle SQL)

So I have the following query which gives me the error in the title. I have no idea why it's giving me the error.
CREATE OR REPLACE VIEW AUSTRALIANMEDALTALLY
AS
SELECT concat(SG_HOSTCITY, SG_YEAR) as 'ioc_game',
SUM(p.part_gold) as 'gold_medals',
SUM(p.part_silver) as 'silver_medals',
SUM(p.part_bronze) as 'bronze_medals'
FROM GAMES.SUMMERGAMES, GAMES.PARTICIPATION
WHERE COUNTRY_ISOCODE = AUS
GROUP BY COUNTRY_ISOCODE;
Any idea why I'm getting the error?
As well as AUS needing to to be quoted as 'AUS', your aliases either need to be double-quoted:
SELECT concat(SG_HOSTCITY, SG_YEAR) as "ioc_game",
which makes them case-sensitive, or not quoted at all:
SELECT concat(SG_HOSTCITY, SG_YEAR) as ioc_game,
And the same for the other aliases.
You also refer to a p as a table alias but you don't define that - presumably that's for participants - and you aren't defining how the tables are related, so you're doing a cross-join. Seems like you probably have the country in both tables, too, so you need to prefix the references with the table alias.
And you're grouping by a column that isn't in the select-list, which will cause its own error; you need to group by any columns you aren't aggregating.
CREATE OR REPLACE VIEW AUSTRALIANMEDALTALLY
AS
SELECT concat(s.SG_HOSTCITY, s.SG_YEAR) as ioc_game,
SUM(p.part_gold) as gold_medals,
SUM(p.part_silver) as silver_medals,
SUM(p.part_bronze) as bronze_medals
FROM GAMES.SUMMERGAMES s
JOIN GAMES.PARTICIPATION p
ON p.gamesno = s.gamesno
WHERE p.COUNTRY_ISOCODE = 'AUS'
GROUP BY s.SG_HOSTCITY, s.SG_YEAR;
WHERE COUNTRY_ISOCODE = AUS
change to
WHERE COUNTRY_ISOCODE = 'AUS'