Wise Dept sum sal differece
HelloWe have sal & deptno columns in the emp table. How can I view the difference in the amount of wages on dept wise
dept20 - dept10 - amount wage difference
dept20 - dept30 - amount wage difference
I want to display the output in the form of two columns.
Please help me... !!
Concerning
Evelyne
Hello
Please try this... !!
select d20_sal - d10_sal as d20_10_diff,
d20_sal - d30_sal as d20_30_diff
from (
select sum(case when deptno=10 then sal end) as d10_sal,
sum(case when deptno=20 then sal end) as d20_sal,
sum(case when deptno=30 then sal end) as d30_sal
from emp
) totals_by_dept
Concerning
KPR
Tags: Database
Similar Questions
-
The NON-NULL DAMOUNT (col 1500,2000,3000 value) value be need not in 'Running Total'my tables are CREATE TABLE LHEADER (HDT DATE, HSR NUMBER, HCODE VARCHAR2(6), HAMOUNT NUMBER); INSERT INTO LHEADER VALUES (TO_DATE('01012009','DD/MM/YYYY'),1,101,5000); INSERT INTO LHEADER VALUES (TO_DATE('01022009','DD/MM/YYYY'),2,102,3000); INSERT INTO LHEADER VALUES (TO_DATE('01032009','DD/MM/YYYY'),3,103,6000); CREATE TABLE DDETAIL (DDT DATE, DHSR NUMBER, DHCODE VARCHAR2(6), DAMOUNT NUMBER, D1 NUMBER, D2 NUMBER, D3 NUMBER); INSERT INTO DDETAIL VALUES (TO_DATE('31012009','DD/MM/YYYY'),1,101,1500,NULL,NULL,10); INSERT INTO DDETAIL VALUES (TO_DATE('31012009','DD/MM/YYYY'),1,101,NULL,20,15,20); INSERT INTO DDETAIL VALUES (TO_DATE('28022009','DD/MM/YYYY'),1,101,NULL,20,12,20); INSERT INTO DDETAIL VALUES (TO_DATE('31032009','DD/MM/YYYY'),1,101,NULL,20,16,20); INSERT INTO DDETAIL VALUES (TO_DATE('30042009','DD/MM/YYYY'),1,101,NULL,20,20,20); INSERT INTO DDETAIL VALUES (TO_DATE('28022009','DD/MM/YYYY'),2,102,2000,NULL,NULL,15); INSERT INTO DDETAIL VALUES (TO_DATE('28022009','DD/MM/YYYY'),2,102,NULL,12,10,15); INSERT INTO DDETAIL VALUES (TO_DATE('31032009','DD/MM/YYYY'),2,102,NULL,12,15,8); INSERT INTO DDETAIL VALUES (TO_DATE('30042009','DD/MM/YYYY'),2,102,NULL,12,12,15); INSERT INTO DDETAIL VALUES (TO_DATE('31012009','DD/MM/YYYY'),3,103,NULL,30,20,25); INSERT INTO DDETAIL VALUES (TO_DATE('28022009','DD/MM/YYYY'),3,103,NULL,30,20,25); INSERT INTO DDETAIL VALUES (TO_DATE('31032009','DD/MM/YYYY'),3,103,NULL,30,20,25); INSERT INTO DDETAIL VALUES (TO_DATE('30042009','DD/MM/YYYY'),3,103,3000,NULL,NULL,25); INSERT INTO DDETAIL VALUES (TO_DATE('30042009','DD/MM/YYYY'),3,103,NULL,30,20,25); i want the output to be; DDT DHSR DHCODE HAMOUNT D1 D2 D3 RUN_TOT 31/01/2009 1 101 1500 10 31/01/2009 1 101 5000 20 15 20 4945 31/01/2009 3 103 6000 30 20 25 5925 28/02/2009 1 101 4945 20 12 20 4893 28/02/2009 2 102 2000 15 28/02/2009 2 102 3000 12 10 15 2963 28/02/2009 3 103 5925 30 20 25 5850 31/03/2009 1 101 4893 20 12 20 4841 31/03/2009 2 102 2963 12 10 15 2926 31/03/2009 3 103 5850 30 20 25 5775 30/04/2009 1 101 4841 20 12 20 4789 30/04/2009 2 102 2926 12 10 15 2889 30/04/2009 3 103 3000 20 30/04/2009 3 103 5775 30 20 25 5700
I'm not able to separarate Prime, these value NON-NULL, then UNiON lines he alongwith operation total lines
and sorting it wise of DDT.
Please help with the code.
TYVM in adv.
Kind regards.
Published by: user10967485 on January 7, 2010 08:15Hello
As Centinul said, thank you for the samples, including in a usable form data!
The results are really what you want from these data? My results are a little different. I'm guessing that you have published the wrong version of the data or the results, but if not, please explain the differences.You can calculate run_tot using the analytic function SUM. The column that you want to display the amount is actually calculated from run_tot, not the other way around. (Otherwise, row_tot and both would be based on previous versions of the other, and to do this in SQL requires a query CONNECT BY or MODEL, that is even more complex.)
WITH got_run_tot AS ( SELECT d.ddt , d.dhsr , d.dhcode , NVL ( d.damount , h.hamount ) AS amount , d.d1 , d.d2 , d.d3 , h.hamount - CASE WHEN d1 + d2 + d3 IS NULL THEN NULL ELSE SUM ( d.d1 + d.d2 + d.d3 ) OVER ( PARTITION BY d.dhcode , d.dhsr ORDER BY d.ddt ) END AS run_tot FROM ddetail d JOIN lheader h ON d.dhcode = h.hcode AND d.dhsr = h.hsr ) SELECT ddt , dhsr , dhcode , NVL ( CASE WHEN d1 + d2 + d3 IS NULL THEN LAG (run_tot) OVER ( PARTITION BY dhcode , dhsr ORDER BY ddt ) END , amount ) AS amount , d1 , d2 , d3 , run_tot FROM got_run_tot ORDER BY ddt , dhcode , d1 + d2 + d3 NULLS FIRST ;
My results:
DDT DHSR DHCODE AMOUNT D1 D2 D3 RUN_TOT --------- ---------- ------ ---------- --- --- --- ---------- 31-JAN-09 1 101 1500 10 31-JAN-09 1 101 5000 20 15 20 4945 31-JAN-09 3 103 6000 30 20 25 5925 28-FEB-09 1 101 5000 20 12 20 4893 28-FEB-09 2 102 2000 15 28-FEB-09 2 102 3000 12 10 15 2963 28-FEB-09 3 103 6000 30 20 25 5850 31-MAR-09 1 101 5000 20 16 20 4837 31-MAR-09 2 102 3000 12 15 8 2928 31-MAR-09 3 103 6000 30 20 25 5775 30-APR-09 1 101 5000 20 20 20 4777 30-APR-09 2 102 3000 12 12 15 2889 30-APR-09 3 103 5775 25 30-APR-09 3 103 6000 30 20 25 5700
-
My dear
This query, I know sal sum in dept by job is
I have to add new coumn in sum to give query for all dept
and new line give sum for each Department
as a matrix report
WITH pivot_data AS (SELECT deptno, job, sal FROM emp)
SELECT "JOB."
"10."
"20."
'30 ',.
"40".
OF pivot_data PIVOT (SUM (sal)
FOR deptno
(10, 20, 30, 40)
)
Hello
So, besides the total sal for each combination of deptno and job, you want also totals for each jdeptno (regardless of the job) and work (regardless of deptno) and a total general (no matter the deptno and employment). This sounds like a job for the CUBE. The following soLution uses the CUBE to calculate amounts and then uses the PIVOT just to combine lines.
WITH all_jobs AS
(
SELECT DISTINCT job
FROM scott.emp
)
data_to_pivot AS
(
SELECT deptno NVL (d.deptno, - 1)
, NVL (j.job, 'ALL') AS job
, NVL (SUM (e.sal), 0) AS sal
GROUPING (j.job) AS job_agg
OF scott.dept d
CROSS JOIN all_jobs j
LEFT OUTER JOIN scott.emp e ON e.deptno = d.deptno
AND e.job = j.job
GROUP BY CUBE (d.deptno, j.job)
)
Simply SELECT employment, '10', all_depts '20', '30', '40', - or everything *, if you don't mind saw job_agg
OF data_to_pivot
PIVOT (MIN (sal)
TO deptno IN (10, 20, 30, 40
All_depts AS-1
)
)
ORDER BY job_agg
work
;
Output:
ALL_DEPTS 10 20 30 40 JOB
--------- ----- ----- ----- ----- ---------
0 6000 0 0 6000 ANALYST
1300 1900 950 0 4150 CLERK
MANAGER OF 2450 2975 2850 0 8275
THE PRESIDENT 5000 0 0 0 5000
0 0 5600 0 5600 SELLER
ALL THE 8750 10875 9400 0 29025
Instead of deriving a "table" of all possible jobs, you should be able to use a partitioned outer join, something like this:
WITH pivot_data AS
(
SELECT deptno NVL (d.deptno, - 1)
, NVL (e.job, 'ALL') AS job
, NVL (SUM (e.sal), 0) AS sal
GROUPING (employment) AS job_agg
OF scott.dept d
LEFT OUTER JOIN scott.emp e PARTITION BY (e.job)
ON e.deptno = d.deptno
GROUP BY CUBE (e.job, d.deptno)
)
SELECT *.
OF pivot_data
PIVOT (MIN (sal)
TO deptno IN (10, 20, 30, 40
All_depts AS-1
)
)
ORDER BY job_agg, job
;
But the only effect of the outer join partitioned seems to cause CUBE to abandon some Super aggregates. I do something ridiculous in this second request, or there is a bug in my version (11.2.0.2.0 Express).
(Almost always, when people say "there must be a bug", they're just doing something stupid.) There is no reason to think that I am an exception. I don't have another version of Oracle at hand right now)
-
I have two tables: employees and their departments. I'm figuring the total employees by the Department and the total employees of the entire society. I know I have to use the SUM function, but I can only calculate total employees by Department and company separately. I need to get this result:
Published by: user13675672 on January 30, 2011 14:29DEPT_NAME DEPT_TOTAL_SALARY COMPANY_TOTAL_SALARY RESEARCH 10875 29025 SALES 9400 29025 ACCOUNTING 8750 29025 This is my code: SELECT department_name, SUM(salary) as total_salary FROM employee, department WHERE employee.department_id = department.department_id GROUP BY department_name; SELECT SUM(salary) FROM employee; Can somebody help please? Thank you in advance.
Published by: user13675672 on January 30, 2011 14:31Hello
Something like:
SELECT dname, dept_tot_sal, SUM (dept_tot_sal) OVER () comp_tot_sal FROM (SELECT dname, SUM (sal) dept_tot_sal FROM dept, emp WHERE dept.deptno = emp.deptno GROUP BY dname);
There might be a smarter way, with no re - select.
Concerning
PeterAnalytical functions:
http://download.Oracle.com/docs/CD/E11882_01/server.112/e17118/functions004.htm -
How to only the totals using the native summary column (= "SUM () function
Hi all, this is my data model:
< name of dataTemplate 'Employees' description = 'Simple Dept - Emp' = >
< dataQuery >
< SQLStatement instance name = "Q1" >
<! [CDATA]
Select SERVICES. DEPTNO as DEPTNO,
DEPARTMENTS. DNAME as a DNAME
Scott. MINISTRIES DEPT order by DEPTNO]] >
< / sqlStatement >
< SQLStatement instance name = "Q2" >
<! [CDATA [Select
EMPLOYEES. ENAME like EMP_NAME,
EMPLOYEES. EMPNO as EMPNO,
EMPLOYEES. Hiring as HIREDATE date,
EMPLOYEES. SAL as WAGES,
EMPLOYEES. DEPTNO as EMP_DEPTNO
Scott. EMPLOYEES OF THE EMP
where EMPLOYED. [[DEPTNO =: DEPTNO]] >
< / sqlStatement >
< / dataQuery >
< dataStructure >
< name of group = "G_DEPT" source = "Q1" >
< element name = "DEPTNO" value = "DEPTNO" / >
< element name = "DNAME" value = "DNAME" / >
< element name = "TOTAL_EMPS" value = "G_EMP. EMPNO"function ="COUNT ()"/ >
< element name = "TOTAL_SALARY" value = "G_EMP. SALARY"function ="SUM ()"/ >
< element name = "AVG_SALARY" value = "G_EMP. SALARY"function ="AVG ()"/ >
< element name = "MAX_SALARY' value = 'G_EMP. SALARY"function ="MAX ()"/ >
< element name = "MIN_SALARY" value = "G_EMP. SALARY"function ="MIN ()"/ >
< name of group = "G_EMP" source = "Q2" >
< feature name = 'EMPNO' value = 'EMPNO' / >
< element name = "EMP_NAME" value = "EMP_NAME" / >
< element name = "HIREDATE" value = "HIREDATE" / >
< element name = "SALARY" value = "SALARY" / >
< / Group >
< / Group >
< / dataStructure >
< / dataTemplate >
This datatemplate works very well with the TOTAL_EMPS, TOTAL_SALARY, AVG_SALARY, MAX_SALARY MIN_SALARY split for all departments; So these are subtotals.
Now, I need to have the totals at the end of my report to the following:
TOTAL_ALL_EMPS
TOTAL_ALL_SALARY
TOTAL_ALL_AVG_SALARY
TOTAL_ALL_MAX_SALARY
TOTAL_ALL_MIN_SALARY
Add a new query (Q1) with all the totals, now the data model is the following:
< name of dataTemplate 'Employees' description = 'Simple Dept - Emp' = >
< dataQuery >
< SQLStatement instance name = "Q1" >
<! [CDATA]
Select COUNT (empno) TOTAL_ALL_EMPS, SUM (sal) TOTAL_ALL_SALARY, AVG (sal) TOTAL_ALL_AVG_SALARY, MAX (sal) TOTAL_ALL_MAX_SALARY MIN (sal) scott TOTAL_ALL_MIN_SALARY. EMPLOYEES EMP]] >
< / sqlStatement >
< SQLStatement instance name = "Q2" >
<! [CDATA]
Select SERVICES. DEPTNO as DEPTNO,
DEPARTMENTS. DNAME as a DNAME
Scott. MINISTRIES DEPT order by DEPTNO]] >
< / sqlStatement >
< SQLStatement instance name = "Q3" >
<! [CDATA [Select
EMPLOYEES. ENAME like EMP_NAME,
EMPLOYEES. EMPNO as EMPNO,
EMPLOYEES. Hiring as HIREDATE date,
EMPLOYEES. SAL as WAGES,
EMPLOYEES. DEPTNO as EMP_DEPTNO
Scott. EMPLOYEES OF THE EMP
where EMPLOYED. [[DEPTNO =: DEPTNO]] >
< / sqlStatement >
< / dataQuery >
< dataStructure >
< name of group = "G_TOT" source = "Q1" >
< element name = "TOTAL_ALL_EMPS" value = "TOTAL_ALL_EMPS" / >
< element name = "TOTAL_ALL_SALARY" value = "TOTAL_ALL_SALARY" / >
< element name = "TOTAL_ALL_AVG_SALARY" value = "TOTAL_ALL_AVG_SALARY" / >
< element name = "TOTAL_ALL_MAX_SALARY" value = "TOTAL_ALL_MAX_SALARY" / >
< element name = "TOTAL_ALL_MIN_SALARY" value = "TOTAL_ALL_MIN_SALARY" / >
< / Group >
< name of group = "G_DEPT" source = "Q2" >
< element name = "DEPTNO" value = "DEPTNO" / >
< element name = "DNAME" value = "DNAME" / >
< element name = "TOTAL_EMPS" value = "G_EMP. EMPNO"function ="COUNT ()"/ >
< element name = "TOTAL_SALARY" value = "G_EMP. SALARY"function ="SUM ()"/ >
< element name = "AVG_SALARY" value = "G_EMP. SALARY"function ="AVG ()"/ >
< element name = "MAX_SALARY' value = 'G_EMP. SALARY"function ="MAX ()"/ >
< element name = "MIN_SALARY" value = "G_EMP. SALARY"function ="MIN ()"/ >
< name of group = "G_EMP" source = "Q3" >
< feature name = 'EMPNO' value = 'EMPNO' / >
< element name = "EMP_NAME" value = "EMP_NAME" / >
< element name = "HIREDATE" value = "HIREDATE" / >
< element name = "SALARY" value = "SALARY" / >
< / Group >
< / Group >
< / dataStructure >
< / dataTemplate >
It works fine, but my question is this:
How can I change the data model and use the native summary column (function = "SUM (), for example) to collect data that I mentioned above (TOTAL_ALL_EMPS, TOTAL_ALL_SALARY, etc.), in order to avoid a third query SQL (Q1)?
In a few words, I would only use the query Q2 and Q3 and have overall totals and subtotals.
Thanks in advance for any help
Alex
Select 1 double DUMMY_FIELD]] >
Select SERVICES. DEPTNO as DEPTNO,
DEPARTMENTS. DNAME as a DNAME
Scott. MINISTRIES DEPT order by DEPTNO]] >
EMPLOYEES. ENAME like EMP_NAME,
EMPLOYEES. EMPNO as EMPNO,
EMPLOYEES. Hiring as HIREDATE date,
EMPLOYEES. SAL as WAGES,
EMPLOYEES. DEPTNO as EMP_DEPTNO
Scott. EMPLOYEES OF THE EMP
where EMPLOYED. [[DEPTNO =: DEPTNO]] >
Try this
Concerning
Charlotte -
Shows the columns having / sum not zero
We have a report that HAV total of twenty columns.
Whenever we run you report (dynamic query) random columns have sum = zero
We want to show only eachtime the columns having the amount greater than zero.
concerning
Jean MarcHi John,.
OK - I've tested this here [http://htmldb.oracle.com/pls/otn/f?p=267:43]. The report on the left shows/hides the SAL and COMM columns according to totals. The report on the right shows the complete data. Change the Dept to see this implemented.
There are other methods available, but that's what I did:
The select dept list (item name is P43_DEPTNO) is simply a list of departments and a NULL option that returns 0 (zero).
This report is a SQL statement of:
SELECT EMPNO, ENAME, SAL, COMM FROM EMP WHERE :P43_DEPTNO = 0 OR :P43_DEPTNO = DEPTNO ORDER BY ENAME
I have a region in the position after the header that uses "No. Template" and contains two pieces of Hidden - P43_SAL and P43_COMM.
I have a calculation of element, running "Before Header", which sets P43_DEPTNO to 0 if it is NULL.
I have a PL/SQL unconditional process, "to charge - before Header", which has the following code to the currently running process:
DECLARE vSAL NUMBER; vCOMM NUMBER; BEGIN SELECT NVL(SUM(SAL),0), NVL(SUM(COMM),0) INTO vSAL, vCOMM FROM EMP WHERE :P43_DEPTNO = 0 OR :P43_DEPTNO = DEPTNO; :P43_SAL := vSAL; :P43_COMM := vCOMM; END;
And, finally, on the SAL column in the report, I put the Condition Type for 'value of element in the Expression 1. = zero' and the Expression 1 is: P43_SAL. And even on the COMM, but the Expression 1 column: P43_COMM
When the page is loaded, the process is executed and SAL and COMM totals are calculated and inserted in P43_SAL and P43_COMM. The report runs and conditions check the values of these two hidden items - if a is not null, then the column is displayed.
Andy
-
SUM on &; Partition BY... Try to get the correct total run.
running: Oracle XE 11
Hi all, I am using the analytical function of PARTITION BY to get my total race for a report. (in this case the bank balance)
SUM (bank_stmts.amount) OVER (PARTITION BY bank_stmts.account_ID ORDER BY bank_stmts.tx_date, bank_stmts.id)
I add a balance initial in the table at the beginning of the year, and everything works fine when I select on the recordings of the whole years...
However, I need to interview a subset of the report, i.e. one month, or a selection of documents, rather than year-round. But I still want to see the total calculated for the whole year on the report, not a total operating for the subset of records. I'm not sure how to do that because when I select a subset, it totals only the subset...
I see that there are a number of clauses on the analytical function, especially BETWEEN the LINES or RANGE, but my assumption is that these still only works on surveyed lines and not any of the table...
My way of thinking for the moment, is that patients have to calculate and enter the total in the table when the row is inserted, probably via a trigger. However, I didn't keep far from...
Kind regards
Richard
SQL > with t as)
2. Select ename,
3 job,
4 hiredate,
5 sum (sal) over (partition by deptno arrested by hiredate) running_total_sal
6 of PEM
7 )
8 select
9 t
10 order by hiredate
11.HIREDATE RUNNING_TOTAL_SAL EMPLOYMENT ENAME
------ --------- --------- -----------------
SMITH, CLERK DECEMBER 17, 80 800
ALLEN SALESMAN FEBRUARY 20 81 1600
WARD 22 FEBRUARY SALESMAN 81 2850
MANAGER OF JONES 2 APRIL 81 3775
BLAKE MANAGER MAY 1, 81 5700
CLARK MANAGER JUNE 9, 81 2450
SELLER OF 7200 08 - SEP - 81 TURNER
MARTIN SELLER 28-SEP-81 8450
PRESIDENT KING NOVEMBER 17 81-7450
JAMES CLERK 9400 3 DECEMBER 81
DECEMBER 3, 81 FORD ANALYST 6775HIREDATE RUNNING_TOTAL_SAL EMPLOYMENT ENAME
------ --------- --------- -----------------
MILLER COMMITTED JANUARY 23, 82 8750
SCOTT, ANALYST APRIL 19 87 9775
ADAMS, CLERK MAY 23, 87 1087514 selected lines.
SQL > with t as)
2. Select ename,
3 job,
4 hiredate,
5 sum (sal) over (partition by deptno arrested by hiredate) running_total_sal
6 of PEM
7 )
8 select
9 t
10 where hiredate > = date ' 1981-01-01'
11 and hiredate< date=""> >
12 order by hiredate
13.HIREDATE RUNNING_TOTAL_SAL EMPLOYMENT ENAME
------ --------- --------- -----------------
ALLEN SALESMAN FEBRUARY 20 81 1600
WARD 22 FEBRUARY SALESMAN 81 2850
MANAGER OF JONES 2 APRIL 81 3775
BLAKE MANAGER MAY 1, 81 5700
CLARK MANAGER JUNE 9, 81 2450SQL >
SY.
-
What type of input parameter is used according to the SUM?
Hello world
As we know sum is an oracle function preset. But what these guys here oracle used for input parameters. How did they do that?
I mean, we can write this sum fuction as many ways as as mentioned below. Please give me some ideas how to do this.
Kind regardsSELECT SUM(salary) as "Total Salary" FROM employees; SELECT SUM(DISTINCT salary) as "Total Salary" FROM employees; SELECT SUM(income - expenses) as "Net Income" FROM gl_transactions; SELECT SUM(sales * 0.10) as "Commission" FROM order_details;
BS2012BS2012 wrote:
Hello world
As we know sum is an oracle function preset. But what these guys here oracle used for input parameters. How did they do that?
I mean, we can write this sum fuction as many ways as as mentioned below. Please give me some ideas how to do this.SELECT SUM(salary) as "Total Salary" FROM employees; SELECT SUM(DISTINCT salary) as "Total Salary" FROM employees; SELECT SUM(income - expenses) as "Net Income" FROM gl_transactions; SELECT SUM(sales * 0.10) as "Commission" FROM order_details;
Kind regards
BS2012As others have said, your question is not very clear.
There are many aspects and angles to look at what you're asking.
First of all, a level superior, the sum function simply takes a numeric value as it's argument, so all of these examples you gave have expressions that evaluate to a numeric value to be provided to the sum function. (As someone else already mentioned you can have non-numeric data types, just as long as they can be implicitly converted to a numeric value).
A statement analysis and the point of view of enforcement, the content of the expression inside the brackets is evaluated before being passed to the sum function. It is not the function sum that he himself takes the expression and evaluates it. The sum function is expecting just a single numeric value.
Internally, what the function sum is the case, is more than just a single... call function and return a value, because it faces several values being passed in the aggregation group. As such, it must have the ability to know if to start in short, is to accept several input values, so he can add them together, you know give up adding entries and pass the result to the back.
If we write our own defined aggregate function (others have already provided a link to explain this) we can see what is happening internally. In the following example, we'll write a function defined by the user that multiplies the values rather than the amounts...
create or replace type mul_type as object( val number, static function ODCIAggregateInitialize(sctx in out mul_type) return number, member function ODCIAggregateIterate(self in out mul_type, value in number) return number, member function ODCIAggregateTerminate(self in mul_type, returnvalue out number, flags in number) return number, member function ODCIAggregateMerge(self in out mul_type, ctx2 in mul_type) return number ); / create or replace type body mul_type is static function ODCIAggregateInitialize(sctx in out mul_type) return number is begin sctx := mul_type(null); return ODCIConst.Success; end; member function ODCIAggregateIterate(self in out mul_type, value in number) return number is begin self.val := nvl(self.val,1) * value; return ODCIConst.Success; end; member function ODCIAggregateTerminate(self in mul_type, returnvalue out number, flags in number) return number is begin returnValue := self.val; return ODCIConst.Success; end; member function ODCIAggregateMerge(self in out mul_type, ctx2 in mul_type) return number is begin self.val := self.val * ctx2.val; return ODCIConst.Success; end; end; / create or replace function mul(input number) return number deterministic parallel_enable aggregate using mul_type; /
For example, our user-defined aggregate function is based on a total object type.
This object keeps a value ("val" in our example).
It has an Initialize method, so when the SQL engine indicates that this is the beginning of an aggregation of values it can set its value to an initial value (in this case zero).
It has an Iterate method, the SQL engine passes the values to it in the context of all grouped values, it can treat them (in our case it multiplies the value of entry with the value already there for this game of aggregations (and takes a value of 1 for the first iteration basis))
She has a Terminate method, so when the SQL engine indicates that the global set of values is completed, it can return the result.
The last method there is a merger and is mandatory, so that when the aggregation is done parallel assessment tool (for better performance internally), the results of these parallel executed aggregations can be combined (see http://docs.oracle.com/cd/E11882_01/appdev.112/e10765/ext_agg_ref.htm#ADDCI5132). As we are multiplying numbers, in our case, it is simply a case of the multiplication of one single result with each other.And to see that it works...
SQL> with t as (select 2 as x from dual union all 2 select 3 from dual union all 3 select 4 from dual union all 4 select 5 from dual) 5 -- 6 select mul(x) 7 from t; MUL(X) ---------- 120
-
Hello
I'm new on OTN. This is my first question.
I have to run a report with the AMT can be combined. If you can provide with the emp table, then I can implement logic with my table accordingly. But without analytical functions... I have to post.
Need your valuable help
RekhaHello
Welcome to OTN.
Try like this...!
select e1.empno, e1.ename, e1.sal, (select sum(sal) from emp e2 where e2.empno <= e1.empno ) AS cume_salary from emp e1 order by empno
KPR
-
Hello world
Could someone suggest me how to calculate total sales. The scenario is
My Table is like this
Cardno sales accumulated sales
101 100 100
102 80 180
103 20 200
I need to calculate the third column "Accumulated Sales" from column 2 "sales".
is there a formula in oracle to calculate cumulative sales? How did I need form the SQL for this requirement? Can anyone help?
Thanks in advance
Concerningselect Cardno, Sales, sum(Sales) over(order by rownum) as "Accumulated Sales" from your_table
-
list of employees where the sum of the salary must be less than an amount
I take the list of employees from the employee table have a column 'Amount' where he sum of "amount" should be less than a certain amount say "100000".
thanxWhat do you mean sum of the amount? Sum of the amount for a particular employee or you are talking about running here sum?
anyway... This technique can lead you I guess
SELECT * FROM)
SELECT empno, ename, sal,
Sum (SAL) (ORDER BY empno) total,
Mgr
FROM emp) WHERE total<>Published by: Buga on October 24, 2009 02:58
-
How to get to the total/sum at the end of the production
How can I get an output like below using SQL statements only;
ENAME SAL
------------ ---
SMITH, 800
KING 5000
ADAMS 1100
Total 6900
Best regards, YacoubSELECT ENAME , Sal FROM EMP UNION SELECT 'TOTAL' A , SUM(Sal) FROM EMP GROUP BY 'TOTAL'
Published by: AJ99 on July 6, 2009 14:57
-
get the only recording where the sum is less
Hello
I need to find all the empno all together (sum) is equal in early 2000 by lowest SALselect EMPNO, SAL from EMP 7369 800 7499 1600 7521 1250 7566 2975 7654 1250 7698 2850 7782 2450 7788 3000 7839 5000 7844 1500 7876 1100 7900 950 7902 3000 7934 1300
IF the sum is below 2000, I can then take an empno, also if, then, the sum will be longer than 2000
It's
800 +.
950 + = 1750
1100 +.
= 2850
I can get the first empno 3 discs = (7369,7900,7876)
If I want to compare with 1750 instead of 2000, I get only two first empno = (7369,7900)
How can he do?
Thanks in advanceSQL> select empno, sal 2 from ( 3 select empno, sal, sal1, nvl(lag(sal1) over(order by sal),0) lsal 4 from (select EMPNO, SAL, sum(sal) over(order by sal) sal1 from EMP)) 5 where lsal <2000 6 / EMPNO SAL ---------- ---------- 7369 800 7900 950 7876 1100 SQL> select empno, sal 2 from ( 3 select empno, sal, sal1, nvl(lag(sal1) over(order by sal),0) lsal 4 from (select EMPNO, SAL, sum(sal) over(order by sal) sal1 from EMP)) 5 where lsal <1750 6 / EMPNO SAL ---------- ---------- 7369 800 7900 950
-
Hi all
I am looking for a query that gives me something like:
Select MAX (SUM (hard)) as maxDur, name of person
This gives an ORA-00937: not a function of simple-group.
That's why one translate only for
Select max (sumDur) in the maxDur, name
de)
Select name, sum (hard) as sumDur of person
Group BY name)
Group by name
And that's almost what I would like to have, but:
It gives me the same result as the subquery "sum". A list of names...
I wish I had 1 single name: one who has hard MAX...
If I take the 'name' of the top application it gives me only one line, the MAX one. But I need to also know the name of the correspondent...
Can I do something like "order by maxDur desc" and "select top 1"? sounds as a workaround...
Any idea?
Thank you!
ReferSimilar to already answers provided - you to achieve through subquery or with the first/last aggregate function (but in this case, you have to make sure, you have only a sum, which is the maximum).
SQL> select sum(sal), job 2 from emp 3 group by job 4 having sum(sal) = (select max(sum(sal)) from emp t group by job) 5 ; SUM(SAL) JOB ---------- --------------------------- 8275 MANAGER SQL> SQL> select 2 max(sum(sal)), 3 max(job) keep(dense_rank last order by sum(sal)) 4 from emp 5 group by job 6 ; MAX(SUM(SAL)) MAX(JOB)KEEP(DENSE_RANKLAST ------------- --------------------------- 8275 MANAGER
Best regards
Maxim
-
Hi all
I think I'm really stupid but I have problems of SQL query.
I have a table with many lines of operations which all refer to different stores, for example:
Store... _Sales_... _Month_
Reading... 200 k... April
Leeds... 50k................ April
Manchester... 70k................ May
Reading... 100 k... May
I need to arrive at the average sales for the month combined as a total. That is the average income of store for a given period is 200 k + 50 k + 70 k + 100 k / * 3 * (because there are only 3 unique shops) - I hope this makes sense!
So, basically, I'm doing both a query of the SUM (and company store) and then out of the average of all the stores together. Is this possible?
Thank youHello
This query returns 140 which seems to be the correct value:
with data as ( select 'Reading' Store , 200 sales from dual union select 'Leeds' , 50 from dual union select 'Manchester' , 70 from dual union select 'Reading' , 100 from dual ) select sum(sales) , count( distinct store ) , sum(sales)/count(distinct store) from data
There are several options:
1. you can get two IR on one page (using IFRAMEs - search for that word on the Forum)...
2. you create another region with the above query and that position just below the report
3. in the foot of the region call a process of PL/SQL (using AJAX) that calculates the value using the query and print it (via htp.p)Greetings,
Roelhttp://roelhartman.blogspot.com/
http://www.bloggingaboutoracle.org/
http://www.Logica.com/You can assign this answer to your question in marking it as useful or Correct ;-)
Maybe you are looking for
-
Accidentally pressed too long on the button Delete. Delete all emails. Not in the trash or me caouldn can't find them anywhere on Thunderbird. Please help me to get back them. Thank you
-
Send just a single command via GPIB
Hello I an instrument controller laser diode here and plugged into a USB of NI GPIB adapter, put in place and got to talk. If I send a command through 'Communicate with Instrument', it works, and NI SPY shows a write to the device. If I start a new V
-
Anyone has any suggestions on a good magnifying glass to the A99 motorway? Thank you Kirk
-
Please will someone please help me am have to vew all sideway files and this isn't luxury its a low really slow I'm trying to fix this problem for five hours please someone help I, ve tried to go on the desktop click right propriy that did not work,
-
Cannot find files on sd card mp3
said to post this here... Hi I have an e280 v1 and love, have had very few probs with it. I just put audio books on the sd card and it's not finding them. I have others there and is finding those so he found the card. I checked and they have the ex .