column statement not working in Oracle - oracle

This is something easy - I know it. Been working for a couple of hours on it, googled it, no luck. Apologize in advance.
For some reason I can NOT alter the column heading using the column statement in Oracle.
Here is what I am running:
column clear;
column PeopleSoftTerms.LName HEADING 'PeopleSoft|Last Name';
SELECT PeopleSoftTerms.LName,
PeopleSoftTerms.FName as "PeopleSoft First Name",
TO_CHAR(PeopleSoftTerms.termdate, 'YYYY/MM/DD') as "Termination Date",
PeopleSoftTerms.ticket as "Maximo Ticket",
PeopleSoftTerms.LANID as "ID Listed in PeopleSoft",
Domain.logonid as "Active Directory ID", Domain.DisplayName, Domain.status
FROM PeopleSoftTerms
INNER JOIN Domain ON PeopleSoftTerms.LName = Domain.LName AND
PeopleSoftTerms.FName = Domain.FName;
I am at a loss here.

COLUMN commands take the column name only, not the fully qualified column name.
core> column event heading "This will work"
core> column v$session.event heading "This will not"
core> select v$session.event from v$session where rownum=1;
This will work
----------------------------------------------------------------
VKTM Logical Idle Wait
(This is in SQLPlus. I do not believe COLUMN is supported at all by SQL Developer.)

What are you trying to do with the "column clear" statement? If you're trying to clear formatting for a column, then you need to add the field name:
column field_name clear
If you're just trying to clear the column definitions, then you should reverse and try:
clear columns
Also, if the "column" statements are a part of the SELECT statement, then you shouldn't need to terminate them with a semi-colon.
column PeopleSoftTerms.LName HEADING 'PeopleSoft|Last Name'
SELECT PeopleSoftTerms.LName,
...
FROM PeopleSoftTerms
INNER JOIN Domain ON PeopleSoftTerms.LName = Domain.LName AND
PeopleSoftTerms.FName = Domain.FName;
Give some of that a try, and see if it helps.

Try to add the "" on the blank character of last name:
column PeopleSoftTerms.LName HEADING 'PeopleSoft|Last" "Name'

Related

BIRT allow user to dynamically select report's columns

I want to add an option for the user when creating the report to select the columns that the report will show. See the attached image below on how it is suppose to look.
Is there a way to do that?
I don't know about the parameter dialog, but assuming that your column names are in an array.
You can have an SQL query with all possible column names
(probably you should use special comments to mark the beginning and end of the select list).
E.g.
select
'X' as dummy
-- BEGIN COLS
, column1
, column2
...
-- END COLS
from ...
where ...
order by ...
Then, in the beforeOpen event of the query, you can access and modify the query with this.queryText (IIRC) and remove all those lines ("," + columnname) in the marked part for which columnname is not contained in the array.

PLSQL UPDATE OF with Join

I'm trying to create a PLSQL statement that updates the money in the inventory of the characters with the money from all the creatures they've fought
The following is the code I've been trying:
DECLARE
inv_money character.money%TYPE;
CURSOR updmoney IS
SELECT *
FROM character
WHERE id IN (SELECT character_id FROM inst_creature)
FOR UPDATE OF money;
BEGIN
FOR v_character IN updmoney LOOP
SELECT inst_creature.money
INTO inv_money
FROM inst_creature,character
WHERE inst_creature.character_id = character.ID;
UPDATE character
SET money = money+inv_money
WHERE CURRENT OF updmoney;
END LOOP;
COMMIT;
END;
There is a character_id in the inst_creature table which is used to define the character that fought that creature.
I'm getting the error
ORA-01422: exact fetch returns more than requested number of rows.
I've been trying to fix it by using Google to get a solution but nothing has been working so far. Any thoughts?
What happens when a character has killed more than one monster? ORA-01422, that's what. The SELECT ... INTO syntax populates a scalar value, that is one row. When the query returns more than one row PL/SQL hurls ORA-01422.
" Is there any solution to this for it to work?"
The easiest way is to fix the query so it returns one row. As you're just adding money to the character's trove, you can use an aggregate:
SELECT sum(inst_creature.money)
INTO inv_money
FROM inst_creature,character
WHERE inst_creature.character_id = character.ID;
So you get one row per character, and one update per character.

Strange Behavior of oracle query

I am not an oracle expert. I faced a very strange problem but do not know why this occur.
My query is
SELECT hc.id, hc.owner_name, hc.national_id, hc.phone_no, hc.location, hc.status, hc.expiry_status, od.office_title AS issuer, hc.create_date, hc.email, hc.LATTITUDE, hc.LONGITUDE, hc.HASAD_NO, hc.NUMBERATION, hc.BREEDING_TYPE, hc.PROGENY, hc.office_id, hc.issuer_id, hc.expiry_status, hc.status FROM health_cards hc
LEFT JOIN office_details od ON od.office_id = hc.issuer_id AND od.lang = :lang
WHERE hc.id = :search_data_num OR hc.national_id = :search_data_num or hc.phone_no = :search_data_num OR hc.owner_name LIKE :search_data ORDER BY hc.create_date DESC, hc.id desc OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY
When i m running this query i m getting following error
ORA-00918: column ambiguously defined
00918. 00000 - "column ambiguously defined"
But if i remove OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY from my query it work perfectly.
I want to know the reason why this query not working with offset statement.
You have repeated column hc.status.
`select 1 as "A" as "A" from dual` - execute OK;
`select * from (select 1 as "A"
, 2 as "A"
from dual);` - ORA-00918: column ambiguously defined
If you and offset, oracle probably does something similar.
You have the column hc.expiry_status twice in your select list.
The problem is that we allow this in a select list, but not within an inline view. When you add the row limiting clause, Oracle transforms the query and the transformation uses an inline view. There is a bug, 13687511 which is marked as fixed.
Meanwhile, the workaround is to either not select it twice, or alias the column(s).

How can i limit the output of select statement for a varchar column in Oracle?

i have a table in which i store the information (product id and description ) of all my products, description column is of type VarChar2(200). i want to format the output of this column in select statement to only result me specific part of output string. E.G Here is my simple select statement:
Select PRODUCTId, PRODUCT_DESC From ProductTable Order By PRODUCTId Desc;
this statement result me the output as:
ProductId Product_Desc
1 Oxford English-Oxford-Oxford Press-Textbook
now i want only the specific part of the output result from product_description column. i have already checked Trim() function but that did not helped me. can someone help me?
A substring function may help.
SELECT SUBSTR('ABCDEFG',3,4) "Substring"
FROM DUAL;
You can use SUBSTR() function. You can provide start and end position for the product_desc column.
The query should be like:
Select product_id,substr(product_desc,2,4) from producttable;
Here you'll get 4 chars from the second one.

Compare date to month-year in Postgres/Ruby

I have a date column in my table and I would like to 'filter'/select out items after a certain year-month. So if I have data from 2010 on, I have a user input that specifies '2011-10' as the 'earliest date' they want to see data from.
My current SQL looks like this:
select round(sum(amount), 2) as amount,
date_part('month', date) as month
from receipts join items
on receipts.item = items.item
where items.expense = ?
and date_part('year', date)>=2014
and funding = 'General'
group by items.expense, month, items.order
order by items.order desc;
In the second part of the 'where', instead of doing year >= 2014, I want to do something like to_char(date, 'YY-MMMM') >= ? as another parameter and then pass in '2011-10'. However, when I do this:
costsSql = "select round(sum(amount), 2) as amount,
to_char(date, 'YY-MMMM') as year_month
from receipts join items
on receipts.item = items.item
where items.expense = ?
and year_month >= ?
and funding = 'General'
group by items.expense, year_month, items.order
order by items.order desc"
and call that with my two params, I get a postgres error: PG::UndefinedColumn: ERROR: column "year_month" does not exist.
Edit: I converted my YYYY-MM string into a date and passed that in as my param instead and it's working. But I still don't understand why I get the 'column does not exist' error after I created that column in the select clause - can someone explain? Can columns created like that not be used in where clauses?
This error: column "year_month" does not exist happens because year_month is an alias defined the SELECT-list and such aliases can't be refered to in the WHERE clause.
This is based on the fact that the SELECT-list is evaluated after the WHERE clause, see for example: Column alias in where clause? for an explanation from PG developers.
Some databases allow it nonetheless, others don't, and PostgreSQL doesn't. It's one of the many portability hazards between SQL engines.
In the case of the query shown in the question, you don't even need the to_char in the WHERE clause anyway, because as mentioned in the first comment, a direct comparison with a date is simpler and more efficient too.
When a query has a complex expression in the SELECT-list and repeating it in the WHERE clause looks wrong, sometimes it might be refactored to move the expression into a sub-select or a WITH clause at the beginning of the query.

Resources