Confusion in group by function
Hi,
I always have a doubt regarding the group by function i.e. if we are using a group by function and we want to display some columns which are not to be grouped by then how do we display those.
Like for eg.
to display last name, department no and average of salary for each department how should we proceed.
I know we can display the department no, and avg(sal) but how abt the last name.
Do we use a subquery?
Any thoughts or explanaitions with example?
Thanks
hi, People often get confused with group by function.
You have to be very clear about this that the group by must include all the columns that have been selected. If you want to use another column out of group by list then that column must be driven by the another table join or inline table join.
bye
Similar Messages
-
Confusion in group by function- Any thoughts?
Hi,
I always have a doubt regarding the group by function i.e. if we are using a group by function and we want to display some columns which are not to be grouped by then how do we display those.
Like for eg.
to display last name, department no and average of salary for each department how should we proceed.
I know we can display the department no, and avg(sal) but how abt the last name.
Do we use a subquery?
Any thoughts or explanaitions with example?
ThanksWhile the analytic functions already suggested are likely more efficient for y our example, there are cases where they may not be. An alternative formulation without analytics would be something like:
SQL> SELECT s.department_id, e.last_name, s.average
2 FROM (SELECT department_id, AVG(salary) average
3 FROM employees
4 GROUP BY department_id) s, employees e
5 WHERE s.department_id = e.department_id
6 ORDER BY s.department_id;
DEPARTMENT_ID LAST_NAME AVERAGE
10 Whalen 4400
20 Hartstein 9500
20 Fay 9500
30 Raphaely 4150
30 Tobias 4150
30 Colmenares 4150
30 Himuro 4150
30 Baida 4150
30 Khoo 4150
40 Mavris 6500
50 Taylor 3475.55556
50 Fleaur 3475.55556
50 Sullivan 3475.55556
50 Geoni 3475.55556
50 Cabrio 3475.55556HTH
John -
Hi All
very simple one but i just confused by it . i just want to display the rows in group by function .
example
SQL> select user_full_name,last_login_date,email_address from comit_user group by company_code
its giving an error .
could some one please help me in this .
thanks in advance .Hi there,
Group by is used with group functions such as SUM, AVG, MAX, MIN, etc
eg. select job, sum(sal) from emp group by job;
JOB SUM(SAL)
ANALYST 6000
CLERK 4150
MANAGER 8275
PRESIDENT 5000
SALESMAN 5600
In your query, it looks like you need to use ORDER BY instead
select user_full_name,last_login_date,email_address from comit_user
ORDER BY company_code
Regards,
John -
Automatic assignment responsible group partner function of Activities
Hi,
I need help on below requirement>>
Automatic assignment responsible group partner function of Activities when status is set to u201CIncorrectly Assignedu201D.
Thanksyou can check with order status badi
BR
Radek -
Sort order for group by functions
I have table TAB1(notenum number, linenum number, linetext varchar2(100)).
Each note can have multiple lines of text. Eg data
NOTENUM LINENUM LINETEXT
1 1 THIS IS LINE1.
1 2 THIS IS LINE2.
2 1 THIS IS LINE1 OF NOTE2.
2 2 THIS IS LINE2 OF NOTE2.
I need the text of lines of the most recent note (max notenum)
<Note>THIS IS LINE1 OF NOTE2. THIS IS LINE2 OF NOTE2</Note>.
I have a query like this
SELECT XMLAGG(XMLPARSE(CONTENT LINETEXT WELLFORMED))
FROM TAB1
GROUP BY NOTENUM
ORDER BY NOTENUM DESC
Above query would return the text of all lines concatenated for each note and the first row would be the one with highest notenum value.
My problem is that concatenation of the LINETEXT should happen in ascending order of LINENUM. Since LINENUM is not in GROUP BY clause I cannot specify it in ORDER BY.
How can I specify to XMLAGG (for that matter any group by function) to concatenate text in a specific order.
I hope I was able to explain my problem clear enough for somebody to help.
thanks for all the help
Message was edited by:
user448703
Message was edited by:
user448703Never mind. I just found out that XMLAGG function has the option to specify ORDER BY. I think that will work.
-
I want to get an output using group by function of there is no data in the table to display a particular value
for eg:
SELECT TRUNC(updated_date) DATE1 , COUNT(1) COUNT FROM table
where TRUNC(updated_date) >=TRUNC(SYSDATE-18) AND TRUNC(updated_date) <=TRUNC(SYSDATE)
GROUP BY TRUNC(updated_date)
ORDER BY TRUNC(updated_date) DESC;
DATE1 COUNT
6/16/2012 14208
6/15/2012 307825
6/14/2012 172988
6/6/2012 138790
6/5/2012 167562
6/4/2012 51870
6/2/2012 130582
6/1/2012 239806
But i need the missed out date i.e 6/3/2012 - 0, 6/7/2012 - 0 to be displayed since there is no data on the repective dates.Hi,
You can only display things that are in a table (or result set, but from now on, I'll just say table), or that can be derived from a table. If you want to display dates that are not in your table, then you have to get them from another table, or derive them from some table.
Some people actually keep tables of possible dates for queries like this. In this case, all you need is a table of the last 19 dates, ending with today. That's easy to derive from dual:
WITH all_dates AS
SELECT TRUNC (SYSDATE) - LEVEL AS date1
, TRUNC (SYSDATE) + 1 - LEVEL AS date2
FROM dual
CONNECT BY LEVEL <= 19
SELECT a.date1
, COUNT (x.updated_date) AS count
FROM all_dates a
LEFT OUTER JOIN table_x x ON x.updated_date >= a.date1
AND x.updated_date < a.date2
GROUP BY a.date1
ORDER BY a.date1
;If you'd care to post CREATE TABLE and INSERT statements for some sample data, and the results you want from that data, then I could test this. -
To fetch data using group by function
Hi Guys,
I am having a table where it holds data with value with zero in one of the column and i need to fetch the records from the table using group by function but to display all the records if zero occurs in my table
for eg
table1
id,customer_name,country
0 sam aus
1 peter ind
1 peter ind
0 samy us
0 rayan nz
if i use select count(*),id from table1 group by id
i will get
count(*),id
3-0
1-2
but wat i need is like below
0 sam aus
0 samy us
0 rayan nz
2 peter ind
whenever zero is occurring i need to display all the records but group by should work for non zero recordsThe simplest way would be to have two queries and UNION them together. For example:
WITH test_data AS
( SELECT 0 AS id, 'sam' AS customer_name, 'aus' AS country FROM DUAL UNION ALL
SELECT 1 AS id, 'peter' AS customer_name, 'ind' AS country FROM DUAL UNION ALL
SELECT 1 AS id, 'peter' AS customer_name, 'ind' AS country FROM DUAL UNION ALL
SELECT 0 AS id, 'samy' AS customer_name, 'us' AS country FROM DUAL UNION ALL
SELECT 0 AS id, 'rayan' AS customer_name, 'nz' AS country FROM DUAL
SELECT cnt
, customer_name
, country
FROM ( SELECT id
, customer_name
, country
, COUNT(*) OVER (PARTITION BY id) AS cnt
, ROW_NUMBER() OVER (PARTITION BY id ORDER BY id) rn
FROM test_data
WHERE id != 0
WHERE rn = 1
UNION
SELECT id
, customer_name
, country
FROM test_data
WHERE id = 0
This gives the following results:
CNT CUSTO COU
0 rayan nz
0 sam aus
0 samy us
2 peter ind -
How to Use a Group by Function
Hi Gurus,
I have Requirment where i need to use the group by function to one column
below is my query , can anyone help how to use the group by for the column OCCASIONALS_QT_STATUS.
below is giving me the error not a group by expression
select distinct source_id,OCCASIONALS_QT_STATUS,
(SELECT sum(head_count)
FROM gen_dcsf_occasionals_count
where OCCASIONALS_QT_STATUS = 'QTS'
and source_id = gdoc.source_id
) OccasionalsQTS,
(SELECT sum(head_count)
FROM gen_dcsf_occasionals_count
where
OCCASIONALS_QT_STATUS = 'NOTQTS'
and source_id = gdoc.source_id
) OccasionalsNOTQTS,
(SELECT sum(head_count)
FROM gen_dcsf_occasionals_count
where
OCCASIONALS_QT_STATUS = 'NTKNWN'
and source_id = gdoc.source_id
) OccasionalsNOTKNWN
from gen_dcsf_occasionals_count gdoc group by OCCASIONALS_QT_STATUS;
any inputs on this is highly appreciable
Thanks in advance909577 wrote:
Hi Gurus,
I have Requirment where i need to use the group by function to one column
below is my query , can anyone help how to use the group by for the column OCCASIONALS_QT_STATUS.
below is giving me the error not a group by expression
select distinct source_id,OCCASIONALS_QT_STATUS,
(SELECT sum(head_count)
FROM gen_dcsf_occasionals_count
where OCCASIONALS_QT_STATUS = 'QTS'
and source_id = gdoc.source_id
) OccasionalsQTS,
(SELECT sum(head_count)
FROM gen_dcsf_occasionals_count
where
OCCASIONALS_QT_STATUS = 'NOTQTS'
and source_id = gdoc.source_id
) OccasionalsNOTQTS,
(SELECT sum(head_count)
FROM gen_dcsf_occasionals_count
where
OCCASIONALS_QT_STATUS = 'NTKNWN'
and source_id = gdoc.source_id
) OccasionalsNOTKNWN
from gen_dcsf_occasionals_count gdoc group by OCCASIONALS_QT_STATUS;
any inputs on this is highly appreciable
Thanks in advanceFor your own sanity, you should format your code to make it more readable
For the sanity of those from whom you seek help, you should preserve that formatting with the code tags:
select
distinct source_id,
OCCASIONALS_QT_STATUS,
(SELECT
sum(head_count)
FROM
gen_dcsf_occasionals_count
where
OCCASIONALS_QT_STATUS = 'QTS' and
source_id = gdoc.source_id
) OccasionalsQTS,
(SELECT
sum(head_count)
FROM
gen_dcsf_occasionals_count
where
OCCASIONALS_QT_STATUS = 'NOTQTS' and
source_id = gdoc.source_id
) OccasionalsNOTQTS,
(SELECT
sum(head_count)
FROM
gen_dcsf_occasionals_count
where
OCCASIONALS_QT_STATUS = 'NTKNWN' and
source_id = gdoc.source_id
) OccasionalsNOTKNWN
from
gen_dcsf_occasionals_count gdoc
group by
OCCASIONALS_QT_STATUS; -
How to implement the Oracle Group by function in Crystal reports?
Hi all,
In SQL, for example we have a group function like:
select district,state, country, continent, sum(no.of people) from world.
Now, How to implement this group function in crystal reports? Please advise.
Thanks in advance..
Regards,
sriramHi Vinay,
Thanks for the prompt reply.
In one of our report, we are supposed to perform group by for 14 columns to get sum of 3 columns and there by displaying 17 columns in the report.
When we tried in crystal reports to implement this oracle group by functionality:
1. We created 14 groups from the Insert->Group option.
2. By performing this, we got 14 group sections vertically(one inside the other).
3. Then we created the sum(15th column),sum(16th column), sum(17th column) by Insert->Summary option.
4. We suppresed all the group sections except for the last group.
5. Then, dragged all the groups to the last group section along with the summary fields.
This is how, we tried to acheive the oracle group by function in Crystal reports.
Please advise, whether our approach is right. If not, please suggest the appropriate approach with a bit detailed explanation.
Thanks,
Sriram. -
Group by function in a measure object
Hi All,
I am trying to create a measure in universe which is the sum of ticket amount group by country. like,
sum(Payments.Ticket_Amount) group by country name. When i place this measure in a report group footer it should display the sum to that particular group. But it is not working.
Could anybody help me how to write a group by function to create a measure.
Thanks,
swati.Hi swati.
You don't need to write the GROUP BY statement at the universe level.
The universe knows that aggregate functions require group by statements and will group the aggregate measure objects by whatever dimension objects are included in the query. If you have a measure object that is not based on an aggregate but has a projection of Sum, it will only be aggregated at the report level once the data has been returned by the query.
Measure: sum(sales_fact.value)
Measure: sales_fact.price
Dimension: country_dim.country
This would generate SQL along the lines of:
SELECT sales_fact.price, country_dim.country, sum(sales_fact.value)
FROM sales_fact, country_dim
WHERE sales_fact.country_key = country_dim.country_key
GROUP BY sales_fact.price, country_dim.country
Hope that clears it up for you.
Regards,
Mark -
Hi frds
I wrote a code where there is a subquery in my select statement, when i use a group by function with my query it comes with an error
Example
This is how my select statement looks
SELECT
mt.subinventory_code,
item.description,
we.wip_entity_name,
SUM((mt.transaction_quantity)*-1),
sum(wro.required_quantity),
sum(wdj.start_quantity),
(SELECT SUM (transaction_quantity) FROM mtl_onhand_quantities
WHERE inventory_item_id = wro.inventory_item_id
AND organization_id = wro.organization_id) rm_stock
And my group by statement is of this way
group by
mt.subinventory_code,
item.description,
we.wip_entity_name
When i excute the query
It results in an error like this
ORA-00979: not a GROUP BY expression
When i remove the subquery from my select statement it works fine,but i want the result including the subquery plz help meI don't know the implications, but give this a try.
SELECT we.wip_entity_name JOB_NO,
mt.subinventory_code DEPARTMENT,
msi1.segment1 assembly_item,
msi1.description assm_desc,
sum(wdj.start_quantity) ASSL_QTY,
item.segment1 PART_CODE,
item.description PART_DESCRIPTION,
item.primary_uom_code UOM,
sum(wro.required_quantity) REQUIRED_QTY,
SUM((mt.transaction_quantity)*-1) ISSUED_QTY,
mfg.meaning JOB_STATUS,
SUM (mol.transaction_quantity) rm_stock
FROM mtl_material_transactions mt,
mtl_system_items item,
mtl_system_items msi1,
wip_e0ntities we,
wip_requirement_operations wro,
mfg_lookups mfg,
wip_discrete_jobs wdj
mtl_onhand_quantities mol
WHERE mt.inventory_item_id = wro.inventory_item_id
and mt.inventory_item_id=item.inventory_item_id
AND item.inventory_item_id = wro.inventory_item_id
AND we.wip_entity_id = wro.wip_entity_id
AND wdj.wip_entity_id = we.wip_entity_id
AND mfg.lookup_code = wdj.status_type
AND mt.organization_id = :org_id
AND mt.inventory_item_id = wro.inventory_item_id
AND item.organization_id = wro.organization_id
AND mt.transaction_source_id = we.wip_entity_id
AND msi1.inventory_item_id = wdj.primary_item_id
AND msi1.organization_id = wro.organization_id
AND we.wip_entity_id = wro.wip_entity_id
AND we.organization_id = wro.organization_id
AND mfg.lookup_code = wdj.status_type
AND mfg.lookup_type = 'WIP_JOB_STATUS'
AND mt.transaction_type_id = 35
AND wro.department_id <> 1001
AND wro.required_quantity > transaction_quantity*-1
AND mol.inventory_item_id = wro.inventory_item_id
AND mol.organization_id = wro.organization_id
group by mt.subinventory_code,
item.description,
item.segment1,
we.wip_entity_name,
mfg.meaning,
item.primary_uom_code,
msi1.segment1 ,
msi1.description;Cheers
Sarma. -
Can i use Lead function with Group by function
I could use this query and get right ouput since i define product id =2000
select product_id, order_date,
lead (order_date,1) over (ORDER BY order_date) AS next_order_date
from orders
where product_id = 2000;
But can i run this query by Group by Function
for example
select product_id, order_date,
lead (order_date,1) over (ORDER BY order_date) AS next_order_date
from orders
group by product_id ;
since data would be like and i need
Product_id order Date
2000 1-jan-09
2000 21-jan-09
3000 13-jan-09
3000 15-jan-09
4000 18-jan-09
4000 19-jan-09
output would be like for eg
Product_id order Date Next_date
2000 1-jan-09 21-jan-09
3000 13-jan-09 15-jan-09
4000 18-jan-09 19-jan-09Thanks everybody for ur help
i could exactly mention what i requred
create table SCHEDULER
( REF VARCHAR2(10),
NO NUMBER ,
PORT VARCHAR2(10),
ARRIVAL DATE ,
DEPARTURE DATE
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0677',1,'KUWAIT','1-Sep-09','02-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0677',2,'INDIA','5-Sep-09','07-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0677',3,'COLUMBO','8-Sep-09','09-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0677',4,'IRAN','10-Sep-09','12-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0677',5,'IRAQ','14-Sep-09','15-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0677',6,'DELHI','17-Sep-09','19-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0677',7,'POLAND','21-Sep-09','23-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0678',1,'INDIA','5-Sep-09','07-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0678',2,'COLUMBO','8-Sep-09','09-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0678',3,'IRAN','10-Sep-09','12-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0678',4,'IRAQ','14-Sep-09','15-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0678',5,'DELHI','17-Sep-09','19-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0678',6,'POLAND','21-Sep-09','23-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA0678',7,'GOA','1-Oct-09','02-Oct-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2372',1,'INDIA','1-Sep-09','02-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2372',2,'KERALA','3-Sep-09','03-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2372',3,'BOMBAY','4-Sep-09','04-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2373',1,'INDIA','5-Sep-09','06-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2373',2,'ANDHERI','6-Sep-09','07-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2376',1,'INDIA','5-Sep-09','07-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2420',1,'INDIA','5-Sep-09','06-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2420',2,'ANDHERI','7-Sep-09','08-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2420',3,'BURMA','10-Sep-09','11-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2420',4,'BENGAL','11-Sep-09','12-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2445',1,'INDIA','4-Sep-09','05-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2445',2,'BURMA','7-Sep-09','09-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2498',1,'BENGAL','8-Sep-09','08-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2498',2,'COCHIN','11-Sep-09','11-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2498',3,'LANKA','12-Sep-09','12-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2498',4,'COLUMBO','13-Sep-09','15-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2498',5,'INDIA','17-Sep-09','18-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2505',1,'COLUMBO','5-Sep-09','06-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2505',2,'GOA','8-Sep-09','09-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2505',3,'INDIA','13-Sep-09','15-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2510',1,'INDIA','4-Sep-09 06-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2510',2,'BENGAL','8-Sep-09 09-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2510',3,'GOA','10-Sep-09 11-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2513',1,'INDIA','7-Sep-09','09-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2513',2,'USA','11-Sep-09','11-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2513',3,'UK','12-Sep-09','13-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2520',1,'INDIA','4-Sep-09','06-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2520',2,'BENGAL','8-Sep-09','09-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2520',3,'GOA','10-Sep-09','11-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2526',1,'INDIA','5-Sep-09','07-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2526',2,'DUBAI','10-Sep-09','11-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2526',3,'GOA','13-Sep-09','15-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2526',4,'OMAN','17-Sep-09','18-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2526',5,'INDIA','19-Sep-09','20-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2527',1,'BURMA','7-Sep-09','08-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2527',2,'INDIA','9-Sep-09','10-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2527',3,'ANDHERI','10-Sep-09','16-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2532',1,'SHARJAH','3-Sep-09','04-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2532',2,'AEDXB','5-Sep-09','05-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2533',1,'AESHJ','2-Sep-09','02-Sep-09');
INSERT INTO SCHEDULER(REF,NO,PORT,ARRIVAL,DEPARTURE)
VALUES('VA2533',2,'INDIA','3-Sep-09','03-Sep-09');
COMMIT;
Suppose these records shows the REF travelling from one location to another with respect to date
We need to find out each REF GROUP WISE AND THE DATE OF TRAVELLING FOR SPECIFIED location travelling IE from STARTING FROM INDIA AND ENDING TO GOA
OUTPUT SHOULD BE LIKE DATA SHOWN BELOW
FROM LOCATION TO LOCATION
REF , NO , PORT , ARRIVAL ,DEPARTURE , REF , NO , PORT , ARRIVAL , DEPARTURE
VA0678 1 INDIA 5-Sep-09 07-Sep-09 VA0678 7 GOA 1-Oct-09 02-Oct-09
VA2510 1 INDIA 4-Sep-09 06-Sep-09 VA2510 3 GOA 10-Sep-09 11-Sep-09
VA2520 1 INDIA 4-Sep-09 06-Sep-09 VA2520 3 GOA 10-Sep-09 11-Sep-09
VA2526 1 INDIA 5-Sep-09 07-Sep-09 VA2526 3 GOA 13-Sep-09 15-Sep-09
---------------------------------------------------------------------------------------------------------------------------------------------------------------- -
Usage of group by function on oracle 8i
Is it possible to use a group by function in a query involving two tables
say for example the emp table and the dept table .i tried the following query
but it never worked.could some one clarify this.iam aware of CUBE and ROLLUP in 8i but i still want to try with group by for the sake of interoperability with 7.3/8.0 .Thanking you
SELECT E.DEPTNO,AVG(SAL),D.DNAME,
FROM EMP E,DEPT D
WHERE E.DEPTNO = D.DEPTNO
GROUP BY E.DEPTNOHi,
try the following:
SELECT E.DEPTNO,D.DNAME,AVG(SAL)
FROM EMP E,DEPT D
WHERE E.DEPTNO = D.DEPTNO
GROUP BY E.DEPTNO, D.DNAME
Regards
-Sree Ram -
I want to get an output using group by function of there is no data in the table to display a particular value
for eg:
SELECT TRUNC(updated_date) DATE1 , COUNT(1) COUNT FROM table
where TRUNC(updated_date) >=TRUNC(SYSDATE-18) AND TRUNC(updated_date) <=TRUNC(SYSDATE)
GROUP BY TRUNC(updated_date)
ORDER BY TRUNC(updated_date) DESC;
DATE1 COUNT
6/16/2012 14208
6/15/2012 307825
6/14/2012 172988
6/6/2012 138790
6/5/2012 167562
6/4/2012 51870
6/2/2012 130582
6/1/2012 239806
But i need the missed out date i.e 6/3/2012 - 0, 6/7/2012 - 0 to be displayed since there is no data on the repective dates.You should probably ask this question in the SQL and PL/SQL forum:
PL/SQL
You'll find answers quicker. -
IR group by function and timestamp datatype
Is there any limitations on the group by function e.g. based on the datatype.
My problem\misunderstanding is with the IR:
USING "GROUP BY"-
I have a TIMESTAMP column (columnname TM_TIME) and I can schose it in the group by clause but not in the function section. Is there any reason why and could I get it to work.
regards
Thorsten
Edited by: Fischert on 02.05.2012 03:05It really depends on the granularity that you need from the timestamp to make it meaningful. The key word is aggregation. So you should ask what level of aggregation do I need?
You can reduce the granularity from the fractional seconds to some lower level of granularity like seconds, minute, hours or even date (equivalent of trunc(date_column) using CAST or Truncating or to_date(to_char(...)) with appropriate format mask.
It just depends on the application and data.
E.g. for the LHC at CERN chasing the Higgins, timestamp is just not fine grained enough (you need 10 exp -23 or lower I guess !)
E.g. if you look at seismic data for oil exploration a hell of a lot happens in 5th and 6th decimal places in the timestamp.
But if you are looking at data logged by a normal SCADA system then maybe a second is detailed enough for the purpose.
In normal business application we are better off "rounding" the timestamp to some meaningful level for aggregation/ reporting.
Regards,
Maybe you are looking for
-
My home button is not working properly on my iPhone 4S???
HELP!
-
What is a "Frozen" Track?
Working in a Project, multiple Tracks mostly Real Instrument, copying and moving Regions between Tracks using option/drag and sometimes get a "Track is Frozen" message box. So I click Cancel and the box closes and the Region move has been done. This
-
Error on HSU_OUTPUT dir when using Headstart Utilities
Hi all, Does anyone have experience with the following problem: when I try to run the Headstart Utilities - Quality checks (elements) - Functions check, I get the following error: "Could not create file, possibly because output-directory does not exi
-
I tried to print to my Epson Artisan 810 today and it has repeatedly failed. It keeps saying that the "filter failed." I checked the discussions here and other forums and the most common solution is to redownload the drivers from Epson. I tried th
-
ICloud storage can I buy extra for just one month?
I Need to transfer data from one iPad to a new one, there's not enough storage in my current iPad to back it up. Can I purchase more for a month? thanks for any help. JO