remove repeating data from Oracle SQL*Plus query - oracle

I am currently building a database system using oracle 10g. I have developed a query that shows an instance of an event, the customer who requested the event and the staff assigned to the event. My query returns all the correct results but I wanted to know if there is a way to delete the repeating data. For each staff member assigned the result returns the event and customer details. I have attached a picture. Is there a way to return the customer and event details only once in their columns and the corresponding records of staff assigned in that columns?
This my code
COLUMN E_ID FORMAT A7
COLUMN E_NAME FORMAT A20
COLUMN E_STIME FORMAT A30
COLUMN E_FTIME FORMAT A30
COLUMN E_COST FORMAT 9999999.99
COLUMN ET_ET_ID FORMAT A5
COLUMN V_V_ID FORMAT A5
COLUMN C_C_ID FORMAT A5
COLUMN C_FNAME FORMAT A10
COLUMN C_LNAME FORMAT A10
COLUMN S_S_ID FORMAT A5
COLUMN S_FNAME FORMAT A10
COLUMN S_LNAME FORMAT A10
COLUMN S_TASK FORMAT A20
COLUMN S_CNUM FORMAT 9999999
COLUMN ST_DESC FORMAT A20
SELECT E_ID, E_NAME, EVENT.C_C_ID ,C_FNAME, C_LNAME, E_STIME, E_FTIME, E_COST, EVENT.ET_ET_ID, EVENT.V_V_ID, EVENT_STAFF.S_S_ID, S_FNAME, S_LNAME, S_TASK, S_CNUM, STAFFTYPE.ST_DESC
FROM CUSTOMER, EVENT, EVENT_STAFF, STAFF, STAFFTYPE
WHERE EVENT.E_ID = 'E000004'
AND EVENT.C_C_ID = CUSTOMER.C_ID
AND EVENT.E_ID = EVENT_STAFF.E_E_ID
AND EVENT_STAFF.S_S_ID = STAFF.S_ID
AND STAFF.ST_ST_ID = STAFFTYPE.ST_ID;
The results in SQL plus is as shown

That's what the BREAK ON directive in SQL*Plus is for. Add to your COLUMN formatting directives:
BREAK ON E_ID ON E_NAME ...
listing all columns where you want to omit duplicates. Don't forget to use ORDER BY to ensure that records for each event and customer return in the right sequence.

Related

rows and column filter of a table in Power BI

if someone has any ideas about the following problem, please share.
suppose, I have a table, say Table 1, with 100 columns, say column name as A1, A2, ..., A100, and one column values are column names. I have a slicer table where I can select A1, A2, ..., and A100. Now, I want to visualize the table, Table 1, which has columns selected in the slicer table, and those rows which contain slicer-selected values.
For example, I have the following table.
enter image description here
if I select A1 and A2 in the slicer, my return will be as follows
enter image description here
Tried unpivoting columns and adding Attribute in the slicer. but it is filtering only columns, not rows.

to update a column details into base table 2 from column of staging table against the datails of column from base table 1

There's total of three tables involved. one header base table, one material
base table, one staging table.
I have created the staging table with 4 columns, the values will be
updated from csv uploaded, column 1 is batch_no, column 2 is for
attribute.
>header base table(h) has batch_no and batch_id
>material base table(m) has batch_id, attr_m (empty, to be updated)
>staging table(s) has batch_no and attr_s
create table he (BATCH_ID number, BATCH_NO varchar2(30));
create table me (a6 varchar2(30), BATCH_id number);
create table s (batch_no varchar2(30), att varchar2(30));
I want to take values from attr_s and update attr_m against batch_no. How do I do that?
Here's my code, please help me fix this code, it doesn't work
update me
set a6 = (select att
from s where batch_no = (select he.batch_no
from he, s
where he.batch_no=s.batch_no))
error received:
single row subquery return multiple rows.
single row subquery return multiple rows
The update statement is applied to each individual row in ME. Therefore the assignment operation requires one scalar value to be returned from the subquery. Your subquery is returning multiple values, hence the error.
To fix this you need to further restrict the subquery so it returns one row for each row in ME. From your data model the only way to do this is with the BATCH_ID, like so:
update me
set a6 = (select att
from s where batch_no = (select he.batch_no
from he, s
where he.batch_no=s.batch_no
and he.batch_id = me.batch_id))
Such a solution will work providing that there is only one record in S which matches a given permutation of (batch_no, batch_id). As you have provided any sample data I can't verify that the above statement will actually solve your problem.

How to make column name dynamic with spool on ORACLE?

I want to ask, is it possible to use cursor to looping the string value? For example I have 3 title column for my '.txt' report :
COLUMN "DATE" format a15
COLUMN "SUBJECT" format a8
COLUMN "CLASS" format a10
and the result is like:
DATE SUBJECT CLASS
---------------------- ------------------ -----------------
08-Apr-2016 Science 10
08-Apr-2016 Social 11
Now, the column name is still generic or hardcode. Then, I want to return the string value to replace the "DATE", "SUBJECT" and "CLASS" to be dynamic. So just follow to the data that loaded to database.
For example :
I have table that is filled up with these data:
TITLE_COLUMN1 TITLE_COLUMN2 TITLE_COLUMN3
DATE SUBJECT CLASS
I want to select the value from TITLE_COLUMN1, TITLE_COLUMN2, TITLE_COLUMN3 to be on my column name. So what is the better way? Using
Cursor
or using
Variable
?
Thanks for the advise and help.
If you are using SQLPlus and you are speaking about substitution variables, you can make this:
column title_column1 new_value vc1
column title_column2 new_value vc2
column title_column3 new_value vc3
select title_column1,title_column2,title_column3 from test_columns;
select &vc1,&vc2,&vc3 from test_data;

In hive, is there a way to specify between which columns to add a new column to?

I can do
ALTER TABLE table_name ADD COLUMNS (user_id BIGINT)
to add a new column to the end of my non-partition columns and before my partition columns.
Is there any way to add a new column to anywhere among my non-partition columns?
For example, I would like to put this new column user_id as the first column of my table
Yes it is possible to change the location of columns but only after adding it in the table using CHANGE COLUMN
In your case, first add the column user_id to the table with below command:
ALTER TABLE table_name ADD COLUMNS (user_id BIGINT);
Now to make user_id column as the first column in your table use change column with FIRST clause:
ALTER TABLE table_name CHANGE COLUMN user_id user_id BIGINT first;
This will move the user_id column to the first position.
Similarly you can use After instead of first if you want to move the specified column after any other column. Like say, I want to move dob column after user_id column. Then my command would be:
ALTER TABLE table_name CHANGE COLUMN dob dob date AFTER user_id;
Please note that this commands changes metadata only. If you are moving columns, the data must already match the new schema or you must change it to match by some other means.
Ah, here's the explanation for why you listed user_id twice (it's not a type):
// Next change column a1's name to a2, its data type to string, and put it after column b.
ALTER TABLE test_change CHANGE a1 a2 STRING AFTER b;
// The new table's structure is: b int, a2 string, c int.
No, it is not possible.
One solution is to create new table using "CREATE TABLE AS SELECT" approach and drop older one.

Formatting output in Oracle database

I have created a table in an Oracle database as shown below:
create table employee(eid int, enameemp varchar(1), emgrid int);
insert into employee values(101,'A',103);
insert into employee values(102,'B',103);
insert into employee values(103,'C',104);
insert into employee values(104,'D',101);
//Displaying the table contents
select * from employee;
EID E EMGRID
--- - ------
101 A 103
102 B 103
103 C 104
104 D 101
The column name is not displayed completely unlike MySQL. Is there any way I can go about it without increasing the column size of enameemp i.e enameemp varchar(10)?
If you use SQL*Plus, By default the output column size would be colummn size itself. If you wish have a custom size you can always.
Try the below command.
COL COLUMN_NAME FORMAT A<ur column size>
EXAMPLE: COL ENAMEEMP FORMAT A10
More SQL plus options from Oracle Docs

Resources