Concatenation of data with the GROUP BY clause
Hi again!Following my previous thread...
I tried to apply the GROUP BY clause instead of preforming my query with RANK() to manage records NULL... I have a scenario where I also need to concatenate data from several lines.
CREATE TABLE T_EMP (NUMBER OF EMP_NO, NAME VARCHAR2 (20));
INSERT INTO T_EMP VALUES (1001, 'MARK');
INSERT INTO T_EMP VALUES (1002, 'DAVID');
INSERT INTO T_EMP VALUES (1003, "SHAUN");
INSERT INTO T_EMP VALUES (1004, "JILL");
CREATE TABLE T_EMP_DEPT (NUMBER OF EMP_NO, DEPT_NO NUMBER);
INSERT INTO T_EMP_DEPT VALUES (1001, 10);
INSERT INTO T_EMP_DEPT VALUES (1001, 20);
INSERT INTO T_EMP_DEPT VALUES (1002, 10);
INSERT INTO T_EMP_DEPT VALUES (1002, 20);
INSERT INTO T_EMP_DEPT VALUES (1002, 30);
INSERT INTO T_EMP_DEPT VALUES (1003, 20);
INSERT INTO T_EMP_DEPT VALUES (1003, 30);
INSERT INTO T_EMP_DEPT VALUES (1004, 10);
CREATE TABLE T_EMP_VISITS (NUMBER OF EMP_NO, DEPT_NO NUMBER, VISITED DATE);
INSERT INTO T_EMP_VISITS VALUES (1001, 10, 1 JAN 2009');
INSERT INTO T_EMP_VISITS VALUES (1002, 10, 1 JAN 2009');
INSERT INTO T_EMP_VISITS VALUES (1002, 30, 11 APR 2009');
INSERT INTO T_EMP_VISITS VALUES (1003, 20, 3 MAY 2009');
INSERT INTO T_EMP_VISITS VALUES (1003, 30: 14 FEB 2009');
COMMIT;
I have a T_EMP master table that stores the name and number of the emp. Each emp is required to visit some departments. This mapping is stored in the T_EMP_DEPT table. An employee can visit one or more departments. T_EMP_VISITS table stores the dates where the employee visited the services required. I need to view the report which should show when an employee all completed visits, which is the maximum date when it finished to visit all departments. If he did not visit any of the report should display date max, otherwise NULL. I was able to do using GROUP BY such proposed by Salim, but how do I show a list separated by commas of the services required for an employee in the same query.
SELECT
EMP_NO,
NAME,
MAX (DEPT_NO) KEEP (DENSE_RANK LAST ORDER BY VISITED) MAX_DEPT_NO,.
MAX (VISITED) KEEP (DENSE_RANK LAST ORDER PER VISIT) VISITS_COMP
DE)
SELECT
T_EMP. EMP_NO,
NAME,
T_EMP_DEPT. DEPT_NO,
VISITED
OF T_EMP
LEFT OUTER JOIN T_EMP_DEPT
ON T_EMP. EMP_NO = T_EMP_DEPT. EMP_NO
LEFT OUTER JOIN T_EMP_VISITS
ON T_EMP_DEPT. EMP_NO = T_EMP_VISITS. EMP_NO
AND T_EMP_DEPT. DEPT_NO = T_EMP_VISITS. DEPT_NO)
GROUP EMP_NO, NAME;
Output
EMP_NO NAME MAX_DEPT_NO VISITS_COMP
1001 MARK 20
1002 DAVID 20
1003 SHAUN 20 3 MAY 09
JILL 1004
Power required
EMP_NO NAME REQ_DEPTS MAX_DEPT_NO VISITS_COMP
1001 MARC 20 10.20
1002 DAVID 10,20,30 20
1003 SHAUN 20,30 20 3 MAY 09
JILL 10 1004
Can we do this in a single query?
Hello
user512647 wrote:
... Sanjay
The query you provided that stragg() use seems to work but my requirement is not in the result set. I don't know how to use stragg with
MAX (DEPT_NO) KEEP (DENSE_RANK LAST ORDER BY VISITED) MAX_DEPT_NO,.
MAX (VISITED) KEEP (DENSE_RANK LAST ORDER PER VISIT) VISITS_COMP
I need more, these two columns these gives me the date when they have completed all visits. If they missed any Department then the result must be NULL in the VISITS_COMP field.
Just add them to the SELECT clause:
SELECT t_emp.emp_no,
name,
STRAGG (t_emp_dept.dept_no) AS deptno,
MAX (t_emp_dept.dept_no) KEEP (DENSE_RANK LAST ORDER BY visited)
AS max_dept_no,
MAX (visited) AS visits_comp
FROM t_emp
LEFT OUTER JOIN t_emp_dept ON t_emp.emp_no = t_emp_dept.emp_no
LEFT OUTER JOIN t_emp_visits ON t_emp_dept.emp_no = t_emp_visits.emp_no
AND t_emp_dept.dept_no = t_emp_visits.dept_no
GROUP BY t_emp.emp_no
, name
;
The column called visit_comp is simply the last visited, regardless of how the employee visited departments.
If you want to have the NULL value if the employee has not yet visited all 3 departments:
... CASE
WHEN COUNT (DISTINCT t_emp_dept.dept_no) = 3
THEN MAX (visited)
END AS visits_comp
The 'magic number' 3 is the total number of departments.
If you want to understand the correct value of that at the time of the execution of the query, replace the code literal 3 hard with a scalar subquery.
Note that 'KEEP MAX (x) (DENSE_RANK OVER LAST SERVICE BY x)' (where the exact same column is used as an argument and that the ORDER BY column) is just "MAX (x)".
Tags: Database
Similar Questions
-
A question about the analytical function used with the GROUP BY clause in SHORT
Hi all
I created the following table named myenterprise
If I want to find which is the total sales by city? I'll run the following queryCITY STOREID MONTH_NAME TOTAL_SALES ---------- ---------- ---------- ---------------------- paris id1 January 1000 paris id1 March 7000 paris id1 April 2000 paris id2 November 2000 paris id3 January 5000 london id4 Janaury 3000 london id4 August 6000 london id5 September 500 london id5 November 1000
that works very well and produces the expected result, i.e.SELECT city, SUM(total_sales) AS TOTAL_SALES_PER_CITY FROM myenterprise GROUP BY city ORDER BY city, TOTAL_SALES_PER_CITY;
Now in one of my books SQL (Mastering Oracle SQL) I found another method by using the SUM, but this time as an analytic function. Here's what the method of the book suggests as an alternative to the problem:CITY TOTAL_SALES_PER_CITY ---------- ---------------------- london 10500 paris 17000
I know that the analytic functions are executed after the GROUP BY clause has been transformed completely and Unlike regular aggregate functions, they return their result for each line belonging to the partitions specified in the partition clause (if there is a defined partition clause).SELECT city, SUM(SUM(total_sales)) OVER (PARTITION BY city) AS TOTAL_SALES_PER_CITY FROM myenterprise GROUP BY city ORDER BY city, TOTAL_SALES_PER_CITY;
Now my problem is that I do not understand what we have to use two functions SUM? If we only use one only, i.e.
This generates the following error:SELECT city, SUM(total_sales) OVER (PARTITION BY city) AS TOTAL_SALES_PER_CITY FROM myenterprise GROUP BY city ORDER BY city, TOTAL_SALES_PER_CITY;
The error is generated for the line 2 column 11 which is, for the expression SUM (total_sales), well it's true that total_sales does not appear in the GROUP BY clause, but this should not be a problem, it has been used in an analytical function, so it is evaluated after the GROUP BY clause.Error starting at line 2 in command: SELECT city, SUM(total_sales) OVER (PARTITION BY city) AS TOTAL_SALES_PER_CITY FROM myenterprise GROUP BY city ORDER BY city, TOTAL_SALES_PER_CITY Error at Command Line:2 Column:11 Error report: SQL Error: ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression" *Cause: *Action:
So here's my question:
Why use SUM (SUM (total_sales)) instead of SUM (total_sales)?
Thanks in advance!
:)
In case you are interested, that's my definition of the table:
Edited by: dariyoosh on April 9, 2009 04:51DROP TABLE myenterprise; CREATE TABLE myenterprise( city VARCHAR2(10), storeid VARCHAR2(10), month_name VARCHAR2(10), total_sales NUMBER); INSERT INTO myenterprise(city, storeid, month_name, total_sales) VALUES ('paris', 'id1', 'January', 1000); INSERT INTO myenterprise(city, storeid, month_name, total_sales) VALUES ('paris', 'id1', 'March', 7000); INSERT INTO myenterprise(city, storeid, month_name, total_sales) VALUES ('paris', 'id1', 'April', 2000); INSERT INTO myenterprise(city, storeid, month_name, total_sales) VALUES ('paris', 'id2', 'November', 2000); INSERT INTO myenterprise(city, storeid, month_name, total_sales) VALUES ('paris', 'id3', 'January', 5000); INSERT INTO myenterprise(city, storeid, month_name, total_sales) VALUES ('london', 'id4', 'Janaury', 3000); INSERT INTO myenterprise(city, storeid, month_name, total_sales) VALUES ('london', 'id4', 'August', 6000); INSERT INTO myenterprise(city, storeid, month_name, total_sales) VALUES ('london', 'id5', 'September', 500); INSERT INTO myenterprise(city, storeid, month_name, total_sales) VALUES ('london', 'id5', 'November', 1000);
It is clear that thet Analytics is reduntant here...
You can even use AVG or any analytic function...SQL> SELECT city, 2 avg(SUM(total_sales)) OVER (PARTITION BY city) AS TOTAL_SALES_PER_CITY 3 FROM myenterprise 4 GROUP BY city 5 ORDER BY city, TOTAL_SALES_PER_CITY; CITY TOTAL_SALES_PER_CITY ---------- -------------------- london 10500 paris 17000
-
CFQUERYPARAM tag does not work in the GROUP BY clause
I get an error whenever I put a CFQUERYPARAM tag in a GROUP BY clause. I saw on another forum, someone had a similar problem with the ORDER BY clause
Here's a sample of what might look like my code
Select x, y, z of ABC
Group of < cfqueryparam value = 'x' cfsqltype = "cf_sql_float" >
Here is the error I get.
Run database query error.
[Macromedia] [Oracle JDBC Driver] [Oracle] ORA-00979: not a GROUP BY expression
Any idea?Cachedwithin and cachedafter functions store the query results in the RAM of the server. This means that, even if it is cached, when you run it, you get the cached result instead of going to the database to run again. This increases the speed of course, but if the data is changed during the period of cover, you have accuracy problems.
It creates no memory problems. In the administrator you book a certain amount of memory for the query cache. If you exceed this amount, the last request to shoot the first query on, or something like that.
In regards to what you're trying to do on the binding of variables to your group by clause, this isn't what cfqueryparam has been designed for. It has been designed to
where clauses (where it =
or insert queries (insert in my values in the table (field) ())
and things like that.You try to use it for anything other than what it was designed for, which explains why it does not work for you.
-
all my data records on the local disk c, how to share the data with the other drive, local drive d.
Hi Jasonbichard,
1. what type of drive is D? Is - this another partition on the same disk?
2 Windows operating system you are using?
You can change the location of the disk to save the data in the d: instead of C: and check if it helps.
a. navigate to the location (username) C:\Users\.
b. right click on the folder that you want to change the location, and then select Properties.
c. click on the location tab and change the location to D: drive.d. click on apply and Ok.
-
Hello
Data Modeler is a foreign key constraints do not recognize when synchronizing data with the model dictionary, although the foreign key is there (in the database that a data dictionary is read). I can't find any criterion when a foreign key is not recognized by the Data Modeler. Are there limits to the length of the attribute, or the number of columns in a foreign key, or other limitations which may lead to this behavior not to recognize a fk by Data Modeler? I have columns more than 32 characters. I compared with the fk is recognized by DM, but I can't find anything that indicates why it is not recognized.
I wonder if someone also has constraints of foreign keys that are not recognized in the comparison of data bases and model?
Thank you
Robert
Hi Robert,.
Thanks for the comments, I logged a bug.
Philippe
-
Dynamic action for validation of date with the notification message plugin
Hi all
Someone help me please with dynamic action for validation of date with the message notification plugin. I have a form with two elements of the date picker control and message notification plugin.
The requirement first user selects the exam is finished and then selects the date. So, if the date is greater than the date of the examination is over + 2 years then doesn't trigger the message notification plugin. I tried to create that dynamic action on the date picker date that triggers the scheduled issue notification message but I want to make conditional, I mean displays the message only if date of the selected is greater than the date of the exam is finished more than 2 years.
In terms simple, notification is displayed only if provided is superior to (date of the exam is completed + 2 years).
I use oracle apex 4.0 version and oracle 10g r2 database. I tried to reproduce the same requirement in my personal workspace. Here are the details. Please take a look.
Workspace: raghu_workspace
username: orton607
password: orton607
APP # 72193
PG # 1
Any help is appreciated.
Thanks in advance.
Orton.
You can get the value of the date of entry:
$(ele) .datePicker ('getDate');
So what to add functions such as:
function validateNotification (d1, d2) {}
Date1 var = $(d1) .datepicker ('getDate');
date2 var = $(d2) .datepicker ('getDate');
if(date1 && date2) {}
return ((date2.getTime()-date1.getTime())/(1000*24*60*60))>(365*2);
} else {}
Returns false;
}
}
The logic based on setting (I have two years from years of 365 days preceding)
Then in the D.A. specify a JavaScript expression as:
validateNotification ('P2_REVIEW_COMPLETED', this.triggeringElement.id)
Refer to page 2 for example.
-
Hello
I am considering buying stand-alone version 6 of Lightroom, but I wonder what does that "Always stay up to date with the latest innovations in photography from Adobe" is not available?
Will be the updated standalone for free with the new camera support?
Will be free updated standalone version with correction of defects?
If there are new features (minor version of lightroom), will not be available for the stand-alone version before the next major release?
Thanks for your help
My understanding is that LR6 will continue to receive bug fixes and new camera support updates, but not feature/news which receives from the CRD. See question 2 below on this post: CC Lightroom / Camera Raw FAQ
-
Detection of colliding with the grouped shapes
Hello world.
I'm having a problem with collision detection.
I do an underwater game where the submarines are groups of shapes (two ellipses and two small rectangles).
I use a circle (IARC) object for the missiles.
It detects collisions when I do this:
//////////////////////////////////////////////////
If (circ.intersects (sub.getBoundsInLocal ()))
{
System.out.println ("you been touched");
Returns true;
}
/////////////////////////////////////////////////
Or it detects collision if I do:
////////////////////////////////////////////////
If (sub.getChildren () .get (2) .intersects (circ.getBoundsInLocal ()))
{
System.out.println ("you been touched");
Returns true;
}
///////////////////////////////////////////////////////////////////////
(the child in the 2 index is the main body of the Sub - ellipse shape)
If I add an object not grouped as a simple rectangle shape and check the collisions on it then it detects very well. 'r' is the rectangle object.
////////////////////////////////////////////////////////////////////////
If (circ.intersects (r.getBoundsInLocal ()))
{
System.out.println ("you hit sucker");
Returns true;
}
///////////////////////////////////////////////////////////////////////////
Can we see why it will not work with the grouped shapes or a child of the Group? Or y at - it a simpler way that I'm missing?
Thanks a lot :)For a group shape, use the boundsInParent property.
circ.intersects(sub.getBoundsInParent())
-
Select the columns not in the group by clause
Hello
My version of DB is 10.0.2
I have query, where in I need to select two columns more but that should not be part of the group by Clause.
Does perform a work around?
SELECT F.custnum, V.Import_date, -- F.ErledigtMM,F.ErledigtGrundMM, (Columns to be selected additionally) nvl(sum(nvl(round(V.VerAuf16/1000,2), 0) +nvl(round(V.VerAuf17/1000,2), 0) + nvl(round(V.VerAuf18/1000,2), 0) + nvl(round(V.VerAuf19/1000,2), 0) + nvl(round(V.VerAuf20/1000,2), 0) + nvl(round(V.VerAufKAPUn/1000,2), 0) + nvl(round(V.VerAufKAPVz/1000,2), 0) ), 0) Mandantenforderung, nvl(sum(nvl(round(V.VerAuf4/1000,2), 0) ), 0) Inkassokosten, nvl(sum(nvl(round(V.VerAuf5/1000,2), 0) ), 0) KontoFuehrungsgebuehren, nvl(sum(nvl(round(V.VerAuf1/1000,2), 0) + nvl(round(V.VerAuf2/1000,2), 0) ), 0) Verwertungskosten, nvl(sum(nvl(round(V.VerAuf10/1000,2), 0) + nvl(round(V.VerAuf8/1000,2), 0) + nvl(round(V.VerAuf9/1000,2), 0) ), 0) Barauslagen, nvl(sum(nvl(round(V.VerAuf13/1000,2), 0) + nvl(round(V.VerAuf14/1000,2), 0) + nvl(round(V.VerAuf15/1000,2), 0) + nvl(round(V.VerAuf6/1000,2), 0) ),0) Gerichtskosten, nvl(sum(nvl(round(V.VerAuf11/1000,2), 0) + nvl(round(V.VerAuf12/1000,2), 0) + nvl(round(V.VerAuf7/1000,2), 0) ), 0) RAKosten, TO_CHAR(F.Created_Date,'MM.YYYY') Created_Date_MM , TO_CHAR(F.Created_Date,'YYYY') Created_Date_MMRRRR FROM FACM F, VA V WHERE F.custnum = DECODE(0,0,F.custnum,0) AND F.accnum = V.accnum GROUP BY F.custnum, V.Import_date,TO_CHAR(F.Created_Date,'MM.YYYY') ,TO_CHAR(F.Created_Date,'YYYY'))
Hello
Here's what I suggest:
select a.custnum, a.import_date, b.ErledigtMM, b.ErledigtGrundMM, a.Mandantenforderung, a.Inkassokosten, a.KontoFuehrungsgebuehren, a.Verwertungskosten, a.Barauslagen, a.Gerichtskosten, a.RAKosten, a.Created_Date_MM , a.Created_Date_MMRRRR from ( SELECT F.custnum, V.Import_date, nvl(sum(nvl(round(V.VerAuf16/1000,2), 0) +nvl(round(V.VerAuf17/1000,2), 0) + nvl(round(V.VerAuf18/1000,2), 0) + nvl(round(V.VerAuf19/1000,2), 0) + nvl(round(V.VerAuf20/1000,2), 0) + nvl(round(V.VerAufKAPUn/1000,2), 0) + nvl(round(V.VerAufKAPVz/1000,2), 0) ), 0) Mandantenforderung, nvl(sum(nvl(round(V.VerAuf4/1000,2), 0) ), 0) Inkassokosten, nvl(sum(nvl(round(V.VerAuf5/1000,2), 0) ), 0) KontoFuehrungsgebuehren, nvl(sum(nvl(round(V.VerAuf1/1000,2), 0) + nvl(round(V.VerAuf2/1000,2), 0) ), 0) Verwertungskosten, nvl(sum(nvl(round(V.VerAuf10/1000,2), 0) + nvl(round(V.VerAuf8/1000,2), 0) + nvl(round(V.VerAuf9/1000,2), 0) ), 0) Barauslagen, nvl(sum(nvl(round(V.VerAuf13/1000,2), 0) + nvl(round(V.VerAuf14/1000,2), 0) + nvl(round(V.VerAuf15/1000,2), 0) + nvl(round(V.VerAuf6/1000,2), 0) ),0) Gerichtskosten, nvl(sum(nvl(round(V.VerAuf11/1000,2), 0) + nvl(round(V.VerAuf12/1000,2), 0) + nvl(round(V.VerAuf7/1000,2), 0) ), 0) RAKosten, TO_CHAR(F.Created_Date,'MM.YYYY') Created_Date_MM , TO_CHAR(F.Created_Date,'YYYY') Created_Date_MMRRRR FROM FACM F, VA V WHERE F.custnum = DECODE(0,0,F.custnum,0) AND F.accnum = V.accnum GROUP BY F.custnum, V.Import_date,TO_CHAR(F.Created_Date,'MM.YYYY') ,TO_CHAR(F.Created_Date,'YYYY')) ) a, FACM b where b.custnum = a.custnum
By the way
WHERE F.custnum = DECODE(0,0,F.custnum,0) AND F.accnum = V.accnum
can be translated into
WHERE F.accnum = V.accnum
because
F.custnum = DECODE(0,0,F.custnum,0)
is always true
becauseDECODE(0,0,F.custnum,0)
means
if 0=0 then F.custnum else 0
-
request with the date of the Group's values
Hi all
I have an adate of table with 13 columns with a column named trandate
with the value as "2008-11-07 18:00:19.
Now I want to retrieve the values from the table as
SELECT count (*) from trandate which where ttype = '081' and T_FILE like 'mountain % ';
I want to count the records for all the months as
Jan-2009 - 120 (120 is the number of records satifying the above condition and belong to the period January 2009 (derived from trandate column)
Feb-2009 - 150 (150 is the number of records satifying the above condition and belong to the period February 2009 (derived from trandate column)
How can I rewrite the query
Thank you
Kai
Published by: KaiS on February 1st, 2011 04:23select to_char(trandate,'mmm - yyyy') trandate, count(1) from trandate where where ttype = '081' and T_FILE like 'monT%' group by to_char(trandate,'mmm - yyyy');
-
Need help in the optimization of the query with the Group and joins by clause
I'm having the problem by running the following query... It takes a lot of time. To simplify, I added the two tables FILE_STATUS = stores the file load details and COMM table Board table job showing records treated successfully and which was communicated to the other system real. Records with status = T is trasnmitted to another system and traansactions with P is waiting.
Here's the query I wrote to give me the details of the file that has been loaded into the system. He reads the table of State and the commission files to display the name of the file, total records loaded, total at the table of the commission and the number of records which has finally been passed successfully loaded (Status = T) with other systems.CREATE TABLE FILE_STATUS (FILE_ID VARCHAR2(14), FILE_NAME VARCHAR2(20), CARR_CD VARCHAR2(5), TOT_REC NUMBER, TOT_SUCC NUMBER); CREATE TABLE COMM (SRC_FILE_ID VARCHAR2(14), REC_ID NUMBER, STATUS CHAR(1)); INSERT INTO FILE_STATUS VALUES ('12345678', 'CM_LIBM.TXT', 'LIBM', 5, 4); INSERT INTO FILE_STATUS VALUES ('12345679', 'CM_HIPNT.TXT', 'HIPNT', 4, 0); INSERT INTO COMM VALUES ('12345678', 1, 'T'); INSERT INTO COMM VALUES ('12345678', 3, 'T'); INSERT INTO COMM VALUES ('12345678', 4, 'P'); INSERT INTO COMM VALUES ('12345678', 5, 'P'); COMMIT;
In production, this request has several joins and takes a long time to deal with... the main culprit for me is the join on the COMM table to count the number of number of transactions sent. Please can you give me tips to optimize this query to get results faster? What I need to delete the Group and use the partition or something else. Help, please!SELECT FS.CARR_CD ,FS.FILE_NAME ,FS.FILE_ID ,FS.TOT_REC ,FS.TOT_SUCC ,NVL(C.TOT_TRANS, 0) TOT_TRANS FROM FILE_STATUS FS LEFT JOIN ( SELECT SRC_FILE_ID, COUNT(*) TOT_TRANS FROM COMM WHERE STATUS = 'T' GROUP BY SRC_FILE_ID ) C ON C.SRC_FILE_ID = FS.FILE_ID WHERE FILE_ID = '12345678';
Don't know if it will be faster based on the information provided, but analytical functions offer an alternative approach;
select carr_cd, file_name, file_id, tot_rec, tot_succ, tot_trans from (select fs.carr_cd, fs.file_name, fs.file_id, fs.tot_rec, fs.tot_succ, count(case when c.status = 'T' then 1 else null end) over(partition by c.src_file_id) tot_trans, row_number() over(partition by c.src_file_id order by null) rn from file_status fs left join comm c on c.src_file_id = fs.file_id where file_id = '12345678') where rn = 1; CARR_CD FILE_NAME FILE_ID TOT_REC TOT_SUCC TOT_TRANS ------- -------------------- -------------- ---------- ---------- ---------- LIBM CM_LIBM.TXT 12345678 5 4 2
-
How to INSERT a SELECT statement with a GROUP BY clause on a table with an IDENTITY column?
n an application, I intend to truncate and insertion on a 12 c Oracle database, but have found this problem with a
IDENTITY
column. Even if theINSERT... SELECT
statement works on mostSELECT
uses I tried, if this statement was also aGROUP BY
clause, it does not work, delivering a "ORA-00979: not aGROUP BY
expression ' complaint. Some examples of code:create table aux ( owner_name varchar2(20), pet varchar2(20) );
insert into aux values ('Scott', 'dog');
insert into aux values ('Mike', 'dog');
insert into aux values ('Mike', 'cat');
insert into aux values ('John', 'turtle');
create table T1 (
id number generated always as identity,
owner_name varchar2(20),
pet_count number );
select owner_name, count(*) as pet_count from aux group by owner_name; -- works just fine
insert into T1 (owner_name, pet_count) select owner_name, count(*) as pet_count from aux group by owner_name; -- doesn't work
The select statement works by itself, but it fails as an INSERT... SELECT statement.
Appreciate the help!
Looks like a bug. You must open the SR with Oracle. Meanwhile, you could materialize select:
SQL > insert into T1 (owner_name, pet_count)
2 with t as (select / * + materialize * / owner_name, count (*) as pet_count to the owner_name group)
3. Select owner_name, pet_count t
4.3 lines were created.
SQL > select * from t1;
ID OWNER_NAME PET_COUNT
---------- -------------------- ----------
1 John 1
Scott 2 1
3 Mike 2SQL >
Keep in mind index THAT MATERIALIZE is undocumented.
SY.
-
Is this a bug with the grouping sets?
Version is the database of Oracle 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production
Shouldn't the superaggregate row have a 1 in the T column?SQL> with data as 2 (select rownum r, 'X' x, 1 n from dual connect by rownum <= 10) 3 select x 4 , sum(n) s 5 , (select sum(1) from dual where dummy = x) t 6 from data 7 group by grouping sets (x, ()) 8 / X S T - ---------- ---------- X 10 1 10 SQL>
Here's your QUERY with the addition of the GROUPING() function:
SQL> WITH data AS 2 ( 3 SELECT rownum r 4 , 'X' x 5 , 1 n 6 FROM dual 7 CONNECT BY rownum <= 10 8 ) 9 SELECT GROUPING(x) 10 , x 11 , sum(n) s 12 , (SELECT sum(1) FROM dual WHERE dummy = x) t 13 FROM data 14 GROUP BY GROUPING SETS (x, ()) 15 ; GROUPING(X) X S T ----------- - ---------- ---------- 0 X 10 1 1 10
In this case, the value of X is zero because it is a subtotal line. If you look at this statement execution plan:
PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------------------------------------------------ SQL_ID gumphwvgumqc4, child number 0 ------------------------------------- WITH data AS ( SELECT rownum r , 'X' x , 1 n FROM dual CONNECT BY rownum <= 10 ) SELECT /*+gather_plan_statistics*/ x , sum(n) s , (SELECT sum(1) FROM dual WHERE dummy = x) t FROM data GROUP BY GROUPING SETS (x, ()) Plan hash value: 1718326399 ----------------------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem | ----------------------------------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | | 2 |00:00:00.01 | 0 | | | | | 1 | SORT AGGREGATE | | 2 | 1 | 2 |00:00:00.01 | 4 | | | | |* 2 | TABLE ACCESS FULL | DUAL | 2 | 1 | 1 |00:00:00.01 | 4 | | | | | 3 | SORT GROUP BY ROLLUP | | 1 | 1 | 2 |00:00:00.01 | 0 | 2048 | 2048 | 2048 (0)| | 4 | VIEW | | 1 | 1 | 10 |00:00:00.01 | 0 | | | | | 5 | COUNT | | 1 | | 10 |00:00:00.01 | 0 | | | | | 6 | CONNECT BY WITHOUT FILTERING| | 1 | | 10 |00:00:00.01 | 0 | | | | | 7 | FAST DUAL | | 1 | 1 | 1 |00:00:00.01 | 0 | | | | ----------------------------------------------------------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 2 - filter("DUMMY"=:B1)
The value of X is related to the subquery. If X is zero this returns no rows (I'm sure you already knew that :)). I would say it should although confusing behavior. You get the same results if you use CUBE or ROLLUP as well.
-
Store data with the VM disks?
I read a few whitepapers that suggest a maximum number of VM are run by LUN. That being said, the recommendation would be to "drive with the virtual machine shop", or on a separate data store? In what senario would you do one of the two?
If you have group single disc less than 2 TB total in most cases there is no need to make separate data warehouses.
---
MCSA, MCTS, VCP, VMware vExpert 2009
-
Need help get data with the most recent date of entry into
Hey guys;
I need help with fine tuning a query to get the one with the most recent implementation.
Here's my current query:
/**********************************************
Select sge.seal_group_id,
SGE.equipment_id,
SGE.effective_date
of seal_group_equipment EMS.
seal_group sg
where equipment_id = 48801
AND EMS. SEAL_GROUP_ID = SG. SEAL_GROUP_ID
and sge.end_date is null
Group of sge.equipment_id, sge.seal_group_id, sge.effective_date
After having sge.effective_date = max (sge.effective_date)
******************************************************/
Which produces the following results:
SEAL_GROUP_ID - EQUIPMENT_ID - EFFECTIVE_DATE
25-48801 - 01/01/1993-00: 00:00
11730-48801 - 22/08/2003 08:42:11
What I really need, is to show only the line with the most recent date of entry into
I hope someone can help
Thank youMAX will not work because the SEAL_GROUP_ID could depart. I would say analytical:
select seal_group_id, equipment_id, effective_date from ( select sge.seal_group_id, sge.equipment_id, sge.effective_date, RANK() over (partition by equipment_id order by effective_date desc) r from seal_group_equipment sge, seal_group sg where equipment_id = 48801 AND SGE.SEAL_GROUP_ID = SG.SEAL_GROUP_ID and sge.end_date is null) where r = 1;
Keep in mind if two records have the same effective_date, they would both appear.
Note: query above has not been tested, since there is no script provided.
Maybe you are looking for
-
You can delete the history?
I was not able to remove any site.
-
Satellite L500 - external screen display after closing the lid?
HelloI use the Satellite L500 - 120 Notebook, and before installing Windows 7, the laptop has a function very uncomfortable, when I applied an external display.When I closed the lid of my laptop, Office was shown on my display (with the native resolu
-
module relay and DAQ Assistant
Hello world! I am a beginner, and currently dealing with simple business priori Labview. Recently I got a USB relay Module must be integrated into an alarm system. Let's say that if we get some more value than the other, the relay must be closed and
-
Sony album will save not received new images
Since the update of the application of the album that I got the problem again received and downloaded images will not appear in my album. All the old photos are always available. When you use the camera the image is saved in the Album. I've cleared t
-
HP 15-g092sa Notebook PC: my laptop won't boot
My partner has a laptop HP which is experiencing problems, it was about this yesterday loaded no problem, but then while working it seemed that its windows is crushed, so he tried to restart it, and now it does not start, it gets as much to "prepare