SQL insert with select statement having strange results
So I have the below sql (edited a bit). Now here's the problem.
I can run the select statement just fine, i get 48 rows back. When I run with the insert statement, a total of 9062 rows are inserted. What gives?
<SQL>
INSERT INTO mars_aes_data
(rpt_id, shpdt, blno, stt, shpr_nad, branch_tableS, csgn_nad,
csgnnm1, foreign_code, pnt_des, des, eccn_no, entity_no,
odtc_cert_ind, dep_date, equipment_no, haz_flag, schd_no,
schd_desc, rec_value, iso_ulti_dest, odtc_exempt, itn,
liscence_no, liscence_flag, liscence_code, mblno, mot,
cntry_load, pnt_load, origin_state, airline_prefix, qty1, qty2,
ref_val, related, routed_flag, scac, odtc_indicator, seal_no,
line_no, port_export, port_unlading, shipnum, shprnm1, veh_title,
total_value, odtc_cat_code, unit1, unit2)
SELECT 49, schemaP.tableS.shpdt, schemaP.tableS.blno,
schemaP.tableS.stt, schemaP.tableS.shpr_nad,
schemaP.tableM.branch_tableS, schemaP.tableS.csgn_nad,
schemaP.tableS.csgnnm1, schemaP.tableD.foreign_code,
schemaP.tableS.pnt_des, schemaP.tableS.des,
schemaP.tableD.eccn_no, schemaP.tableN.entity_no,
schemaP.tableD.odtc_cert_ind, schemaP.tableM.dep_date,
schemaP.tableM.equipment_no, schemaP.tableM.haz_flag,
schemaP.tableD.schd_no, schemaP.tableD.schd_desc,
schemaP.tableD.rec_value,
schemaP.tableM.iso_ulti_dest,
schemaP.tableD.odtc_exempt, schemaP.tableM.itn,
schemaP.tableD.liscence_no,
schemaP.tableM.liscence_flag,
schemaP.tableD.liscence_code, schemaP.tableS.mblno,
schemaP.tableM.mot, schemaP.tableS.cntry_load,
schemaP.tableS.pnt_load, schemaP.tableM.origin_state,
schemaP.tableM.airline_prefix, schemaP.tableD.qty1,
schemaP.tableD.qty2,
schemaC.func_getRefs@link (schemaP.tableS.ptt, 'ZYX'),
schemaP.tableM.related, schemaP.tableM.routed_flag,
schemaP.tableM.scac, schemaP.tableD.odtc_indicator,
schemaP.tableM.seal_no, schemaP.tableD.line_no,
schemaP.tableM.port_export,
schemaP.tableM.port_unlading, schemaP.tableS.shipnum,
schemaP.tableS.shprnm1, schemaP.tableV.veh_title,
schemaP.tableM.total_value,
schemaP.tableD.odtc_cat_code, schemaP.tableD.unit1,
schemaP.tableD.unit2
FROM schemaP.tableD@link,
schemaP.tableM@link,
schemaP.tableN@link,
schemaP.tableS@link,
schemaP.tableV@link
WHERE tableM.answer IN ('123', '456')
AND SUBSTR (tableS.area, 1, 1) IN ('A', 'S')
AND entity_no IN
('A',
'B',
'C',
'D',
'E',
AND TO_DATE (SUBSTR (tableM.time_stamp, 1, 8), 'YYYYMMDD')
BETWEEN '01-Mar-2009'
AND '31-Mar-2009'
AND tableN.shipment= tableD.shipment(+)
AND tableN.shipment= tableS.shipnum
AND tableN.shipment= tableM.shipment(+)
AND tableN.shipment= tableV.shipment(+)
<SQL>
Edited by: user11263048 on Jun 12, 2009 7:23 AM
Edited by: user11263048 on Jun 12, 2009 7:27 AM
Can you change this:
BETWEEN '01-Mar-2009'
AND '31-Mar-2009'To this:
BETWEEN TO_DATE('01-Mar-2009', 'DD-MON-YYYY')
AND TO_DATE('31-Mar-2009','DD-MON-YYYY')That may make no difference but you should never rely on implicit conversions like that, they're always likely to cause you nasty surprises.
If you're still getting the discrepancy, instead of and INSERT-SELECT, can you try a CREATE TABLE AS SELECT... just to see if you get the same result.
Similar Messages
-
SQL Insert and Select statements on same page
Newbie here.
Is it possible to insert data from a form to a database and then get data from the database using "select" from the same page.
I have a form that submits the authors details to an Access database but I need to get the AuthorID (which is an autonumber in the table) to show on the same page.
<%@ page language="java" contentType="text/html" import="java.sql.*" %>
<html>
<head>
<title>Confirm Details Submission</title>
</head>
</body>
<%
String title = request.getParameter("title");
String surname = request.getParameter("surname");
surname = surname.replaceAll("'", "''");
String forename = request.getParameter("forename");
forename = forename.replaceAll("'", "''");
String address = request.getParameter("address");
address = address.replaceAll("'", "''");
String address2 = request.getParameter("address2");
address2 = address2.replaceAll("'", "''");
String town = request.getParameter("town");
town = town.replaceAll("'", "''");
String postcode = request.getParameter("postcode");
String email = request.getParameter("email");
email = email.replaceAll("'", "''");
String dob = request.getParameter("dob");
String telephone = request.getParameter("telephone");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection Conn = DriverManager.getConnection("jdbc:odbc:Novbase","","");
Statement Stmt = Conn.createStatement();
String Query = ("INSERT INTO Author (Title, Surname, Forename, Address, Address2, Town, Postcode, EMail, DOB, TelNo) VALUES ('" + title + "', '" + surname + "', '" + forename + "', '" + address + "', '" + address2 + "', '" + town + "', '" + postcode + "', '" + email + "', '" + dob + "', '" + telephone +"')");
//Stmt.executeUpdate(Query) is where the records are inserted.
int SQLStatus = Stmt.executeUpdate(Query);
if(SQLStatus != 0)
%>
Thank you. Your Author ID number is ??
<%
else
%>
Error - Your details have not been submitted</font></h4>
<br>
Please click back on your browser and try again. If you experience further difficulties please go to our <a href ="help.html">help</a> pages.<br>
<%
Stmt.close(); Conn.close();
%>
</body>
</html>Hi,
You could execute queries as many times as you want using the same connection.
For ex;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection Conn = DriverManager.getConnection("jdbc:odbc:Novbase","","");
Statement Stmt = Conn.createStatement();
String Query = ("INSERT INTO Author (Title, Surname, Forename, Address, Address2, Town, Postcode, EMail, DOB, TelNo) VALUES ('" + title + "', '" + surname + "', '" + forename + "', '" + address + "', '" + address2 + "', '" + town + "', '" + postcode + "', '" + email + "', '" + dob + "', '" + telephone +"')");
//Stmt.executeUpdate(Query) is where the records are inserted.
int SQLStatus = Stmt.executeUpdate(Query);
if(SQLStatus != 0)
Query = "SELECT Forname FROM Author;";
ResultSet name = stmt.executeQuery(Query);
//Now name will have all the Forname fields in the database. You could retrieve that using the get methods.
%>
Thank you. Your Author ID number is ??
<%
else
%>
Error - Your details have not been submitted</font></h4>
<br>
Please click back on your browser and try again. If you experience further difficulties please go to our <a href ="help.html">help</a> pages.<br>
<%
Stmt.close(); Conn.close();
%>
I hope that can help you.
Rajesh -
Creating a Job for publication in Sql Server with select statement for passing a parameter
I am creating a job for adding article to a publication. the second step (e.g. for adding article) gives me error. I pass the name of the table as follow:
EXEC sp_addarticle
@publication = 'TTB', --THE NAME OF MY PUBLICATION
'select top (1)' @article = 'Name from TableAdded Order by create_date Desc',
'select top (1)' @source_object = 'Name from TableAdded Order by create_date Desc',
@force_invalidate_snapshot = 1;
TableAdded is a table I have in my publisher which contains the name of the newly added table.
I believe it fails because the way I pass the name of the article to the store procedure is not correct. Can anyone please help me on that?
Kind regards
AminYes, the way you add article is wrong.
you may try the below and let us know if you have any issues:
while loop begin (Get the values from TableAdded with status IsAdded=0)
Assign the values to variables like @article = Select top 1 Name from TableAdded where IsAdded=0
Call the sp_addarticle assigning the variable
update the Isadded flag in tableAdded for the name to 1
end while
BTB, Article configuration is supposed to be a one time configuration. Having said, you can easily configure through API. Caution, not sure what are you trying here, for adding new article, you may need to synch the data first in the above approach and should
carried out in the downtime. -
Number of rows inserted is different in bulk insert using select statement
I am facing a problem in bulk insert using SELECT statement.
My sql statement is like below.
strQuery :='INSERT INTO TAB3
(SELECT t1.c1,t2.c2
FROM TAB1 t1, TAB2 t2
WHERE t1.c1 = t2.c1
AND t1.c3 between 10 and 15 AND)' ....... some other conditions.
EXECUTE IMMEDIATE strQuery ;
These SQL statements are inside a procedure. And this procedure is called from C#.
The number of rows returned by the "SELECT" query is 70.
On the very first time call of this procedure, the number rows inserted using strQuery is *70*.
But in the next time call (in the same transaction) of the procedure, the number rows inserted is only *50*.
And further if we are repeating calling this procedure, it will insert sometimes 70 or 50 etc. It is showing some inconsistency.
On my initial analysis it is found that, the default optimizer is "ALL_ROWS". When i changed the optimizer mode to "rule", this issue is not coming.
Anybody faced these kind of issues?
Can anyone tell what would be the reason of this issue..? any other work around for this...?
I am using Oracle 10g R2 version.
Edited by: user13339527 on Jun 29, 2010 3:55 AM
Edited by: user13339527 on Jun 29, 2010 3:56 AMYou have very likely concurrent transactions on the database:
>
By default, Oracle Database permits concurrently running transactions to modify, add, or delete rows in the same table, and in the same data block. Changes made by one transaction are not seen by another concurrent transaction until the transaction that made the changes commits.
>
If you want to make sure that the same query always retrieves the same rows in a given transaction you need to use transaction isolation level serializable instead of read committed which is the default in Oracle.
Please read http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10471/adfns_sqlproc.htm#ADFNS00204.
You can try to run your test with:
set transaction isolation level serializable;If the problem is not solved, you need to search possible Oracle bugs on My Oracle Support with keywords
like:
wrong results 10.2Edited by: P. Forstmann on 29 juin 2010 13:46 -
Count(*) function in select statement having group by condition
Hi
I would like to use count(*) in select statement having group by clause. say for example there is a state with a number of cities listed. I would like to get the count of cities for each state.
the sql stement is grouped by state and it is joined with 5 more tables.
Thanks
ps: ignore the previous postAssuming there is one record per city per state, then
SELECT state,count(*)
FROM state_tbl
GROUP BY stateWill get one record per state with the number of cities in each state. If you want to join that result set to other tables you need to either create a view with that statement or use an in-line view viz.
SELECT c.cust_name,c.state,s.num_cities
FROM customers c,
(SELECT state,count(*) num_cities
FROM state_tbl
GROUP BY state) s
WHERE c.state = s.stateTTFN
John -
Count(*) in select statement having group by clause
Hi
I would like to use count(*) in select statement having group by clause. say for example there is a state with a number of cities listed. I would like to get the count of cities for each state.
the sql stement is grouped by city and it is joined with 5 more tables.
ThanksI suspect you want to look into analytic functions (assuming you have a recent version of Oracle). asktom.oracle.com has numerous examples if you do a search.
Justin -
Discoverer Custom SQL Report with Group By,Having
Hi all,
I'm working with Discoverer 4.1.41.05 and retrieve data from Oracle HRMS.
I want to create a report based on a custom SQL Folder in Discoverer Administration to solve the following scenario:
SELECT EMPLOYEE_NUMBER FROM PER_ALL_PEOPLE_F
MINUS
SELECT EMPLOYEE_NUMBER FROM PER_ALL_PEOPLE_F,PER_EVENTS, PER_BOOKINGS
GROUP BY EMPLOYEE_NUMBER
HAVING SUM(PER_EVENTS.ATTRIBUTE1) >= 10)
The problem is when I run the sql from SQL*Plus is working fine producing the correct result.
When I create the Custom Folder and create a Discoverer report based on it I retrieve all employees since the second select statement retrieve no rows!
Does anyone has any idea how I can solve this problem or if there is another way to create this scenario?
Any feedback is much appreciated.
Thanking you in advance.
ElenaHi Elena,
I think you are missing some joins, you could try something like:
SELECT EMPLOYEE_NUMBER FROM PER_ALL_PEOPLE_F p
WHERE NOT EXISTS
(SELECT NULL
FROM PER_EVENTS e, PER_BOOKINGS b
WHERE b.person_id = p.person_id
AND e.event_id = b.event_id
GROUP BY b.person_id
HAVING SUM(e.ATTRIBUTE1) >= 10)
Rod West -
SQL Insert from "Select Query" Not Work when use Order By
Hai every body...
I have a problem with T-SQL. I use SQL Server 2012 Express with SP2 on Windows 7 32-bit SP1.
This is the problem
-- first, create table
create table dtoth.tableA (
id_data TINYINT PRIMARY KEY,
kd_data VARCHAR(20),
nm_data VARCHAR(200)
-- then, insert values
INSERT INTO dtoth.tableA VALUES (0,'100.001','KAS');
INSERT INTO dtoth.tableA VALUES (1,'110.001','BANK');
INSERT INTO dtoth.tableA VALUES (2,'120.001','PIUTANG DAGANG');
INSERT INTO dtoth.tableA VALUES (3,'121.001','PIUTANG GIRO');
INSERT INTO dtoth.tableA VALUES (4,'130.001','PERSEDIAAN BARANG DAGANGAN');
-- then, i create a temporary table
create table dtoth.temp_tableA (
kd_data VARCHAR(20),
nm_data VARCHAR(200)
-- then, i create a store procedure to call data from temporary table
CREATE procedure dtoth.report
AS
BEGIN
DELETE FROM dtoth.temp_tableA;
INSERT INTO dtoth.temp_tableA SELECT kd_data, nm_data FROM dtoth.tableA ORDER BY kd_data desc;
SELECT * FROM dtoth.temp_tableA;
END
GO
When i execute the the store procedure with
EXEC dtoth.report;
the result is not accurate like this (kd_data not sorted desc):
I want the column "kd_data" sort descending because i use ORDER BY kd_data DESC in insert statement on the store procedure.
By the way, if i execute code like :
SELECT kd_data, nm_data FROM dtoth.tableA ORDER BY kd_data desc;
the result is correct like this
So, what solution for my code in the store procedure ?
Thanks.to get the data sorted, you should order by in your select in the stored procedure
sorting while inserting does not guarenatee order..
remove the order by in insert statement and put that in the select statement
so, your procedure should be
-- then, i create a store procedure to call data from temporary table
CREATE procedure dtoth.report
AS
BEGIN
DELETE FROM dtoth.temp_tableA;
INSERT INTO dtoth.temp_tableA SELECT kd_data, nm_data FROM dtoth.tableA;
SELECT * FROM dtoth.temp_tableA ORDER BY kd_data desc;
END
GO
Hope it Helps!! -
Problem with Select Statements
Hi All,
I have a performance problem for my report because of the following statements.
How can i modify the select statements for improving the performance of the report.
DATA : shkzg1h LIKE bsad-shkzg,
shkzg1s LIKE bsad-shkzg,
shkzg2h LIKE bsad-shkzg,
shkzg2s LIKE bsad-shkzg,
shkzg1hu LIKE bsad-shkzg,
shkzg1su LIKE bsad-shkzg,
shkzg2hu LIKE bsad-shkzg,
shkzg2su LIKE bsad-shkzg,
kopbal1s LIKE bsad-dmbtr,
kopbal2s LIKE bsad-dmbtr,
kopbal1h LIKE bsad-dmbtr,
kopbal2h LIKE bsad-dmbtr,
kopbal1su LIKE bsad-dmbtr,
kopbal2su LIKE bsad-dmbtr,
kopbal1hu LIKE bsad-dmbtr,
kopbal2hu LIKE bsad-dmbtr.
*These statements are in LOOP.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1s , kopbal1s)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1su , kopbal1su)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1h , kopbal1h)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1hu , kopbal1hu)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2s , kopbal2s)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2su , kopbal2su)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2h , kopbal2h)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2hu , kopbal2hu)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.>
Siegfried Boes wrote:
> Please stop writing answers if you understrand nothing about database SELECTS!
> All above recommendations are pure nonsense!
>
> As always with such questions, you must do an analysis before you ask! The coding itself is perfectly o.k., a SELECT with an aggregate and a GROUP BY can not be changed into a SELECT SINGLE or whatever.
>
> But your SELECTS mustr be supported by indexes!
>
> Please run SQL Trace, and tell us the results:
>
> I see 8 statements, what is the duration and the number of records coming back for each statement?
> Maybe only one statement is slow.
>
> See
> SQL trace:
> /people/siegfried.boes/blog/2007/09/05/the-sql-trace-st05-150-quick-and-easy
>
>
> Siegfried
Nice point there Siegfried. Instead of giving constructive suggestion, people here give a very bad suggestion on using SELECT SINGLE combined with SUM and GROUP BY.
I hope the person already look at your reply before he try using select single and wondering why he has error.
Anyway, the most important thing is how many loop expected for those select statements?
If you have like thousands of loop, you can expect a poor performance.
So, you should also look at how many times the select statement is called and not only performance for each select statement when you're doing SQL trace.
Regards,
Abraham -
Sql Error in Select statement when doing subquery
Hi,
I am trying to see what the error is in the subquery part of the select statement.
Subquery should be fetching the safety_stock_quantity based on the MAX(effectivity_date).
Any suggestions?
SELECT kbn.last_update_date,itm.segment1,itm.description,kbn.kanban_card_number,kbn.kanban_size,
(SELECT msc.safety_stock_quantity
FROM mtl_safety_stocks msc
WHERE msc.effectivity_date = (select MAX(msc2.effectivity_date)
from mtl_safety_stocks msc2
where msc2.inventory_item_id = itm.inventory_item_id
and msc2.organization_id = itm.organization_id)
AND msc.inventory_item_id = itm.inventory_item_id
AND msc.organization_id = itm.organization_id
FROM mtl_system_items_b itm
,mtl_onhand_quantities_detail moqd
,mtl_safety_stocks msc
,mtl_kanban_card_activity kbn
WHERE itm.inventory_item_id = kbn.inventory_item_id
AND itm.organization_id = kbn.organization_id
AND itm.inventory_item_id = moqd.inventory_item_id
AND itm.organization_id = moqd.organization_id
AND moqd.subinventory_code = kbn.source_subinventory
AND kbn.card_status = 1
AND kbn.supply_status = 5
AND msc.inventory_item_id = itm.inventory_item_id
AND msc.organization_id = itm.organization_id
GROUP BY
kbn.last_update_date,itm.segment1,itm.description,kbn.kanban_card_number,kbn.kanban_size;
Thanks
PravnHi, Pravn,
Remember the ABC's of GROUP BY:
When you use a GROUP BY clause and/or an aggregate fucntion, then every item in the SELECT clause must be:
(A) an <b>A</b>ggregate function,
(B) one of the "group <b>B</b>y" expressions,
(C) a <b>C</b>onstant, or
(D) something that <b>D</b>epends entirely on the above. (For example, if you "GROUP BY TRUNC(dt)", you can "SELECT TO_CHAR (TRUNC(dt), 'Mon-DD')").
There's a GROUP BY clause in your main query, so every item in the main SELECT clause must be one of the above. The last item, the unnamed scalar sub-query, is none of the above.
How can you fix this problem? That depends on your data, the results you want, and perhaps on your Oracle version. If you'd like help, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables involved. Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
Always say which version of Oracle you're using.
You may have noticed that this site normally doesn't display multiple spaces in a row.
Whenever you post formatted text (including, but limited to, actual code) on this site, type these 6 characters:
\(small letters only, inside curly brackets) before and after each section of formatted text, to preserve spacing. -
Problem with SELECT statement. What is wrong with it?
Why is this query....
<cfquery datasource="manna_premier" name="kit_report">
SELECT Orders.ID,
SaleDate,
Orders.UserID,
Distributor,
DealerID,
Variable,
TerritoryManager,
US_Dealers.ID,
DealerName,
DealerAddress,
DealerCity,
DealerState,
DealerZIPCode,
(SELECT SUM(Quantity)
FROM ProductOrders PO
WHERE PO.OrderID = Orders.ID) as totalProducts,
FROM Orders, US_Dealers
WHERE US_Dealers.ID = DealerID AND SaleDate BETWEEN #CreateODBCDate(FORM.Start)# AND #CreateODBCDate(FORM.End)# AND Variable = '#Variable#'
</cfquery>
giving me this error message...
Error Executing Database Query.
[Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC Microsoft Access Driver] The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect.
The error occurred in D:\Inetpub\mannapremier\kit_report2.cfm: line 20
18 : WHERE PO.OrderID = Orders.ID) as totalProducts,
19 : FROM Orders, US_Dealers
20 : WHERE US_Dealers.ID = DealerID AND SaleDate BETWEEN #CreateODBCDate(FORM.Start)# AND #CreateODBCDate(FORM.End)# AND Variable = '#Variable#'
21 : </cfquery>
22 :
SQLSTATE
42000
SQL
SELECT Orders.ID, SaleDate, Orders.UserID, Distributor, DealerID, Variable, TerritoryManager, US_Dealers.ID, DealerName, DealerAddress, DealerCity, DealerState, DealerZIPCode, (SELECT SUM(Quantity) FROM ProductOrders PO WHERE PO.OrderID = Orders.ID) as totalProducts, FROM Orders, US_Dealers WHERE US_Dealers.ID = DealerID AND SaleDate BETWEEN {d '2009-10-01'} AND {d '2009-10-31'} AND Variable = 'Chick Days pre-book'
VENDORERRORCODE
-3504
DATASOURCE
manna_premier
Resources:
I copied it from a different template where it works without error...
<cfquery name="qZVPData" datasource="manna_premier">
SELECT UserID,
TMName,
UserZone,
(SELECT COUNT(*)
FROM Sales_Calls
WHERE Sales_Calls.UserID = u.UserID) as totalCalls,
(SELECT COUNT(*)
FROM Orders
WHERE Orders.UserID = u.UserID) as totalOrders,
(SELECT SUM(Quantity)
FROM ProductOrders PO
WHERE PO.UserID = u.UserID AND PO.NewExisting = 1) as newItems,
(SELECT SUM(NewExisting)
FROM ProductOrders PO_
WHERE PO_.UserID = u.UserID) as totalNew,
SUM(totalOrders)/(totalCalls) AS closePerc
FROM Users u
WHERE UserZone = 'Central'
GROUP BY UserZone, UserID, TMName
</cfquery>
What is the problem?It's hard to say: what's your request timeout set to?
700-odd records is not much of a fetch for a decent DB, and I would not expect that to case the problem. But then you're using Access which doesn't fit the description of "decent DB" (or "fit for purpose" or "intended for purpose"), so I guess all bets are off one that one. If this query is slow when ONE request is asking for it, what is going to happen when it goes live and multiple requests are asking for it, along with all the other queries your site will want to run? Access is not designed for this. It will really struggle, and cause your site to run like a dog. One that died serveral weeks ago.
What else is on the template? I presume you're doing something with the query once you fetch it, so could it be that code that's running slowly? Have you taken any steps to isolate which part of the code is taking so long?
How does the query perform if you take the subquery out of the select line? Is there any other way of getting that data? What subquery will be running once for every row of the result set... not very nice.
Adam -
Problem with select statement using Ranges
Hi Guys,
I have used Ranges and used a select statement for selecting those ranges but I am facing a problem.
RANGES: r_doctyp for EDIDC-DOCTYP.
r_doctyp-sign = 'I'.
r_doctyp-option = 'EQ'.
r_doctyp-low = 'DEBMAS'.
append r_doctyp.
r_doctyp-sign = 'I'.
r_doctyp-option = 'EQ'.
r_doctyp-low = 'MATMAS'.
append r_doctyp.
r_doctyp-sign = 'I'.
r_doctyp-option = 'EQ'.
r_doctyp-low = 'PRICAT'.
append r_doctyp.
r_doctyp-sign = 'I'.
r_doctyp-option = 'EQ'.
r_doctyp-low = 'ORDERS'.
append r_doctyp.
r_doctyp-sign = 'I'.
r_doctyp-option = 'EQ'.
r_doctyp-low = 'INVOIC'.
append r_doctyp.
Select DOCNUM " IDoc number
DOCTYP " IDoc Type
from EDIDC into table IT_ZEDIDC
where CREDAT EQ s_credat-low
and DOCTYP EQ r_doctyp " IDOC Types
and DIRECT EQ '1'.
Here my select statement is only taking INVOIC.
But my statement should take any document type.
Thanks,
Prasad.Hi...,
Your following select statement is correct.
Select DOCNUM " IDoc number
DOCTYP " IDoc Type
from EDIDC into table IT_ZEDIDC
where CREDAT IN s_credat
and DOCTYP IN r_doctyp " IDOC Types
and DIRECT EQ '1'.
Why you are not getting result..
1. structure of the IT_ZEDIDC is having two fields DOCNUM , DOCTYP with same data lengths. If not it should be...
2. Order in the database table is must be similer to the order you maintained in the select statement.
3. As you are hard coding the input ranges make sure about every letter.
4. take a look at other where condition fields too.
5. check the table of the ranges in debugging mode.
6. why can't you declare separate work area and table for ranges...?
like .... data: r_tab type range of <field>
data: wa_tab like line of r_tab.
7. Use clear work area statement after the append statment.
--Naveen Inuganti. -
Excessive flashback log generates with select statement
Hi everyone;
We have some extractions taken from a "flashback on" database.
Extractions are just select statements but when they are run, database produces excessive flashback logs.
What may be the reason database produce flashback logs with just select statements?
(It's certain that there are no insert-update-delete operations)
Version: 10.2.0.4.3
Thanks...Do you do heavy update/delete before you select the statements ?
I am not very sure if delayed block cleanout also have the same effect on flashback logs but the output below is leading me to think that way
HR@ORACOS> select * from v$flashback_database_stat;
BEGIN_TIME END_TIME FLASHBACK_DATA DB_DATA REDO_DATA ESTIMATED_FLASHBACK_SIZE
20100527 15:32:53 20100527 15:50:16 875266048 1207132160 2038729728 0
20100527 14:32:50 20100527 15:32:53 248160256 127295488 450139648 1.3215E+10
20100527 13:32:48 20100527 14:32:50 10452992 15646720 4400640 1.5549E+10
20100527 12:32:43 20100527 13:32:48 745693184 948461568 1311620608 2.2789E+10
20100527 11:25:56 20100527 12:32:43 1262026752 1984741376 2358546432 2.7212E+10
HR@ORACOS> set autotrace traceonly statistics
HR@ORACOS> update base_table_np set y='INVALID';
commit;
4021808 rows updated.
Statistics
2512 recursive calls
8341430 db block gets
4069140 consistent gets
120569 physical reads
1908471980 redo size
848 bytes sent via SQL*Net to client
793 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
4021808 rows processed
HR@ORACOS> set autotrace off;
HR@ORACOS> select * from v$flashback_database_stat;
HR@ORACOS>
BEGIN_TIME END_TIME FLASHBACK_DATA DB_DATA REDO_DATA ESTIMATED_FLASHBACK_SIZE
20100527 15:32:53 20100527 16:00:36 1236664320 2021974016 4019910656 0
20100527 14:32:50 20100527 15:32:53 248160256 127295488 450139648 1.3215E+10
20100527 13:32:48 20100527 14:32:50 10452992 15646720 4400640 1.5549E+10
20100527 12:32:43 20100527 13:32:48 745693184 948461568 1311620608 2.2789E+10
20100527 11:25:56 20100527 12:32:43 1262026752 1984741376 2358546432 2.7212E+10
HR@ORACOS> set autotrace traceonly statistics
HR@ORACOS> select * from base_table_np;
4021808 rows selected.
Statistics
139 recursive calls
0 db block gets
53908 consistent gets
4404 physical reads
1652384 redo size ------->delayed block cleanout effect
175008833 bytes sent via SQL*Net to client
88996 bytes received via SQL*Net from client
8045 SQL*Net roundtrips to/from client
4 sorts (memory)
0 sorts (disk)
4021808 rows processed
HR@ORACOS> set autotrace off
HR@ORACOS> select * from v$flashback_database_stat; ----flashback data size increases
HR@ORACOS>
BEGIN_TIME END_TIME FLASHBACK_DATA DB_DATA REDO_DATA ESTIMATED_FLASHBACK_SIZE
20100527 15:32:53 20100527 16:01:11 1305264128 2054594560 4021728256 0
20100527 14:32:50 20100527 15:32:53 248160256 127295488 450139648 1.3215E+10
20100527 13:32:48 20100527 14:32:50 10452992 15646720 4400640 1.5549E+10
20100527 12:32:43 20100527 13:32:48 745693184 948461568 1311620608 2.2789E+10
20100527 11:25:56 20100527 12:32:43 1262026752 1984741376 2358546432 2.7212E+10Basically what I do is I update a 4 million table big redo generated with flashback logs
When I do select after the update I still see the redo generated because of delayed block cleanout but what I also see is the slight increase in flashback data size (check the first row of flashback_database_stat) which suits what you asking for. Select statement generates flashback log
Tested on 11.2.0.1 with single active session on the db
Coskan Gundogar
Blog: http://coskan.wordpress.com
Twitter: http://www.twitter.com/coskan
Linkedin: http://uk.linkedin.com/in/coskan
--------- -
PL/SQL Function in Select statement
Hi
I am calling a function in select statement. The query works in Toad and PL/SQL developer except SQL plus and disconnecting Oracle connection.
The error is “ERROR at line 1: ORA-03113: end-of-file on communication channel”.
When I called the same query from BC4J View Object the error message is “java.sql.SQLException: No more data to read from socket”.
Can any one advise me please?
Thanks
SriniSrini
I've seen similar cases in the past with 9.2.0.x (x <= 5). What Oracle version are you using? It's worth checking the bug database (I can't just now - I don't have a valid support id in my current contract). And as Warren says, post your SQL query.
HTH
Regards nigel -
How to return the result set of multiple select statements as one result set?
Hi All,
I have multiple select statements in my stored procedure that I want to return as one result set
for instance
select id from tableA
union
select name from table b
but union will not work because the result sets datatypes are not identical so how to go about this ?
ThanksYou have to CAST or CONVERT (or implicitly convert) the columns to the same datatype. You must find a datatype that both columns can be converted to without error. In your example I'm guessing id is an int and name is a varchar or nvarchar.
Since you didn't convert the datatypes, SQL will use its data precedence rules and attempt to convert name to an int. If any row contains a row that has a value in name that cannot be converted to an int, you will get an error. The solution is
to force SQL to convert the int to varchar. So you want something like
select cast(id as varchar(12)) from tableA
union
select name from tableb
If the datatypes are something other that int or varchar, you must find a compatable datatype and then convert one (or both) of the columns to that datatype.
Tom
Maybe you are looking for
-
Hi guys ! just a ''little'' rant/frustration venting here : a few days ago i decided i'd finally give iCloud a go & check out how well it works... launched the app, followed the instructions & thought i might as well edit my iTunes Store Apple ID
-
How can I get my money back if iTunes has charged me twice for songs that never downloaded?
-
help
-
Hi, does anyone know of any applications that are available for oracle forms that filter out the source code and convert it into a text document, pdf, or automatically print just the code?
-
Cfdocument does not support justified text
I'm finding it impossible to justify text in a PDF created using cfdocument. I've tried both <p align="justify"> and CSS but neither work. If I change it to right or center it works which suggests justify is unsupported. Is this a bug or an oversight