Invalid column issue - sql

I created a view as below from my existing tables:
Create View LostCase
As
Select L.LNO,Count(*) "NoOfCasesLost"
From L, LWCS,C
Where L.LNO=LWCS.LNO
And LWCS.CASENO=C.CNO
And C.OUTCOME='LOSE'
Group By L.LNO ;
However, whenever I run query as per below , I always get
invalid column name for LostCase.NoOfCasesLost
Select L.LNO,L.LNAME,LostCase.NoOfCasesLost
From L, LostCase
Where L.LNO = LostCase.LNO;
I don't get it why it happens.

If you have specified the column name in the quotation marks, you have to refer it afterwards as:
Select L.LNO,L.LNAME,LostCase."NoOfCasesLost"
From L,LostCase
Where L.LNO=LostCase.LNO;

Related

Postgresql , updating existing table row with another tables data

I am trying to update a null column using another tables value but it doesn't seems to work right. below codes were tried
SET
"Test name "= "Test"(
SELECT Transformertest.Test,Transformertest.TestID
FROM public.Transformertest WHERE TestID='Tes3')
WHERE test2table.Type='Oil Immersed Transformers'
UPDATE
public.test2table
SET
"Test name" = subquery."Test"
FROM
(
SELECT
"Test"
FROM Transformertest WHERE "TestID"='Tes2'
) AS subquery
WHERE
"Type"='Auto Transformer' AND "Phase"='3' AND "Rated Frequency"='60';
enter image description here
don't use space in column name.
Integers don't need to be quoted
See the result here (enter link description here)
what you need to do here (assuming Phase and Rated Frequency are integers)
remove unnecessary "" and spaces on column names
UPDATE
public.test2table
SET
test_name = subquery.Test
FROM
(
SELECT
test
FROM Transformertest WHERE Test_ID='Tes2'
) AS subquery
WHERE
Type='Auto Transformer' AND Phase=3 AND Rated_Frequency=60;
this should be working now

concatenate 2 columns from different tables

Using the below query i am trying to get the db_unique_name from v$database combined with the PRIVILEGE column of DBA_PRIV_AUDIT_OPTS.But i am getting the error 'ORA-00918:column ambiguously defined'.Please suggest.Thanks.
select db_unique_name||':'||PRIVILEGE DB_NAME_PRIV,PROXY_NAME,PRIVILEGE,SUCCESS,FAILURE from
(SELECT d.PRIVILEGE,d.PROXY_NAME,d.PRIVILEGE,d.SUCCESS,d.FAILURE from DBA_PRIV_AUDIT_OPTS d
where d.PRIVILEGE in ('AUDIT SYSTEM','AUDIT ANY','ALTER SYSTEM','GRANT ANY ROLE','GRANT ANY PRIVILEGE','GRANT ANY OBJECT PRIVILEGE','CREATE USER','ALTER USER','DROP USER')),v$database;
You specified d.privilege twice, so just removed extra one:
select db_unique_name||':'||PRIVILEGE DB_NAME_PRIV,PROXY_NAME,PRIVILEGE,SUCCESS,FAILURE from
(SELECT d.PRIVILEGE,d.PROXY_NAMEd.SUCCESS,d.FAILURE from DBA_PRIV_AUDIT_OPTS d
where d.PRIVILEGE in ('AUDIT SYSTEM','AUDIT ANY','ALTER SYSTEM','GRANT ANY ROLE','GRANT ANY PRIVILEGE','GRANT ANY OBJECT PRIVILEGE','CREATE USER','ALTER USER','DROP USER')),v$database;
Also why not simply without subquery?
select db_unique_name||':'||PRIVILEGE DB_NAME_PRIV,PROXY_NAME,PRIVILEGE,SUCCESS,FAILURE
from DBA_PRIV_AUDIT_OPTS d, v$database
where d.PRIVILEGE in (
'AUDIT SYSTEM','AUDIT ANY','ALTER SYSTEM'
,'GRANT ANY ROLE','GRANT ANY PRIVILEGE'
,'GRANT ANY OBJECT PRIVILEGE','CREATE USER'
,'ALTER USER','DROP USER'
);

SQL an opening bracket was expected

Can anyone please look over my code, I keep getting the following errors
An opening bracket was expected. (near "AS" at position 43)
At least one column definition was expected. (near " " at position 42)
Unexpected token. (near ")" at position 695)
However, I did use the opening bracket and close it at the end. I can not see any syntax error with Atom
CREATE TABLE marketin_testDatabase.results AS (
SELECT
/*begin of the variable list for log_link_visit_action*/
llva.`idlink_va`,
llva.`idsite`,
llva.`idvisitor`,
llva.`idvisit`,
/*end of the variable list for log_link_visit_action*/
/*begin of the variable list for log_visit*/
-- lv.`idvisit`, -- duplicate column name
lv.`idsite`,
lv.`idvisitor`,
lv.`last_idlink_va` -- remember to delete the last comma
/*end of the variable list for log_visit*/
FROM marketin_yolopiwik.matomo_log_link_visit_action llva -- create alias for the long table name
LEFT OUTER JOIN marketin_yolopiwik.matomo_log_visit lv
ON lv.idvisit =
llva.idvisit
);
When you use CREATE TABLE syntax, you need to specify all columns and data types in parenthesis, that's why an opening bracket is expected. You can use SELECT INTO statement:
SELECT
llva.`idlink_va`,
llva.`idsite`,
llva.`idvisitor`,
llva.`idvisit`,
-- lv.`idvisit`,
lv.`idsite`,
lv.`idvisitor`,
lv.`last_idlink_va`
INTO marketin_testDatabase.results
FROM marketin_yolopiwik.matomo_log_link_visit_action llva
LEFT OUTER JOIN marketin_yolopiwik.matomo_log_visit lv
ON lv.idvisit = llva.idvisit
Does the table marketin_testDatabase.results already exist? If yes, do you want to add to it or rewrite it?

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.

What to do with the error "Ambiguous Columns Defined"?

This is my sql command:
select INCOME_TYPE_ID,
REGION_CODE,
FIN_YEAR_CODE,
PORTION_AMOUNT
from INCOME.INCOME_TYPE,
COMMON.REGION,
INCOME.RECEIVE_DOC_PORTION,
INCOME.ASSESS_ORDER_ITEM,
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC,
ACCOUNTING.VOUCHER_ITEM,
ACCOUNTING.VOUCHER,
ACCOUNTING.FIN_YEAR
where INCOME.RECEIVE_DOC_PORTION.ASSESS_ORDER_ITEM_ID = INCOME.ASSESS_ORDER_ITEM.ASSESS_ORDER_ITEM_ID
and INCOME.ASSESS_ORDER_ITEM.INCOME_TYPE_ID=INCOME.INCOME_TYPE.INCOME_TYPE_ID
and INCOME.RECEIVE_DOC_PORTION.RECEIVE_DOC_PORTION_ID = ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.RECEIVE_DOC_PORTION_ID
and ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.VOUCHER_ITEM_ID = ACCOUNTING.VOUCHER_ITEM.VOUCHER_ITEM_ID
and ACCOUNTING.VOUCHER_ITEM.VOUCHER_ID = ACCOUNTING.VOUCHER.VOUCHER_ID
and ACCOUNTING.VOUCHER.REGION_CODE = COMMON.REGION.REGION_CODE
and ACCOUNTING.VOUCHER.FIN_YEAR_CODE = ACCOUNTING.FIN_YEAR.FIN_YEAR_CODE
and I got this error:
Ambiguous Columns Defined
I'm Using SQL Developer as Oracle client.
Apparently one (or more) column names in your select list exists in more than one table of the FROM list.
You need to prefix every column in the SELECT list with the table it's coming from (it's also a good practice to always do that, regardless of the fact if they are ambigous)
Mention name of table before every column in select query.
Ambiguous column means that you have more than one column with the same name in one of the SELECT statements.
Try this instead, prefgixing all selected columns with their fully qualified names (as you have done elsewhere in your SELECT):
select INCOME.INCOME_TYPE.INCOME_TYPE_ID,
COMMON.REGION.REGION_CODE,
ACCOUNTING.FIN_YEAR.FIN_YEAR_CODE,
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.PORTION_AMOUNT
from INCOME.INCOME_TYPE,
COMMON.REGION,
INCOME.RECEIVE_DOC_PORTION,
INCOME.ASSESS_ORDER_ITEM,
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC,
ACCOUNTING.VOUCHER_ITEM,
ACCOUNTING.VOUCHER,
ACCOUNTING.FIN_YEAR
where INCOME.RECEIVE_DOC_PORTION.ASSESS_ORDER_ITEM_ID = INCOME.ASSESS_ORDER_ITEM.ASSESS_ORDER_ITEM_ID
and INCOME.ASSESS_ORDER_ITEM.INCOME_TYPE_ID = INCOME.INCOME_TYPE.INCOME_TYPE_ID
and INCOME.RECEIVE_DOC_PORTION.RECEIVE_DOC_PORTION_ID = ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.RECEIVE_DOC_PORTION_ID
and ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.VOUCHER_ITEM_ID = ACCOUNTING.VOUCHER_ITEM.VOUCHER_ITEM_ID
and ACCOUNTING.VOUCHER_ITEM.VOUCHER_ID = ACCOUNTING.VOUCHER.VOUCHER_ID
and ACCOUNTING.VOUCHER.REGION_CODE = COMMON.REGION.REGION_CODE
and ACCOUNTING.VOUCHER.FIN_YEAR_CODE = ACCOUNTING.FIN_YEAR.FIN_YEAR_CODE
I had to guess the filly qualified name for
ACCOUNTING.VOUCHER_ITEM_RECEIVE_DOC.PORTION_AMOUNT
It might be
INCOME.RECEIVE_DOC_PORTION.PORTION_AMOUNT
But you should be able to resolve that easily.
Hope it helps...