Case with group by function
I have the following script pulling from the work order module(*where clause omitted):
SELECT status,
COUNT(distinct NUM) countofWO FROM wo
GROUP BY status; Result set is: DS -1; WAPPR-2; INPRG-3;
I have another script that pulls from the equipment module:
select count(*) equipment_count from equipment; with result set 500
I need a sql statement that will provide me with the following results:
equipment_count - 500
sum(countofWO) for all status - 6
sum(countofWO) if status = DS - 1
sum(countofWO) if status = DS and INPRG - 4
sum(countofWO) if status = WAPPR and INPRG - 5;
I started by creating multiple instances of the select statement with different where clauses and UNION'ed them. It took 30 minutes to execute. I tried to use the case statement, but had issues with the group by
Hi,
Welcome to the forum!
Whenever you have a question, it helps to post:
(1) The version of Oracle (and any other relevant software) you're using
(2) A little sample data (just enough to show what the problem is) from all the relevant tables
(3) The results you want from that data
(4) Your best attempt so far (formatted)
(5) The full error message (if any), including line number
Executable SQL statements (like "CREATE TABLE AS ..." or "INSERT ..." statements) are best for (2).
If you can present your problem using commonly available tables (for example, tables in scott schema, or views in the data dictionary), then you can omit (2).
Formatted tabular output is okay for (3). Type these 6 characters:
{code}
(small letters only, inside curly brackets) before and after formatted text, to preserve spacing.
Without the information above, the best I can do is guess.
For the totals on the wo table, this might be what you want:
SELECT COUNT (DISTINCT num) AS all_status
, COUNT (DISTINCT CASE WHEN status = 'DS' THEN num END) AS ds
, COUNT (DISTINCT CASE WHEN status = 'DS'
OR status = 'INPRG' THEN num END) AS ds_inprg
, COUNT (DISTINCT CASE WHEN status = 'WAPPR'
OR status = 'INPRG' THEN num END) AS wappr_inprg
FROM wo
;You might use a cross-join to get the data from the equipment table in the same result set. Without the information above, I can't tell.
Similar Messages
-
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
---------------------------------------------------------------------------------------------------------------------------------------------------------------- -
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?
Thankshi, 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 -
Hi,
I am having a scenario like :
Column 1: BrokerList(dimension1)
Column 2 : Broker(dimension2)
Column 3 : Metric value(measure)
so i am having a case when (dimension 3) Custodian = 'ss' then sum(metirc) group by dimension1,dimension2 but the result value is not matching
BrokerList
Broker
Metric
a1
a
10
b
20
c
30
a1 :total
60
a2
a
50
c
60
d
10
a2:total
120
Grand total
180
Here the metric is based on other case condition.. so the total value is not matching.. Is there any other way to do a case function with group by funtions. Please advise.
regards,
GuruUse filter on metric by ss value and then go for group by from Criteria
something like
sum(FILTER(metric USING (Custodian = 'ss')) by dimension1,dimension2)
mark if helps
~ http://cool-bi.com -
Before I attempted using the 'groups tab' function for the first time, there was no icon (unlike currently). There is now the icon with for the function yet I am unable to remove it or its function.
Try a clean reinstall and delete the Firefox program folder and desktop shortcut before (re)installing a fresh copy of the current Firefox release.
Download a fresh Firefox copy and save the file to the desktop.
*Firefox 23.0: http://www.mozilla.org/en-US/firefox/all.html
Uninstall your current Firefox version, if possible, to cleanup the Windows registry and settings in security software.
*Do NOT remove personal data when you uninstall your current Firefox version, because all profile folders will be removed and you lose personal data like bookmarks and passwords from profiles of other Firefox versions.
Remove the Firefox program folder before installing that newly downloaded copy of the Firefox installer.
*(32 bit Windows) "C:\Program Files\Mozilla Firefox\"
*(64 bit Windows) "C:\Program Files (x86)\Mozilla Firefox\"
*It is important to delete the Firefox program folder to remove all the files and make sure that there are no problems with files that were leftover after uninstalling.
*http://kb.mozillazine.org/Uninstalling_Firefox
Your bookmarks and other personal data are stored in the Firefox profile folder and won't be affected by an uninstall and (re)install, but make sure that "remove personal data" is NOT selected when you uninstall Firefox.
*http://kb.mozillazine.org/Profile_folder_-_Firefox
*http://kb.mozillazine.org/Profile_backup
*http://kb.mozillazine.org/Standard_diagnostic_-_Firefox#Clean_reinstall -
Performance issues with pipelined table functions
I am testing pipelined table functions to be able to re-use the <font face="courier">base_query</font> function. Contrary to my understanding, the <font face="courier">with_pipeline</font> procedure runs 6 time slower than the legacy <font face="courier">no_pipeline</font> procedure. Am I missing something? The <font face="courier">processor</font> function is from [url http://www.oracle-developer.net/display.php?id=429]improving performance with pipelined table functions .
Edit: The underlying query returns 500,000 rows in about 3 minutes. So there are are no performance issues with the query itself.
Many thanks in advance.
CREATE OR REPLACE PACKAGE pipeline_example
IS
TYPE resultset_typ IS REF CURSOR;
TYPE row_typ IS RECORD (colC VARCHAR2(200), colD VARCHAR2(200), colE VARCHAR2(200));
TYPE table_typ IS TABLE OF row_typ;
FUNCTION base_query (argA IN VARCHAR2, argB IN VARCHAR2)
RETURN resultset_typ;
c_default_limit CONSTANT PLS_INTEGER := 100;
FUNCTION processor (
p_source_data IN resultset_typ,
p_limit_size IN PLS_INTEGER DEFAULT c_default_limit)
RETURN table_typ
PIPELINED
PARALLEL_ENABLE(PARTITION p_source_data BY ANY);
PROCEDURE with_pipeline (argA IN VARCHAR2,
argB IN VARCHAR2,
o_resultset OUT resultset_typ);
PROCEDURE no_pipeline (argA IN VARCHAR2,
argB IN VARCHAR2,
o_resultset OUT resultset_typ);
END pipeline_example;
CREATE OR REPLACE PACKAGE BODY pipeline_example
IS
FUNCTION base_query (argA IN VARCHAR2, argB IN VARCHAR2)
RETURN resultset_typ
IS
o_resultset resultset_typ;
BEGIN
OPEN o_resultset FOR
SELECT colC, colD, colE
FROM some_table
WHERE colA = ArgA AND colB = argB;
RETURN o_resultset;
END base_query;
FUNCTION processor (
p_source_data IN resultset_typ,
p_limit_size IN PLS_INTEGER DEFAULT c_default_limit)
RETURN table_typ
PIPELINED
PARALLEL_ENABLE(PARTITION p_source_data BY ANY)
IS
aa_source_data table_typ;-- := table_typ ();
BEGIN
LOOP
FETCH p_source_data
BULK COLLECT INTO aa_source_data
LIMIT p_limit_size;
EXIT WHEN aa_source_data.COUNT = 0;
/* Process the batch of (p_limit_size) records... */
FOR i IN 1 .. aa_source_data.COUNT
LOOP
PIPE ROW (aa_source_data (i));
END LOOP;
END LOOP;
CLOSE p_source_data;
RETURN;
END processor;
PROCEDURE with_pipeline (argA IN VARCHAR2,
argB IN VARCHAR2,
o_resultset OUT resultset_typ)
IS
BEGIN
OPEN o_resultset FOR
SELECT /*+ PARALLEL(t, 5) */ colC,
SUM (CASE WHEN colD > colE AND colE != '0' THEN colD / ColE END)de,
SUM (CASE WHEN colE > colD AND colD != '0' THEN colE / ColD END)ed,
SUM (CASE WHEN colD = colE AND colD != '0' THEN '1' END) de_one,
SUM (CASE WHEN colD = '0' OR colE = '0' THEN '0' END) de_zero
FROM TABLE (processor (base_query (argA, argB),100)) t
GROUP BY colC
ORDER BY colC
END with_pipeline;
PROCEDURE no_pipeline (argA IN VARCHAR2,
argB IN VARCHAR2,
o_resultset OUT resultset_typ)
IS
BEGIN
OPEN o_resultset FOR
SELECT colC,
SUM (CASE WHEN colD > colE AND colE != '0' THEN colD / ColE END)de,
SUM (CASE WHEN colE > colD AND colD != '0' THEN colE / ColD END)ed,
SUM (CASE WHEN colD = colE AND colD != '0' THEN 1 END) de_one,
SUM (CASE WHEN colD = '0' OR colE = '0' THEN '0' END) de_zero
FROM (SELECT colC, colD, colE
FROM some_table
WHERE colA = ArgA AND colB = argB)
GROUP BY colC
ORDER BY colC;
END no_pipeline;
END pipeline_example;
ALTER PACKAGE pipeline_example COMPILE;Edited by: Earthlink on Nov 14, 2010 9:47 AM
Edited by: Earthlink on Nov 14, 2010 11:31 AM
Edited by: Earthlink on Nov 14, 2010 11:32 AM
Edited by: Earthlink on Nov 20, 2010 12:04 PM
Edited by: Earthlink on Nov 20, 2010 12:54 PMEarthlink wrote:
Contrary to my understanding, the <font face="courier">with_pipeline</font> procedure runs 6 time slower than the legacy <font face="courier">no_pipeline</font> procedure. Am I missing something? Well, we're missing a lot here.
Like:
- a database version
- how did you test
- what data do you have, how is it distributed, indexed
and so on.
If you want to find out what's going on then use a TRACE with wait events.
All nessecary steps are explained in these threads:
HOW TO: Post a SQL statement tuning request - template posting
http://oracle-randolf.blogspot.com/2009/02/basic-sql-statement-performance.html
Another nice one is RUNSTATS:
http://asktom.oracle.com/pls/asktom/ASKTOM.download_file?p_file=6551378329289980701 -
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. -
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 -
DISTINCT on object type collection not working with GROUP BY
Hello,
I have an object type with collection defined as:
create or replace type individu_ot as object (
numero_dossier number(10),
code_utilisateur varchar2(8 char),
nom varchar2(25 char),
prenom varchar2(25 char),
map member function individu_map return number
create or replace type body individu_ot is
map member function individu_map return number
is
begin
return SELF.numero_dossier;
end individu_map;
end;
create or replace type individu_ntt is table of individu_ot
/When I use it in simple SQL without any aggregation, the distinct keyword works well and returns me the distinct entry of my object type in the collection:
SQL> select cast(collect(distinct individu_ot(indivmc.numero_dossier, indivmc.idul, indivmc.nom, indivmc.prenom)) as individu_ntt) as distinct_list
from site_section_cours sisc
inner join enseignant_section_mc ensemc
on sisc.code_session = ensemc.code_session and
sisc.numero_reference_section_cours = ensemc.numero_reference_section_cours
inner join individu_mc indivmc
on ensemc.numero_dossier_pidm = indivmc.numero_dossier
where sisc.seq_site_cours = 6
DISTINCT_LIST(NUMERO_DOSSIER,CODE_UTILISATEUR,NOM,PRENOM)
INDIVIDU_NTT(INDIVIDU_OT(15,PROF5,Amidala,Padmé))
1 row selected.However in SQL with broader selection with group by, the distinct isn't working anymore.
SQL> select *
from (
select sisc.seq_site_cours,
cast(collect(distinct individu_ot(indivmc.numero_dossier, indivmc.idul, indivmc.nom, indivmc.prenom)) as individu_ntt) as distinct_list
from site_section_cours sisc
inner join enseignant_section_mc ensemc
on sisc.code_session = ensemc.code_session and
sisc.numero_reference_section_cours = ensemc.numero_reference_section_cours
inner join individu_mc indivmc
on ensemc.numero_dossier_pidm = indivmc.numero_dossier
group by sisc.seq_site_cours
where seq_site_cours = 6
SEQ_SITE_COURS DISTINCT_LIST(NUMERO_DOSSIER,CODE_UTILISATEUR,NOM,PRENOM)
6 INDIVIDU_NTT(INDIVIDU_OT(15,PROF5,Amidala,Padmé),INDIVIDU_OT(15,PROF5,Amidala,Padmé),INDIVIDU_OT(15,PROF5,Amidala,Padmé),INDIVIDU_OT(15,PROF5,Amidala,Padmé),INDIVIDU_OT(15,PROF5,Amidala,Padmé))
1 row selected.there is case where I need to return more than one collections, with distinct entries in it.
Is there something I am missing?
Thanks
BrunoNot a bug, rather an undocumented feature.
Here are some alternatives you might want to test :
1) Using the SET operator to eliminate duplicates :
SELECT sisc.seq_site_cours,
set(
cast(
collect(individu_ot(indivmc.numero_dossier, indivmc.idul, indivmc.nom, indivmc.prenom))
as individu_ntt
) as distinct_list
FROM site_section_cours sisc
INNER JOIN enseignant_section_mc ensemc
ON sisc.code_session = ensemc.code_session
AND sisc.numero_reference_section_cours = ensemc.numero_reference_section_cours
INNER JOIN individu_mc indivmc
ON ensemc.numero_dossier_pidm = indivmc.numero_dossier
GROUP BY sisc.seq_site_cours
;2) Using MULTISET with a subquery
SELECT sisc.seq_site_cours,
CAST(
MULTISET(
SELECT distinct
indivmc.numero_dossier, indivmc.idul, indivmc.nom, indivmc.prenom
FROM enseignant_section_mc ensemc
INNER JOIN individu_mc indivmc
ON ensemc.numero_dossier_pidm = indivmc.numero_dossier
WHERE sisc.code_session = ensemc.code_session
AND sisc.numero_reference_section_cours = ensemc.numero_reference_section_cours
AS individu_ntt
) AS distinct_list
FROM site_section_cours sisc
; -
Segmentation in CRM 2007 (6.0) with AND/OR functionality
We have been trying to understand the new functionality in Segment Builder for using the new multi-filter functionality of AND and OR. We know about the multiple filters and using them to u201Ccombineu201D results, but our question is specifically about using the AND functionality because it does not seem to be working (it works just like the OR functionality and we expected it to behave differently). We have tried several scenarios and cannot see any difference in the results whether we use AND Across, AND Within, OR Across, OR Within.
So let me give a more specific example. Let's say I have two attributes, color and type. Color=Red, Blue, Green, White, Black; Type=Sport, Sedan, Truck, Van. Then I have the following assignments of the attributes: Red=Sally, Tom, Joe, and Dick; Truck=Bill, Tom, Sam, Debbie, and Joe.
If I use OR Across attributes, with Red and Truck, I will get those who have any color of Red OR those who have any type of Truck: Result is Sally, Tom, Joe, Dick, Bill, Sam, and Debbie.
If I use AND Across attributes, with Red and Truck, I should only get those who have a Red Truck: Result is Tom and Joe. However, what we have found in our testing is that we get the same result with AND Across attributes as we do with OR Across attributes.
How do we get just those who have both a Red color and a Truck type? Or is the AND functionality broken? If I cannot get this result using the AND/OR functionality, how can i get this result in Segment Builder?
Also see this related Thread: Segment builder and/or functionality....
Thanks, BryanSo, let me add the step to this that specifically is not working. Let's add to this the fact that to the Material of Truck, there are attached Warranties and Service Contracts (Truck is part of an equipment master). So, now I want all Red Trucks which have a Warranty expiring this month AND no Service Contract. To further explain, the equipment master has a Type (Truck), and either a Warranty or Contract attached to it (either, both, or neither). So I want to use the AND to identify Trucks that have no Warranty or and expiring one, AND Trucks that have no Service Contract. I only want Trucks that meet both conditions at the same time.
In our testing, we have found that using the AND with the KEEP functionality it works in all cases EXCEPT when someone owns more than one Truck. For example, lets say Bob owns two Trucks, one has no Warranty but a Service Contract, and the other Truck has a Warranty, but no Service Contract. In this case, Bob should not show up in the Target Group because he does not own a Truck that has no Warranty AND no Service Contract. However, because he shows up in the no Warranty target group for one Truck, and the no Service Contract target group for the other Truck, when you do a KEEP you get Bob in the final Target Group. Is there any way to find just Trucks that have both no Warranty and no Service Contract?
Thanks, Bryan -
Is it possible to rank with group condition?
Is it possible to rank with group condition? for example I have following dataset.
RANK() OVER (partition BY PC.POLICY_NUMBER, PC.TRANSACTION_TYPE, COV_CHG_EFF_DATE order by PC.POLICY_NUMBER, COV_CHG_EFF_DATE, PC.TIMESTAMP_ENTERED) AS RNK,
POLICY_NUMBER TRANSACTION_TYPE COV_CHG_EFF_DATE TIMESTAMP_ENTERED Rank
10531075PQ 01 01/FEB/2009 15/SEP/2009 01:16:09.356663 AM 1
10531075PQ 01 01/FEB/2009 15/SEP/2009 01:16:09.387784 AM 2
10531075PQ 02 15/OCT/2009 16/OCT/2009 04:40:24.564928 PM 1
10531075PQ 02 15/OCT/2009 16/OCT/2009 04:40:24.678118 PM 2
10531075PQ 10 15/OCT/2009 16/OCT/2009 04:45:20.290117 PM 1
10531075PQ 10 15/OCT/2009 16/OCT/2009 04:40:29.088737 PM 2
10531075PQ 09 15/OCT/2009 16/OCT/2009 04:40:29.088737 PM 1 (expected 3)
10531075PQ 06 17/OCT/2009 17/OCT/2009 04:45:20.290117 PM 1
10531075PQ 07 17/OCT/2009 17/OCT/2009 04:40:29.088737 PM 1 (expected 2) I want to group rank based by transaction IDs. For ex, '09' and '10' as one set and '06' an '07' as another set. Instead of rank from start, I want rank continue for any occurance of '09' or '10'. In the above example, for the following row, I am expecting rank 3 as there are 2 transaction '10' already exist for same COV_CHG_EFF_DATE.
10531075PQ 09 15/OCT/2009 16/OCT/2009 04:40:29.088737 PM 1 (expected 3)
I wonder if it possible with Rank or another other analytical function. I am not looking for exact working code, but will appreciate if someone give me idea/tips. Sample table, and test data if anyone want experiment
drop table PC_COVKEY_PD;
create table PC_COVKEY_PD (
POLICY_NUMBER varchar(30),
TERM_IDENT varchar(3),
COVERAGE_NUMBER varchar(3),
TRANSACTION_TYPE varchar(3),
COV_CHG_EFF_DATE date,
TIMESTAMP_ENTERED timestamp
delete from PC_COVKEY_PD;
commit;
insert into PC_COVKEY_PD values ('10531075PQ', '021', '002', '01', to_date('01/FEB/2009','DD/MM/YYYY'), cast('15/SEP/2009 01:16:09.356663 AM' as timestamp));
insert into PC_COVKEY_PD values ('10531075PQ', '021', '001', '01', to_date('01/FEB/2009','DD/MM/YYYY'), cast('15/SEP/2009 01:16:09.387784 AM' as timestamp));
insert into PC_COVKEY_PD values ('10531075PQ', '021', '004', '02', to_date('15/OCT/2009','DD/MM/YYYY'), cast('16/OCT/2009 04:40:24.164928 PM' as timestamp));
insert into PC_COVKEY_PD values ('10531075PQ', '021', '004', '02', to_date('15/OCT/2009','DD/MM/YYYY'), cast('16/OCT/2009 04:40:24.264928 PM' as timestamp));
insert into PC_COVKEY_PD values ( '10531075PQ', '021', '005', '10', to_date('15/OCT/2009','DD/MM/YYYY'), cast('16/OCT/2009 04:40:24.364928 PM' as timestamp));
insert into PC_COVKEY_PD values ('10531075PQ', '021', '002', '10', to_date('15/OCT/2009','DD/MM/YYYY'), cast('16/OCT/2009 04:40:24.464928 PM' as timestamp));
insert into PC_COVKEY_PD values ( '10531075PQ', '021', '004', '09', to_date('15/OCT/2009','DD/MM/YYYY'), cast('16/OCT/2009 04:40:24.564928 PM' as timestamp));
insert into PC_COVKEY_PD values ('10531075PQ', '021', '004', '06', to_date('22/NOV/2011','DD/MM/YYYY'), cast('17/OCT/2009 04:40:24.564928 PM' as timestamp));
insert into PC_COVKEY_PD values ('10531075PQ', '021', '004', '07', to_date('22/NOV/2011','DD/MM/YYYY'), cast('17/OCT/2009 04:40:24.664928 PM' as timestamp));
commit;
SELECT POLICY_NUMBER,
TERM_IDENT,
COVERAGE_NUMBER,
TRANSACTION_TYPE,
COV_CHG_EFF_DATE,
TIMESTAMP_ENTERED,
RANK() OVER (partition BY PC.POLICY_NUMBER, PC.TERM_IDENT, PC.TRANSACTION_TYPE, PC.COV_CHG_EFF_DATE
order by PC.POLICY_NUMBER, PC.TERM_IDENT, PC.COV_CHG_EFF_DATE, PC.TIMESTAMP_ENTERED) AS RNK
FROM PC_COVKEY_PD PC
ORDER BY PC.POLICY_NUMBER, PC.TERM_IDENT, PC.COV_CHG_EFF_DATE, PC.TIMESTAMP_ENTERED ;Edited by: 966820 on 30-Oct-2012 19:26Hi,
966820 wrote:
I want to group rank based by transaction IDs. For ex, '09' and '10' as one set and '06' an '07' as another set. So, whenever you see '09', you want to treat it as '10', and whenever you see '06', you want to act as if it were '07'.
You can use CASE to map '09' to '10', and '06' to '07'. like this:
, RANK () OVER ( PARTITION BY policy_number
, term_ident
, CASE transaction_type
WHEN '06' THEN '07'
WHEN '09' THEN '10'
ELSE transaction_type
END
, cov_chg_eff_date
ORDER BY timestamp_entered
) AS rnkWhen you "PARTITION BY x", there's no point in saying "ORDER BY x" in the same analytic clause.
Thanks for posting the CREATE TABLE and INSERT statements.
Thanks, too, for posting your query, though, as mentioned already, it's unreadable. When posting any formatted text (and code should always be formatted), type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing.
See the forum FAQ {message:id=9360002} -
Text-input component with auto-complete functionality for mobile
Hey guys,
I wonder if there is a text-input component with auto-complete functionality optimized for mobile development with AIR like the one that Android provides natively as you can see here http://developer.android.com/reference/android/widget/AutoCompleteTextView.html and here http://developer.android.com/resources/tutorials/views/hello-autocomplete.html.
At least I didn't find it in the latest SDK and I think such a component is pretty standard nowadays. Or did I simply overlook it?
Thank you guys for any advice.
Regards,
ThiloHi relaxtraja,
thanks for your reply!
I knew that there are a lot of auto-complete scripts on the web. What I was hoping was, that Adobe provides an officially supported auto-complete component optimized for mobile as part of the SDK (like in the Android SDK as I pointed out), but that doesn't seem to be the case. It's sad, because it's an essential part of modern UIs in mobile applications.
I've already created an auto-complete component by myself based on Spark components and I will end up using it. But I will have to optimize it for mobile applications beforehands. I just think something this essential should be provided by the SDK.
However, thanks for posting!
Regards,
Thilo -
Passing arrays with Call Library Function does not work after application builder
Calling a DLL with Call Library Function which requires an array of data works correctly in Labview, but after building an exe with application builder, the call no longer works. Dereferecing the pointer in the DLL retuns all 0s and not the actual values.
Solved!
Go to Solution.
Attachments:
TEST.zip 28 KBI did not run your code because it is a little unclear to me what it does.
Two things:
First, is the DLL you are calling the DLL-ified version of PopUpNames.vi? Then the problem is likely that the panel is not being built into the DLL.
When LabView builds an application / dll, it strips the front panel and block diagram from all VIs that it doesn't think need to show a panel at run time. This reduces file size and increases code security. The App Builder's panel inclusion logic can be overridden by Build Specifications -> Source File Settings -> Remove front panel. A better method is to put a property node on a control in a window you want to show marking it "visible"; this is sufficient to tell the App Builder it should keep the panel.
Currently Source File Settings shows "no dependencies" (clearly incorrect---another evil side effect of Express VIs I guess) but if you change the settings as shown below to keep ALL panels, one might hope the App Builder can figure it to keep the panel when it deconstructs the Express VI. (Alternatively convert the Express VI into a regular one.)
A second comment: I am a bit flummoxed at the larger goal here. You are calling LabView DLL from LabView, which doesn't make a lot of sense, so I assume your larger goal is to call LabView from C or vice-versa. In that case be aware that your DLL is x86 (32-bit) but you are passing 64-bit ints as your pointers. In this case it is 32-bit LabView with 32-bit pointers in embedeed in 64-bit containers calling 32-bit LabView with 32-bit pointers in embedeed in 64-bit containers, so it all works, but if your going to call this from C or whatnot you're going to have to follow that same design.
When calling C code the LabView Call Library Function does have a "unsigned pointer-sized integer" data type that always appears to be 64 bits in the dev env but which actually passes a 64 or 32-bit int to the DLL depending on the environment. The "pointer sized int" has to be 64 bits in the "LabView" part of the code because LabView's strong typing requires the data type to be determined at compile time. Casting all pointers to the largest data type in LabView makes it possible to write platform-independent code, but down at the Call Library level you still have to put the right number of bytes on the stack. -
Custom IKM Knowledge Modules are not working with Group By Clause
Hi All,
I am facing an issue with custom IKM knowledge modules. Those are IKM Sql Incremental Update and IKM Sql Control Append.
My Scenario is
1. Created an interface with table on source and temporary datastore with some columns in target.
2. In the Interface on the target i defined one column as UD1 and other column as UD2 for which group by to be implemented .
3. Customized IKM Sql Incremental Update with " Group by " by making modification in my IKM Sql Incremental Update
detail step "Insert flow into I$ table" i.e., i replaced like this whereever i find this <%=odiRef.getGrpBy()%> API
Group By
<%=snpRef.getColList("","[EXPRESSION]","","","UD1")%>,
<%=snpRef.getColList("","[EXPRESSION]","","","UD2")%>
Up to UD5.
. Here in the place of [EXPRESSION] i passed column names for UD1 similarly other column name for [EXPRESSION] of UD2.
4.Made all the proper mappings and also selected the KM's needed for that interface like CKM Sql ,IKM Sql Incremental Update.
5. Executed the Interface with global context.
Error i am getting in this case is :
Caused By: java.sql.SQLSyntaxErrorException: ORA-00979: not a GROUP BY expression
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:462)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:405)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:931)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:481)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:205)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:548)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:217)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1115)
Here the columns in the select clause are there in Group By also.
I did the same scenario using IKM Sql to file append .In that case i am able to do it. But not with the above mentioned KM's. Please let me know if any one know it or tried with this, as it is
high priority for me.
Please help me out.
Thanks,
keerthiHi Keerthi,
If your are transfering data from Oracle to Oracle (I means source is oracle Db and target is also oracle DB) then use below KM's and group by will come automatically based on the key values you selected on interface target datastore
1) CKM Oracle
2) LKM Oracle to Oracle (DBLINK)
3) IKM Oracle Incremental Update (MERGE)
Hope this will helps to resolve your issue
Regards,
Phanikanth
Maybe you are looking for
-
May someone please tell me how to delete the loops in Garageband that came with Soundtrack Pro? The the thing is, I uninstalled Soundtrack pro and it's loops. But Garageband still has SP's loops in the loop browser, but the loops are gone. Poof! Not
-
How do i map one field to another in control file via SQL Loader
Can someone please reply back to this question Hi, I have a flat file (student.dat delimiter %~| ) using control file (student.ctl) through sql loader. Here are the details. student.dat student_id, student_firstname, gender, student_lastName, student
-
Hi folks, I have a report, but I run it only the first page is displayed. (query returns enough data for displaying more then 1 page). I checked every repeat frame and also normal frame within repoirt and it's "Print Object On" property (it's setup f
-
Cannot edit DC metadata of a project
I have checked out DC in writable mode. ie from inactive DCs but in the project, i am not able to edit the DC metadata part. and i am able to edit the web dynpro component part this problem started after i re imported a model which was already added
-
Creating Links in Adobe Presenter/Breeze
Here is the situation I need help with - I need to create a CD with the following: A "Breezo" autorun file (easy enough), but in that "Breezo" autorun, I need to have links to a series of 11 published "Breezos" and it all needs to be on a CD. Is this