PL/SQL :: SQL To Sum A Column While Avoiding Repeating Values
Jan 31, 2013
I have these records in a table:
CODE AMOUNT DESCRIPTION
AAA 5 five dollars for pizza
AAA 2 two dollars for tips
AAA 1 one dollar for dogsitting
BBB 6 six dollars for babysitting
BBB 1 one dollar for tips
My goal is to list all records, "grouping" by code, with sum(amount), but the final display has to show all descriptions, one for row, avoiding to repeat the "CODE" column and "sum(AMOUNT)" column.The result should be like this:
CODE SUM(AMOUNT) DESCRIPTION
AAA 8 five dollars for pizza
two dollars for tips
one dollar for dogsitting
BBB 7 six dollars for babysitting
one dollar for tips
That is, the "CODE" is displayed only the first row, with its sum of "amount".I think I have to use the analytics functions, but I was a little stuck.
I am trying to come up with a sql select statement that provides all rows for employees with 2 or more cities.
with sample_table as ( select 'John' name,'city' ValueType,'Toronto' Value from dual union all select 'John' name,'city' ValueType,'Vancouver' Value from dual union all select 'Susan' name,'city' ValueType,'Toronto' Value from dual union all select 'Susan' name,'city' ValueType,'Seattle' Value from dual union all select 'Susan' name,'age' ValueType,30 Value from dual union all select 'Susan' name,'city' ValueType,'Atlanta' Value from dual union all
[Code]...
NAME VALUETYPE VALUE ----------- ------------- ------------ John City Toronto John City Vancouver Susan City Toronto Susan City Seattle Susan Age 30 Susan City Atlanta David City Chicago David age 35 David Status married David City Dallas
The above code is just to describe the sample table and the desired result set. Please note that Mary is not on the result set since she has no city assigned to her. Also Julia is not on the result set since she only has one city assigned to her. The others are there because they had at least 2 cities assigned to them.
I need the sql syntax that would return this result set.
if the same name repeating it should to append with _1 and _2 until same name reached.
select 'fname' name from dual union all select 'lname' name from dual union all select 'email' name from dual union all select 'fname' name from dual union all select 'fname' name from dualmy output should be like below...
I am creating a report which generates the data to a PDF/CSV based on users Input.When I create the report using Report Builder and execute the report,Delimited data appears in a PDF layout.
But if I add the below code in After Parameter Form trigger,the column headers keeps repeating with the data.
function AfterPForm return boolean is begin IF :P_DESTYPE = 'CSV' THEN :MODE :='DEFAULT'; :DESFORMAT:='DELIMITED'; Elsif :P_DESTYPE = 'PDF' THEN :DESTYPE := 'CACHE'; :DESFORMAT:='PDF'; END IF; return (TRUE); end; [code]....
But the PDF output seems fine where the header is shown only once.I could see many posts regarding this in Oracle reports 6i wherein delimited_hdr=no is used in Command Line but I would like to know the work around in Oracle reports 10g.
My scenario is to insert values into 'out' column by comparing 's' and 'IP' columns of temp table.The exact situation is at first need to go to ip column,take a value and then go to source column and check for the same value of ip which is taken previously.Then after corresponding ip of that source column should be inserted back in previous source column.
The situation is marked clearly in file which i am attaching with '--' comments at respective places.I am also pasting the code which i tried out,unfortunately it is giving error as exact fetch returns more than requested number of rows since there are duplicates in the table.I tried it using nested for loops.Also implemented using rowid,but it didnt work.
fixing the errors or if there is any new logic that can be implemented.
DECLARE i_e NUMBER(10); BEGIN FOR cur_1 IN(SELECT IP from temp where IP IS NOT NULL) LOOP FOR cur_2 IN(SELECT IP from temp where s=cur_1.IP)
I've got situation where one of the procedure causing insert into table rows but some of them are not needed Temporary I need to disable this and exclude some type of data being inserted by changing procedure.
I wouldn't want to change the procedure is there a way to write a trigger that would not allow to insert a row into a table if condition is met? Something of a logic:
CREATE OR REPLACE TRIGGER avoid_insert BEFORE (AFTER?) INSERT if entity_id = 123 DO NOT DO THE INSERT (DO NOT DO ANYTHING)
CREATE TABLE A(EMP_ID NUMBER, EMP_NAME VARCHAR2(100)) CREATE TABLE B(EMP_ID NUMBER, EMP_ATT1 VARCHAR2(10), EMP_ATT2 VARCHAR2(10)) INSERT INTO A VALUES(1, 'ONE'); INSERT INTO A VALUES(2, 'TWO'); INSERT INTO A VALUES(3, 'THREE');
[Code]....
This query returns all the matching row of A and B
SELECT A.EMP_ID, A.EMP_NAME, B.EMP_ATT1, B.EMP_ATT2 FROM A INNER JOIN B ON A.EMP_ID=B.EMP_ID
The output for this shows:
EMP_ID EMP_NAME EMP_ATT1 EMP_ATT2 1 ONE 1ATT1 1ATT2 2 TWO 2ATT1 2ATT2 2 TWO 2ATT1.1 2ATT2.1 3 THREE 3ATT1 3ATT2
The requirement is to avoid duplicate rows even if matched:
EMP_ID EMP_NAME EMP_ATT1 EMP_ATT2 1 ONE 1ATT1 1ATT2 2 TWO 2ATT1 2ATT2 3 THREE 3ATT1 3ATT2
From two given tables, how do you fetch the values from two columns using values from one column(get values from col.A if col.A is not null and get values from col.B if col.A is null)?
Given two tables in a 1:N relationship, I want to retrieve, for each parent row, any child row which either satisfies some condition cond1 or, if no child rows matching cond1 exist, which satisfies another condition cond2.
I can easily write such a query using NOT EXISTS, but I am forced to repeat the condition cond1. This is inconvenient, as the repeated condition is quite long (involving other tables as well), and I would like to write it only once.
For example (using the HR schema included in Oracle 10g XE): I want to retrieve, for each department, all the "new" employees (hired after a certain date) or, if no "new" employees exist, the department manager.
On my APEX page i have region which has sql query as source and it displays as HTML table the query result to the user.
I want to display addinonal column with a hyperlink inside, and that hyperlink would have CGI/URL-parameters which contains the other values of the HTML row.
So, let's say my APEX region queryes columns as "select c1, c2, c3, c4 ..." and displays out values "V1, V2, V3, V4" then i want to have addional output column with such hyperlink:
a href="f?p=100:7:13467554876288::NO::c1,c2,c3,c4:v1,v2,v3,v4">My link column with CGI-parameters</aHow can i create such hyperlink?
The overall idea is that the link would forward to a page which loads those values "v1,v2,v3,v4" into form fields and user can proceed from there.
I want to pass multiple column values of a row in an interactive report page to hidden items in another page through column link. And I did it successfully. However, I found I need to pass more than 3 columns of a row in this report, while a column link only permits me to pass 3 column value at most. Is there anyway that I can pass more column values to hidden items in another page?
We are on oracle 10.2.0.4 on Solaris 10 and have a perf. issue with a bind variable using query. The query is in java application. I want to test its performance when the query doesn't use bind variable and instead uses the passed value as literal. How can it be done?
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7839 KING PRESIDENT 17-NOV-81 5000 10
1 row selected.
But the statements will be taken as similar statements by oracle (due to :vn). Now I want oracle to take it as literal and the change for this has to be done in java code in my actual scenario which has a different query (but conceptually it uses bind variable and I want it to use passed value as literal). How can it be done?
How to Insert the data in Repeating Tables. I mean Suppose One Student has many Addresses like home,office,permanent etc.
There is one column in this table sequence_no. while Inserting a record how to insert this sequence no I don't know It maintains unique sequence no for each student.
If student has 3 addresses then Its seq no is 3.
I am inserting through a procedure where multiple students data is to be inserted.
AID UCD U_TXT UDATE PID 116 1 Req Documents 01-OCT-2011 100 116 2 AGG APPR 01-OCT-2011 101 116 3 Docs received 02-oct-2011 102 116 1 Tmp received 02-oct-2011 103 117 2 Notice sent 03-oct-2011 104
UCD - We have total 19 codes (1 to 19), each can have multiple rows for one AID.. like 1 repeated twice for AID 116. PID - Primary id (Primary key column)
Output I am looking -------------------- AID COL1 COL1_TXT COL1_DATE COL2 COL2_TXT COL2_DATE..ETC 116 1 'Tmp received' 02-oct-2011 2 AGG APPR 01-OCT-2011 117 2 Notice sent 03-oct-2011
If the same UCD repeated multiple times then we should get the max(PID) record for that UCD and for that AID
I tried with group by AID,PID. but couldn't bring the rows to columns. I have attached the script with the post
I am trying to delete duplicate or repeating words in a string using regular expressions (regex) and an Oracle database.I have Googled quite a bit on this topic and was unable to track down something that fit the bill or remotely came close to paying the tip.
A pattern would be: word word start with newThe final string should look like this: word start with new Basically all the strings begin with the two words repeating.
During the Report Runtime the Each Page Contain 55 Lines. Their as Lot of Item Groups are available in the Report Each Items Group have Maximum 10 Lines in the Report with Summary Contain in the Repeating Frame
My Requirement is During the Report Preview if the Report Not Fitted in the Page then Whole Item Group is Require to Skip to Next Page.
I have a graph inside a repeating frame, but every time a run the report, it doesn't bring me all de graph, generally skip the last one, for example i show one graph per location but never show me the last ones. What can i do? If I ask 4 location, it bring me 3, if I ask 5 it bring me 4.