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

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.

Related

Ambigously defined column in a subquery

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
)

ORA-00904: Invalid column name on a correlated subquery

I'm having a problem correlating a subquery on Oracle 8i
That subquery gives me an ORA-00904: Invalid column name, I don't understand why. Shouldn't it work?
SELECT
HIST_FA.HIFA_PLAN_CODIGO AS FE_CODIGO_PLANTEL,
HIST_FA.HIFA_NUMERO AS FE_NUMERO_CONTROL,
HIST_FA.HIFA_FECHA AS FE_FECHA_HORA_EMISION,
HIST_FA.HIFA_DEST_CLIE_CODIGO AS FE_CODIGO_CLIENTE,
-- . . . a bunch of other columns . . .
FROM
VFA_HIST_FA HIST_FA,
VFA_HIST_ITEMS_FA HIST_ITEMS_FA,
(
SELECT
HIRF_HIIF_HIFA_NUMERO AS FE_NUMERO_CONTROL,
SUM(DECODE(HIRF_RETE_CODIGO, 0, HIRF_MONTO)) AS FE_MONTO_ENTREGADO,
SUM(DECODE(HIRF_RETE_CODIGO, 5, HIRF_MONTO)) AS FE_IMPUESTO_UNICO,
SUM(DECODE(HIRF_RETE_CODIGO, 6, HIRF_MONTO)) AS FE_MARGEN_COMERCIALIZACION
FROM
VFA_HIST_ITEMS_RETENC_FA
WHERE
HIRF_HIIF_HIFA_NUMERO = HIST_FA.HIFA_NUMERO
GROUP BY
HIRF_HIIF_HIFA_NUMERO
) PP
WHERE
HIST_FA.HIFA_NUMERO = HIST_ITEMS_FA.HIIF_HIFA_NUMERO;
Of course, I can do this at the main WHERE, but then it scans all rows in the inner one.
HIST_FA.HIFA_NUMERO = PP.FE_NUMERO_CONTROL
So, not an option, since the query becomes everlasting.
Best regards!
Change the Alias Name for the column name
HIRF_HIIF_HIFA_NUMERO AS FE_NUMERO_CONTROL
to some other name and try again.

SQL Hive subquery error

I have the query below
set hive.cli.print.header=true;
set hive.query.max.partition=1000;
set hive.mapred.mode=unstrict;
SELECT
dim_lookup("accounts",name,"account_id") = '28016' as company,
dim_lookup("campaigns",name,"campaign_id") in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215) as campaign_name,
case when is_click_through=0 then "PV" else "PC" end as conv_type,
(SELECT COUNT(1) FROM impressions WHERE ad_info[2] in (117649,112311,112319,112313,107799,110743,112559,112557,105191,105231,107377,108675,106587,107325,110671,107329,107181,106565,105123,106569,106579,110835,105127,105243,107185,105211,105215)) AS impressions
FROM actions
WHERE
data_date>='20170101'
AND data_date<='20171231'
AND conversion_action_id in (20769223,20769214,20769219,20764929,20764932,20764935,20769215,20769216,20764919,20769218,20769217,20769220,20769222)
GROUP BY conv_type
When I execute it I get an error
ERROR ql.Driver: FAILED: ParseException line 8:1 cannot recognize input near 'SELECT' 'COUNT' '(' in expression specification
I am trying to fetch each count of impression for a specified conversion_action_id. What could be the error in my query? Thanks for the help.
FYI: ad_info[2] and campaign_id are the same.
The problem is quite clear, you have a subquery inside your SELECT.
That is not how this works.
Unfortunately the exact solution is not that clear, as it I am not completely sure what you want, but here is some general advice:
Write your subquery, test it and make sure it is ok
Rather than putting it in your SELECT part, put it in your FROM part, and (as always) SELECt from the FROM
Just think of your subquery output as an other table that can be used in the from statement, and which needs to be combined (JOIN, UNION?) with other tables in the from statement.

Why isn't my simple unmatched query not working?

SELECT [Denial Query].BlPerFct_RcvGrpBlPerId, ATBDetailDaily.ClaimNo,
ATBDetailDaily.EncRpt_EncStsMne, ATBDetailDaily.Payer,
ATBDetailDaily.PayerHealthPlan, ATBDetailDaily.EncStpDate,
ATBDetailDaily.BlPerFct_TotBalAmt
FROM ATBDetailDaily
LEFT JOIN [Denial Query] ON
ATBDetailDaily.[BlPerFct_RcvGrpBlPerId] = [Denial Query].[BlPerFct_RcvGrpBlPerId]
WHERE ((([Denial Query].BlPerFct_RcvGrpBlPerId)="IsNull")
AND ((ATBDetailDaily.Payer)<>"Guar - Pers"));
I’m trying to get the BlPerFct_RcvGrpBlPerId rows that are in the ATBDetailDaily, that are not in the Denial Query.... I'm just trying to extract all rows out of the ATBDetailDaily that are not captured in the Denial Query using a unique identifier.
Your WHERE clause should read:
WHERE [Denial Query].BlPerFct_RcvGrpBlPerId Is Null
Currently you're asking it to match the literal string "IsNull", but what you need to do is check if is exists at all, which is what the Is Null comparison does.

Error in select statement, with union all in a subquery

In Oracle 11g, I came across an error for a query and cannot figure why it is erroring on me. Here is the query:
select
main_data.issue_number,
main_data.transaction_number
from
(
select
p1.payment_date,
p1.media_number,
p1.payment_amount,
p1.issue_number,
p1.advice_na_number,
name.name_address_line_1,
name.name_address_line_2,
name.name_address_line_3,
name.name_address_line_4,
name.name_address_line_5,
name.name_address_line_6,
name.name_address_line_7,
name.name_address_city,
name.state_code,
name.address_country_code,
name.zip_code,
name.tax_id_number,
p1.output_tx_number_prin,
p1.output_tx_number_int,
'' as "transaction_number",
p1header.check_account_number
from
p1
left join name on p1.name_address_number = name.name_address_number
left join p1header on p1.issue_number = p1header.issue_number
UNION ALL
select
check.date_of_payment,
check.media_number,
check.payment_amount,
check.issue_number,
check.payee_na_number,
name.name_address_line_1,
name.name_address_line_2,
name.name_address_line_3,
name.name_address_line_4,
name.name_address_line_5,
name.name_address_line_6,
name.name_address_line_7,
name.name_address_city,
name.state_code,
name.address_country_code,
name.zip_code,
name.tax_id_number,
'' as "output_tx_number_prin",
'' as "output_tx_number_int",
check.transaction_number,
check.dda_number as "check_account_number"
from check
left join name on check.payee_na_number = name.name_address_number
) main_data
Selecting individual fields like above will give me an "invalid identifier error". If I do select * then it gives me back the data without any error. What am I doing wrong here? Thank you.
The old quoted identifier problem... see point 9 in the database object naming documentation, and note that Oracle does not recommend using quoted identifiers.
You've put your column alias as lower case inside double-quotes. That means that any references to it also have to be quoted and exactly match the case. So this would work:
select
main_data.issue_number,
main_data."transaction_number"
from
...
But unless you have a burning need to have that alias like that - and I doubt you do as all the identifier names from the actual table columns are not quoted - it would be simpler to remove the double quotes from the inner selects:
select
main_data.issue_number,
main_data.transaction_number
from
(
select
...
'' as transaction_number,
p1header.check_account_number
...
UNION ALL
select
...
'' as output_tx_number_prin,
'' as output_tx_number_int,
check.transaction_number,
check.dda_number as check_account_number
...
You don't actually need to alias the columns in the second branch of the union; the column identifiers will all be taken from the first branch.