GROUP BY FUNCTION [message #422017] |
Sun, 13 September 2009 09:04 |
shaaquille
Messages: 2 Registered: September 2009
|
Junior Member |
|
|
hi
i am newbie and i am preparing for oracle sql exam...
i am having problems with a query ... hope u kan help me
------------------------------------------
create a query that displays total number of employees...
and of that total number,number of emoloyees hired in 1980,1981,1982
out put shd be like this
total -- 1980 -- 1981 -- 1982 -- 1983
14 ----- 2------ 1 ----- 3 ----- 3
----------------------------------------------
thank you in advance
regards !
|
|
|
|
Re: GROUP BY FUNCTION [message #422046 is a reply to message #422017] |
Mon, 14 September 2009 01:55 |
ayush_anand
Messages: 417 Registered: November 2008
|
Senior Member |
|
|
SQL> select HIREYEAR,count
2 from
3 (
4 select to_char(HIREDATE,'yyyy ') HIREYEAR,count(0) count
5 from scott.emp
6 where to_char(HIREDATE,'yyyy ') in (1981,1982,1987)
7 group by to_char(HIREDATE,'yyyy ')
8 )
9 MODEL
10 DIMENSION BY (HIREYEAR)
11 MEASURES (count)
12 RULES (
13 count['total'] = sum(count)[HIREYEAR IN (1981,1982,1987)] )
14 /
HIREY COUNT
----- ----------
1982 1
1981 10
1987 2
total 13
[Updated on: Mon, 14 September 2009 01:56] Report message to a moderator
|
|
|
|
Re: GROUP BY FUNCTION [message #422074 is a reply to message #422017] |
Mon, 14 September 2009 03:56 |
shaaquille
Messages: 2 Registered: September 2009
|
Junior Member |
|
|
thanx fr all of your replies !
actually i had solved it... and it was quite simple fr me :s
-----------------------
select count(ename)total,
sum(count(case to_char(hiredate,'YYYY') when '1980' then 1 end)) "1980",
sum(count(case to_char(hiredate,'YYYY') when '1981' then 1 end)) "1981",
sum(count(case to_char(hiredate,'YYYY') when '1982' then 1 end)) "1982",
sum(count(case to_char(hiredate,'YYYY') when '1983' then 1 end)) "1983"
from emp
group by ename
---------------------------
it worked fr me !
once again thanx fr your replies !
regards !
|
|
|
|
|
Re: GROUP BY FUNCTION [message #422207 is a reply to message #422054] |
Tue, 15 September 2009 02:03 |
ayush_anand
Messages: 417 Registered: November 2008
|
Senior Member |
|
|
thanks Michel
I was just trying to use MODEL somewhere
while Rollup is very good really
SQL> SELECT DECODE (GROUPING (EXTRACT (YEAR FROM hiredate)),
2 1, 'TOTAL',
3 EXTRACT (YEAR FROM hiredate)
4 ) hireyear,
5 COUNT (0) cou
6 FROM scott.emp
7 WHERE TO_CHAR (hiredate, 'yyyy') IN (1981, 1982, 1987)
8 GROUP BY ROLLUP (EXTRACT (YEAR FROM hiredate))
9 /
HIREYEAR COU
---------------------------------------- ----------
1981 10
1982 1
1987 2
TOTAL 13
|
|
|
|
Re: GROUP BY FUNCTION [message #422213 is a reply to message #422209] |
Tue, 15 September 2009 02:19 |
|
Michel Cadot
Messages: 68718 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Quote:Michel, out of curiosity: what's "better" about an extract?
Just 2 points:
1/ This is what you want: extracting year from the date (that is get a number) and not displaying a year (which is the purpose to to_char)
2/ The implementation.
Date is in format (not exactly that but the principle), century/year in century/month/day/hour/minute/second, each field in a byte.
When using extract, Oracle knows exactly which byte(s) to extract and directly goes to it (them).
When using to_char, Oracle gets the whole date and call a function that interprets the format and for each format element get the corresponding byte(s) in the date, converts it (them), put it in the result string and goes to the next part of given format.
Regards
Michel
|
|
|
|
Re: GROUP BY FUNCTION [message #422219 is a reply to message #422214] |
Tue, 15 September 2009 02:45 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
I just did some tests (code below, and Extract does seem to be faster than TO_CHAR. For year conversions, it's about 15-20% faster.
create or replace type ty_timing_table as table of number;
/
declare
t_timing ty_timing_table := ty_timing_table();
v_iter pls_integer := 10000;
v_max pls_integer := 100;
v_time pls_integer;
v_avg number;
v_total pls_integer;
v_stddev number;
v_num number;
begin
t_timing.delete;
t_timing.extend(v_iter);
for i in 1..v_iter loop
v_time := dbms_utility.get_time;
for i in 1..v_max loop
v_num := extract(year from sysdate-v_max*5);
end loop;
t_timing(i) := dbms_utility.get_time - v_time;
end loop;
select avg(column_value)
,stddev(column_value)
,sum(column_value)
into v_avg,v_stddev,v_total
from table(t_timing);
dbms_output.put_line('Test 1: Total: '||v_total||' Avg: '||round(v_avg,3-trunc(log(10,v_avg)))||' stddev: '||round(v_stddev,3));
t_timing.delete;
t_timing.extend(v_iter);
for i in 1..v_iter loop
v_time := dbms_utility.get_time;
for i in 1..v_max loop
v_num := to_char(sysdate-v_max*5,'yyyy');
end loop;
t_timing(i) := dbms_utility.get_time - v_time;
end loop;
select avg(column_value)
,stddev(column_value)
,sum(column_value)
into v_avg,v_stddev,v_total
from table(t_timing);
dbms_output.put_line('Test 1: Total: '||v_total||' Avg: '||round(v_avg,3-trunc(log(10,v_avg)))||' stddev: '||round(v_stddev,3));
end;
|
|
|
|
|
|