I have an APEX report and I want to have subtotals (group by column 'l_char') and Grand totals . I have achieved the subtotals by doing a page break and using the aggregate function 'add'.
Now, I also want to show the grand subtotal at the end (eg. : it should show Total = 20000 at the end of the report).
Can somebody please help and let me know how can i achieve this.
Thanks,
Abha
Alternatively, if you can afford switching from interactive to classic report, then with an "ordinary" query:
select deptno,
ename,
sal
from emp
where deptno in (10, 20)
order by deptno, ename;
Set report's attributes:
break formatting:
report sum label: Total
break columns: first column
Set sal column's properties:
advanced - compute sum: set it to ON
Run the page; result is
It is easy to create totals using the "Actions" menu. But, grand total can't be computed that way (at least, I don't know how).
But, if you modify the query and do everything within, then you'd group by rollup and get the result.
This example is based on Scott's sample emp table; adjust it so that it contains your columns/table(s):
select deptno,
ename,
sum(sal) sumsal
from emp
where deptno in (10, 20)
group by rollup (deptno, ename)
order by deptno, ename;
The result is then
Related
When I try it without displaying department_id it works fine as :
SQL> SELECT MAX(AVG(SALARY)) FROM EMPLOYEE GROUP BY DEPARTMENT_ID;
MAX(AVG(SALARY))
----------------
800000
But when I want to display the department_id's too, it gives me error as follows:
SQL> SELECT DEPARTMENT_ID, MAX(AVG(SALARY)) FROM EMPLOYEE GROUP BY DEPARTMENT_ID;
SELECT DEPARTMENT_ID, MAX(AVG(SALARY)) FROM EMPLOYEE GROUP BY DEPARTMENT_ID
*
ERROR at line 1:
ORA-00937: not a single-group group function
Is there any explanation for this? What am I doing wrong? I went through answers of previous questions like this and tried their solutions but got same or some other error. Any help would be appreciated.
I suggest you use
SELECT department_id, avg_salary
FROM ( SELECT DEPARTMENT_ID,
AVG (SALARY) avg_salary,
RANK () OVER (ORDER BY AVG (salary) DESC) rnk
FROM EMPLOYEE
GROUP BY DEPARTMENT_ID)
WHERE rnk = 1;
i.e.
use your first query as a "source"
additionally, rank average salaries in descending order (using the rank analytic function)
select row which ranks as highest
select empno , deptno , sal from emp
order by 1,2
What are 1, 2 in alignment?
What is one and two? Why don't you write the column name?
The query language defines this as a shortcut.
select empno , deptno , sal from emp
order by 1,2
select empno , deptno , sal from emp
order by empno, deptno
mean precisely the same thing. The numbers refer to the column numbers in your SELECT, counting from 1.
The shortcut comes in handy if you have stuff like
select CONCAT(surname, ', ', givenname) name, empno , deptno , sal from emp
order by 1
because it saves typing. In standard SQL you'd have to write that query
select CONCAT(surname, ', ', givenname) name, empno , deptno , sal from emp
order by CONCAT(surname, ', ', givenname)
Use whichever one you please. But be careful; it makes your ORDER BY clause dependent on the order of items in your SELECT clause. The next person to work on your code may not be expecting such a dependency. Especially if that next person is you.
It works just the same, whether you use
order by empno, deptno
or
order by 1, 2
Note that - although positional sorting requires less typing, you have to synchronize such an order by clause with every select column list rearrangement. I never use it (except for quick & dirty testing queries).
I am having a query where I am selecting 4 columns. And, I want to put a grand total at the bottom of one of the columns, and not do any grouping:
SELECT customer_id, email, total_amount, order_date
FROM...................
I want to do a grand total of TOTAL_AMOUNT at the bottom, but not worry about any grouping. I'm not seeing how to do this using GROUPING or ROLLUP. I'm hoping not to have this as any running total in another oolumn, but as a grand total at the bottom.
Many thanks.
You can add a grand total row with a UNION ALL and a column to track if the row is for the grand total.
select customer_id, email, total_amount, order_date, 0 is_grand_total
from orders
union all
select null, null, sum(total_amount), null, 1 is_grand_total
from orders
order by is_grand_total, customer_id;
SQL Fiddle example.
(In my opinion, this is often a good way to add summary logic to queries. I'd rather have a slightly more complicated solution with one language (SQL), than a solution that involves two or more languages or applications.)
A simple SQL*Plus option:
SQL> break on report
SQL> compute sum of sal on report
SQL>
SQL> select deptno, ename, sal
2 from emp
3 where deptno = 10;
DEPTNO ENAME SAL
---------- ---------- ----------
10 CLARK 2450
10 KING 5001
10 MILLER 1300
----------
sum 8751
SQL>
I have a Apex Page to create meeting appointments. There are 2 items in it
Participating Departments
Participating Employees
The employees who would be participating should be only from the selected "participating departments".
Both are shuttle items. In the first one I am displaying
SELECT DEPTNAME, DEPTNO FROM DEPARTMENTS
For the second one, I should be using
SELECT EMPNAME, EMPNO FROM EMPLOYEES WHERE DEPTNO IN (<<the selected departments>>)
Now I am having difficulty to form these selected departments in query. Can someone help on this?
MY SECOND QUESTION:
I need to also display the appointments as an interactive report. Since I am saving all the selected departments as numerical values (for ex 2:3:7), the report is displaying these deptno, instead of departmentname (For ex, HR,Finance,Reception)
Can someone help me with 2nd question pls?
This query
select regexp_substr('1:2:3','[^:]+', 1, level) ID from dual
connect by regexp_substr('1:2:3', '[^:]+', 1, level) is not null
splits your string to table
ID
--
1
2
3
After that you can use this query as subquery in WHERE DEPTNO IN (...) clause and join it with LOV source for Interactive Report.
i have three columns employee_id,e_name,Sal and i want the sum of Sal group by employee_id and i want all the columns like employee_id,e_name,Sal,sum as out put in oracle.
in/put= OP=
employee_id,e_name,Sal employee_id,e_name,sal,sum
select employee_id,e_name,sal, sum(Sal) over()
from YOUR_TABLE
you will have the same value of sum for each employee