Incorrect count query
I'm using CursoredStream to fetch a large result set. When doing a cursor.size() Toplink generates the wrong count query. I do have group value functions in the query as shown below.
For e.g. this is the cursor query:
--- Actual select query ---
SELECT t0.AWARD_ID, t0.AWARD_NUMBER, t0.FINAL_AMOUNT, t2.NAME,
SUM(t1.REPORT_AMOUNT), t4.FISCAL_YEAR, t5.DESCRIPTION
FROM REF_ENTITY t6, T_CODE t5, SOL t4, APP t3, APP_VERSION t2, SUBG t1,
AWARD t0
WHERE ((((t4.FISCAL_YEAR = '1993') AND (t6.REF_ENTITY_ID = 5)) AND (t4.SUBG_
REPORT = 'Y')) AND ((t5.T_CODE_ID = t0.T_CODE_ID_STATUS) AND ((t2.APP_ID = t3.A
PP_ID) AND ((t6.REF_ENTITY_ID = t4.REF_ENTITY_ID) AND ((t4.SOL_ID = t3.SOL_ID)
AND ((t3.APP_ID = t0.APP_ID) AND (t0.AWARD_ID = t1.AWARD_ID)))))))
GROUP BY t0.FINAL_AMOUNT, t0.AWARD_ID, t0.AWARD_NUMBER, t4.FISCAL_YEAR, t5.D
ESCRIPTION, t2.NAME ORDER BY t0.AWARD_NUMBER ASC
The count query generated by Toplink is:
SELECT COUNT(*)
FROM REF_ENTITY t4, AWARD t3, APP t2, SOL t1, SUBG t0
WHERE ((((t1.FISCAL_YEAR = ?) AND (t4.REF_ENTITY_ID = ?))
AND (t1.SUBG_REPORT = 'Y')) AND ((t4.REF_ENTITY_ID = t1.REF_ENTITY_ID)
AND ((t1.SOL_ID = t2.SOL_ID) AND ((t2.APP_ID = t3.GMS_APP_ID)
AND (t3.AWARD_ID = t0.AWARD_ID)))))
I have to provide my own ValueReadQuery to get the correct count in the above case. Are there any rules for determining when we have to explicitly provide our own count query?
cheers
p.s: Toplink version 9.0.3.4
Abe,
This sounds like a bug. I assume you are using a ReportQuery to generate the initial SQL. You should only have to provide a ValueReadQuery to a cursored query when using SQL, stored-procedure, or to optimize what is being generated.
This looks like the group-by on your ReportQuery is being ignored on the size query. The best plan is to submit this to support with the TopLink code used and the information provided in this post.
In the mean time the work-around of providing your own query is probably the best solution.
Doug
Similar Messages
-
How can we improve the perfromace of a count query?
Suppose I have a table A that holds more than 50 million rows.
Now if i want to count the no of rows which is the best one
1) Select count(*) from A.
Definitely not as it is doing a full table scan
2) Select count(primary_key) from A.
3) Select count(row_id) from A.
One more question whats the difference between select count(*) from table_name and select count(1) from table_a. Many people suggest count(1) and i dont see any reason though.
Kindly guide me.> Please see my points 1,2 and 3.
Can this change the execution plan (path) of the CBO in anyway?
1. count rows
2. count rows using primary key
3. counting rows using physical row addresses
The fact is that the rows, and all the rows, need to be counted. The CBO will choose the most "attractive" I/O path - i.e. the smallest one, the one with the least amount of I/O. It does not need tricks like COUNT(1) or COUNT(PK) or COUNT(ROWID) in order to make an appropriate decision.
Example:
SQL> create table tab1 nologging as select level as ID, LPAD('*',4000,'*') as STUFF from dual connect by level <= 10000;
Table created.
SQL> set autotrace on
Running a SELECT COUNT(*) without any alternate I/O paths (no indexes exist)
SQL> select count(*) from tab1;
COUNT(*)
10000
Execution Plan
Plan hash value: 899213575
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 2306 (4)| 00:00:28 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | TABLE ACCESS FULL| TAB1 | 9982 | 2306 (4)| 00:00:28 |
Note
- dynamic sampling used for this statement
Statistics
28 recursive calls
0 db block gets
10087 consistent gets
10000 physical reads
0 redo size
208 bytes sent via SQL*Net to client
238 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
Creating an PK index
SQL> alter table tab1 add constraint pk_tab1 primary key(id) using index;
Table altered.
Running the same SELECT COUNT(*) - but the CBO now sees that the PK index
is smaller and cheaper to scan than scanning the table
SQL> select count(*) from tab1;
COUNT(*)
10000
Execution Plan
Plan hash value: 1796789124
| Id | Operation | Name | Rows | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 9 (23)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | | |
| 2 | INDEX FAST FULL SCAN| PK_TAB1 | 9982 | 9 (23)| 00:00:01 |
Note
- dynamic sampling used for this statement
Statistics
194 recursive calls
0 db block gets
131 consistent gets
20 physical reads
0 redo size
222 bytes sent via SQL*Net to client
238 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
5 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> -
I have a query -->select c1,c2,c3 from table1 . This query takes only few milliseconds. But when I take count from the same query i.e. when I execute select count(c1,c2,c3) from table1 then it takes a very long time (about 1 min). The table1 contains about 25000 rows. Please help to improve performance of Count query.
Satej wrote:
I have a query -->select c1,c2,c3 from table1 . This query takes only few milliseconds. But when I take count from the same query i.e. when I execute select count(c1,c2,c3) from table1 then it takes a very long time (about 1 min).Classic misperception of Toad, SQL Navigator and similar tool users. All these tools fetch just first result screen and show time it took to fetch just that and not the time to fetch all rows. And in order to count you need to fetch all rows.l That is why select count(*) takes longer. But 1 min for 25000 rows is a bit long. Check execution plan to see what is going on.
SY. -
Deployment - RemoteException: Incorrect QL query ...
Hi everybody,
I am currently trying to migrate from WLS 8.1 to SAP WEB AS 6.40 SP11. I have built my application EAR and already
resolved some typical migration issues in my deployment descriptors. The EAR contains multiple component jars with
their according deployment descriptors My current problem is a tough one though. Neither the exception nor the stack
reveals where the exact problem lies. I checked my persistent.xml and ejb-jar.xml for well-formedness and
validity ... everything's fine.
here is the stack:
[code]<i>Starting Deployment of myApp
Aborted: development component 'myApp'/'mycompany.com'/'localhost'/'2005.10.28.10.14.19':
Caught exception during application deployment from SAP J2EE Engine's deploy service:
java.rmi.RemoteException: Cannot deploy application mycompany.com/myApp.. Reason: Incorrect QL query: , errors: line 1: expecting "select", found '<end-of-query>'line 1: expecting "from", found '<end-of-query>'.; nested exception is: com.sap.engine.services.deploy.container.DeploymentException: <--Localization failed: ResourceBundle='com.sap.engine.services.deploy.DeployResourceBundle', ID='com.sap.engine.services.ejb.exceptions.deployment.EJBDeploymentException: Incorrect QL query: , errors: line 1: expecting "select", found '<end-of-query>'line 1: expecting "from", found '<end-of-query>'.
at com.sap.engine.services.ejb.deploy.ejbql.QLTranslator.prepareQLContext(QLTranslator.java:191)
at com.sap.engine.services.ejb.deploy.ejbql.QLTranslator.translateQuery(QLTranslator.java:103)
at com.sap.engine.services.ejb.deploy.ejbql.QLTranslator.translateAllQueries(QLTranslator.java:171)
at com.sap.engine.services.ejb.deploy.DeployAdmin.translateQL(DeployAdmin.java:1398)
at com.sap.engine.services.ejb.deploy.DeployAdmin.generate(DeployAdmin.java:254)
at com.sap.engine.services.ejb.EJBAdmin.deploy(EJBAdmin.java:2118)
at com.sap.engine.services.deploy.server.application.DeploymentTransaction.makeComponents(DeploymentTransaction.java:594)
at com.sap.engine.services.deploy.server.application.DeployUtilTransaction.commonBegin(DeployUtilTransaction.java:379)
at com.sap.engine.services.deploy.server.application.DeploymentTransaction.begin(DeploymentTransaction.java:296)
at com.sap.engine.services.deploy.server.application.ApplicationTransaction.makeAllPhasesOnOneServer(ApplicationTransaction.java:290)
at com.sap.engine.services.deploy.server.application.ApplicationTransaction.makeAllPhases(ApplicationTransaction.java:323)
at com.sap.engine.services.deploy.server.DeployServiceImpl.makeGlobalTransaction(DeployServiceImpl.java:3033)
at com.sap.engine.services.deploy.server.DeployServiceImpl.deploy(DeployServiceImpl.java:463)
at com.sap.engine.services.deploy.server.DeployServiceImplp4_Skel.dispatch(DeployServiceImplp4_Skel.java:1555)
at com.sap.engine.services.rmi_p4.DispatchImpl._runInternal(DispatchImpl.java:294)
at com.sap.engine.services.rmi_p4.DispatchImpl._run(DispatchImpl.java:183)
at com.sap.engine.services.rmi_p4.server.P4SessionProcessor.request(P4SessionProcessor.java:119)
at com.sap.engine.core.service630.context.cluster.session.ApplicationSessionMessageListener.process(ApplicationSessionMessageListener.java:37)
at com.sap.engine.core.cluster.impl6.session.UnorderedChannel$MessageRunner.run(UnorderedChannel.java:71)
at com.sap.engine.core.thread.impl3.ActionObject.run(ActionObject.java:37)
at java.security.AccessController.doPrivileged(Native Method)
at com.sap.engine.core.thread.impl3.SingleThread.execute(SingleThread.java:94)
at com.sap.engine.core.thread.impl3.SingleThread.run(SingleThread.java:162)
', Arguments: []--> : Can't find resource for bundle java.util.PropertyResourceBundle, key com.sap.engine.services.ejb.exceptions.deployment.EJBDeploymentException: Incorrect QL query: , errors: line 1: expecting "select", found '<end-of-query>'line 1: expecting "from", found '<end-of-query>'.
at com.sap.engine.services.ejb.deploy.ejbql.QLTranslator.prepareQLContext(QLTranslator.java:191)
at com.sap.engine.services.ejb.deploy.ejbql.QLTranslator.translateQuery(QLTranslator.java:103)
at com.sap.engine.services.ejb.deploy.ejbql.QLTranslator.translateAllQueries(QLTranslator.java:171)
at com.sap.engine.services.ejb.deploy.DeployAdmin.translateQL(DeployAdmin.java:1398)
at com.sap.engine.services.ejb.deploy.DeployAdmin.generate(DeployAdmin.java:254)
at com.sap.engine.services.ejb.EJBAdmin.deploy(EJBAdmin.java:2118)
at com.sap.engine.services.deploy.server.application.DeploymentTransaction.makeComponents(DeploymentTransaction.java:594)
at com.sap.engine.services.deploy.server.application.DeployUtilTransaction.commonBegin(DeployUtilTransaction.java:379)
at com.sap.engine.services.deploy.server.application.DeploymentTransaction.begin(DeploymentTransaction.java:296)
at com.sap.engine.services.deploy.server.application.ApplicationTransaction.makeAllPhasesOnOneServer(ApplicationTransaction.java:290)
at com.sap.engine.services.deploy.server.application.ApplicationTransaction.makeAllPhases(ApplicationTransaction.java:323)
at com.sap.engine.services.deploy.server.DeployServiceImpl.makeGlobalTransaction(DeployServiceImpl.java:3033)
at com.sap.engine.services.deploy.server.DeployServiceImpl.deploy(DeployServiceImpl.java:463)
at com.sap.engine.services.deploy.server.DeployServiceImplp4_Skel.dispatch(DeployServiceImplp4_Skel.java:1555)
at com.sap.engine.services.rmi_p4.DispatchImpl._runInternal(DispatchImpl.java:294)
at com.sap.engine.services.rmi_p4.DispatchImpl._run(DispatchImpl.java:183)
at com.sap.engine.services.rmi_p4.server.P4SessionProcessor.request(P4SessionProcessor.java:119)
at com.sap.engine.core.service630.context.cluster.session.ApplicationSessionMessageListener.process(ApplicationSessionMessageListener.java:37)
at com.sap.engine.core.cluster.impl6.session.UnorderedChannel$MessageRunner.run(UnorderedChannel.java:71)
at com.sap.engine.core.thread.impl3.ActionObject.run(ActionObject.java:37)
at java.security.AccessController.doPrivileged(Native Method)
at com.sap.engine.core.thread.impl3.SingleThread.execute(SingleThread.java:94)
at com.sap.engine.core.thread.impl3.SingleThread.run(SingleThread.java:162)</i>[/code]
I tried the deployment via SDM, Visual Administrator, SWDS and deploytool, but without success.
Using the Visual Administrator I get a totally different exception. I thought that the deployment can be done by
any of the above mentioned tools with the same outcome. There is no storage.xml included in the jar, the
persistent.xml and ejb-jar.xml are parsable, thus they are wellformed and valid:
[code]<i>com.sap.engine.deploy.exceptions.BaseIOException: Cannot parse persistent.xml or storage.xml for JAR C:\usr\sap\J2E\JC00\j2ee\admin\.\temp\deploying\deploy_temp\reader1131614442531\comp.jar.
Reason: com.sap.engine.deploy.exceptions.BaseIOException: Cannot parse ejb-jar.xml for JAR C:\usr\sap\J2E\JC00\j2ee\admin\.\temp\deploying\deploy_temp\reader1131614442531\comp.jar.
Reason: java.lang.ArrayIndexOutOfBoundsException: 2..
at com.sap.engine.deploy.ejb.descriptors.jar.ReadEjbJar.getEjbJarDescriptor(ReadEjbJar.java:130)
at com.sap.engine.deploy.ejb.descriptors.jar.ReadEjbJar.getEjbJarDescriptor(ReadEjbJar.java:150)
at com.sap.engine.deploy.tool.deployer.ExtendedEarReader.loadDescriptorFromFile(ExtendedEarReader.java:230)
at com.sap.engine.deploy.tool.deployer.ExtendedEarReader.getFullEarDescriptor(ExtendedEarReader.java:102)
at com.sap.engine.services.deploy.gui.runtime.EarLoader.loadEar(EarLoader.java:137)
at com.sap.engine.services.deploy.gui.runtime.MainDeployPanelLogical.loadEarAction(MainDeployPanelLogical.java:169)
at com.sap.engine.services.deploy.gui.runtime.EarDialog.setEar(EarDialog.java:79)
at com.sap.engine.services.deploy.gui.DeployDialogLogical.okButtonAction(DeployDialogLogical.java:294)
at com.sap.engine.services.deploy.gui.DeployDialogLogical.actionPerformed(DeployDialogLogical.java:71)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:480)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:141)
at java.awt.Dialog$1.run(Dialog.java:540)
at java.awt.Dialog.show(Dialog.java:561)
at java.awt.Component.show(Component.java:1133)
at java.awt.Component.setVisible(Component.java:1088)
at com.sap.engine.services.deploy.gui.DeployDialogLogical.showDialog(DeployDialogLogical.java:378)
at com.sap.engine.services.deploy.gui.ApplicationsPanel.deployAction(ApplicationsPanel.java:409)
at com.sap.engine.services.deploy.gui.ApplicationsPanel.actionPerformed(ApplicationsPanel.java:351)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:480)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)</i>[/code]
Any help is highly appreciated.
thanks and best regards
MarcelHi Marcel,
Actually what this error means is that you have a method in your EJB QL that has incorrect syntax. If you look at the ejb-jar.xml file for your project you should see a section like this:
<query>
<query-method>
<method-name>findByNameAndID</method-name>
<method-params>
<method-param>java.lang.String</method-param>
<method-param>java.lang.Long</method-param>
</method-params>
</query-method>
<ejb-ql>select Object(inv) from InventoryBean inv where
inv.manufacturer = ?1 and inv.productID = ?2</ejb-ql>
</query>
This should correspond to the finder methods that you have defined for your entity bean. If it doesn't (i.e. variables don't match up) or if the syntax is incorrect you will get the error you mention. Note that it is also case sensitive.
Cheers,
Steve
If you find a post useful, please help keep the community going by setting a good example and rewarding the poster with points. -
Equipment count incorrect in query
Hello BI Gurus,
We are facing the following problem. I have a report requirement in the following format.
Sep-07 Oct-07 Nov-07 Dec-07
Total No of Machines 11 11 12 12
Machines relate to Equipment here. I am extracting this data from 0equipment_attr. This report is for a given Region, Plant and for a particular type of machine.
The problem here is as follows.
Let us suppose that equipment moves from plant X to another plant Y in September and later to plant Z.
Equipment should reflect the changes in Total number of machines.
Currently, Equipment has "From" and "To" date. Based on this, I am getting this value in calmonth,
let us say for example for
Equipment From date To Date Calmonth Plant
400000 17.07.2008 01.01.1000
400000 24.12.2008 18.07.2008 07.2008 1101
400000 31.12.9999 25.12.2008 12.2008 1103
Though, I am getting Calmonth correctly to indicate the status of the equipment present for that month..
I have 2 problems, first of all, at query level, it just shows 1 record with the latest month that is in this case , Dec 2008. but incase of equipment master i do see both the records..
In my reporting requirement, It needs to have history data over 12 months, so a given equipment will not indicate the status correctly..
As we see in the above example, equipment 400000 is moved from plant 1101 to 1103, when i look at query, it just shows me one record which is the latest..
I believe the problem here is with time dependency. calmonth is time dependent attribute in 0equipment, if i turn it time independent, all the values in calmonth will be same, i.e 12.2008 which should not be the case..
So in short there are 2 problems, how do i get the correct status of equipment at monthly level with the current scenario and second one will be for the months where there is no movement of equipment, it should show the correct count of equipment.,.
Please advice..
Thanks in advance,Hi Vipul,
Check this url.....
https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/009819ab-c96e-2910-bbb2-c85f7bdec04a
Thanks,
Vijay. -
Invalid SQL generated for count query.
A SQL statement was generated with the table name information missing when
a count SQL query was issued (e.g. SELECT COUNT(*) FROM WHERE <SOME
EXPRESSION>). The same query worked fine before switching to vertical
class mapping.
Basically no queries work with the result set to "count(this)". The
temporary work-around was to retrieve the collection and get the size. I
would rather not have to do this.
Any ideas?Brendan,
What version of Kodo is this happening with? Can you send a test case
demonstrating this to [email protected]?
-Patrick
Brendan Brothers wrote:
A SQL statement was generated with the table name information missing when
a count SQL query was issued (e.g. SELECT COUNT(*) FROM WHERE <SOME
EXPRESSION>). The same query worked fine before switching to vertical
class mapping.
Basically no queries work with the result set to "count(this)". The
temporary work-around was to retrieve the collection and get the size. I
would rather not have to do this.
Any ideas? -
Sub-Select Count query breaking TOAD
Oracle 10.2.0.4.0
Running TOAD 9.1
I am running some SQL on our eBusiness Suite:
SELECT pha.segment1
, pha.type_lookup_code
, (SELECT COUNT(DISTINCT pha2.po_header_id)
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
AND pla.contract_id = pha.po_header_id) po_count
, (SELECT MAX(pha2.creation_date)
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
AND pla.contract_id = pha.po_header_id) latest_cpa_po
FROM po.po_headers_all pha
, po.po_vendors pv
, po.po_vendor_sites_all pvsa
WHERE pha.vendor_id = pv.vendor_id
AND pha.vendor_site_id = pvsa.vendor_site_id
-- AND pv.VENDOR_NAME LIKE 'H%'
AND pha.vendor_id = 98
AND pha.type_lookup_code = 'CONTRACT'
AND pha.org_id IN(7041, 7042);The above query runs quicky (approx. 1 second). If I take out the AND pha.vendor_id = 98 then the query takes a few minutes to run.
When I try to export it, or scroll down to view > 500 rows, TOAD crashes.
I know this isn't a TOAD forum, but I think that this is probably an issue with my no doubt rubbish SQL.
If I take out this sub-select, then the problem doesn't happen:
, (SELECT COUNT(DISTINCT pha2.po_header_id)
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
AND pla.contract_id = pha.po_header_id) po_countHowever, I can't work out a better way of getting the data I need.
The sub-select counts POs which have been raised where the contractID on the PO line is the same as the PO Header ID from the main query.
Any advice please, on what I could do to sort this out would be much appreciated.
Thanks!Hi,
It looks like you can replace both scalar sub-queries with a join, like this:
WITH header_lines_summary AS
SELECT pla.contract_id
, COUNT (DISTINCT pha2.po_header_id) AS po_count
, MAX (pha2.creation_date) AS latest_cpa_po
FROM po.po_headers_all pha2
, po.po_lines_all pla
WHERE pha2.po_header_id = pla.po_header_id
GROUP BY pla.contract_id
) -- Everything up to this line is new
SELECT pha.segment1
, pha.type_lookup_code
, hls.po_count -- Changed
, hls.latest_cpa_po -- Changed
FROM po.po_headers_all pha
, po.po_vendors pv
, po.po_vendor_sites_all pvsa
, header_lines_summary hls -- New
WHERE pha.vendor_id = pv.vendor_id
AND pha.vendor_site_id = pvsa.vendor_site_id
AND pha.po_header_id = hls.contract_id (+) -- New
-- AND pv.VENDOR_NAME LIKE 'H%'
AND pha.vendor_id = 98
AND pha.type_lookup_code = 'CONTRACT'
AND pha.org_id IN (7041, 7042);Aside from the sub-query (which is entirely new), the query above is just what you posted, with 2 lines changed and 2 lines added, as marked.
This should be more efficient, but I don't know for certain that it will solve the Toad problem.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables, and also post the results you want from that data.
It never hurts to say what version of Oracle you're using. -
Help with a COUNT query...
I have created the following tables:
create table ZONE (
ZoneNo number(2),
State char(3),
PRIMARY KEY (ZoneNo)
create table SCHOOL (
SchoolType char(1),
SchoolNumber number(4),
SchoolName char(40),
SchoolZone number(2),
PRIMARY KEY (SchoolNumber,SchoolType),
FOREIGN KEY (SchoolZone) REFERENCES ZONE
create table TEACHER (
TeacherCode char(5),
Surname char(30),
FirstName char(30),
Gender char(1),
WorksFor number(4),
YearCommenced number(4),
LivesIn number(2),
TeachingType char(1),
PRIMARY KEY (TeacherCode),
FOREIGN KEY (WorksFor, TeachingType) REFERENCES SCHOOL(SchoolNumber,SchoolType),
FOREIGN KEY (LivesIn) REFERENCES ZONE
create table COMPUTER (
SerialNumber char(10),
Description char(50),
RegisteredTo char(5),
PRIMARY KEY (SerialNumber),
FOREIGN KEY (RegisteredTo) REFERENCES TEACHER
My question is, how would i make a COUNT (or similar) query that would display each SchoolName and show the total amount of teachers who work at that school in the second column?Yes, it needs to join all columns on foreign key.
This is usual, gentle, safety, and universal approach.
insert into ZONE values (6,'CA');
insert into SCHOOL values ('E',600,'Oracle Elemental School',6);
insert into SCHOOL values ('J',600,'Oracle Middle School',6);
insert into SCHOOL values ('S',600,'Oracle High School',6);
insert into TEACHER values (12001,'Suzuki','Akiko','F',600, 2005, 6, 'E');
insert into TEACHER values (12002,'Scott','Tiger','M',600, 2005, 6, 'E');
insert into TEACHER values (12003,'Zhong','San','M',600, 2005, 6, 'J');
insert into TEACHER values (12004,'Li','Si','M',600, 2005, 6, 'J');
insert into TEACHER values (12005,'Wang','Wu','M',600, 2005, 6, 'J');
insert into TEACHER values (12006,'Kim','Youngae','F',600, 2005, 6, 'S');
insert into TEACHER values (12007,'Kim','Jiu','F',600, 2005, 6, 'S');
insert into TEACHER values (12008, null,'Joe','M',600, 2005, 6, 'S');
column schoolname format a30
set lines 120
select s.SchoolName
,count(*) "ALL"
,count(t.TeacherCode) "ALL(CODE)"
,count(distinct t.TeacherCode) "DISTINCT(CODE)"
,count(t.surname) "ALL(NAME)"
,count(distinct t.surname) "DISTINCT(NAME)"
from School s, Teacher t
where s.SchoolNumber = t.WorksFor
and s.SchoolType = t.TeachingType
group by s.SchoolName
SCHOOLNAME ALL ALL(CODE) DISTINCT(CODE) ALL(NAME) DISTINCT(NAME)
Oracle Elemental School 2 2 2 2 2
Oracle High School 3 3 3 2 1
Oracle Middle School 3 3 3 3 3select s.SchoolName
,count(*) "ALL"
,count(t.TeacherCode) "ALL(CODE)"
,count(distinct t.TeacherCode) "DISTINCT(CODE)"
,count(t.surname) "ALL(NAME)"
,count(distinct t.surname) "DISTINCT(NAME)"
from School s, Teacher t
where s.SchoolNumber = t.WorksFor
--and s.SchoolType = t.TeachingType
group by s.SchoolName
SCHOOLNAME ALL ALL(CODE) DISTINCT(CODE) ALL(NAME) DISTINCT(NAME)
Oracle Elemental School 8 8 8 7 6
Oracle High School 8 8 8 7 6
Oracle Middle School 8 8 8 7 6 -
Using a "Sum" Calculated Field on a "Count" query column?
Here's my query using the Query Report Builder:
SELECT Col1, COUNT(Col1) AS Count_Column
FROM Table
GROUP BY Col1
It generates a report that lists all the values of column
"Col1" and how many times each value was used in Col1
For instance, if Col1 contained the value 'A' 12 times, 'B' 6
times, and 'C' 19 times, the report would generate this:
A - 12
B - 6
C - 19
What i need as a column footer is the total count of all the
values, which in this case 12+6+19=37
I am using a calculated field, setting the data type to
Double, the calcuation to Sum, and the perform calculation on to
'query.Count_Column'. Reset Field When is set to None.
When I run the report, it doubles the last number in the
report's Count column (19) and displays 38 on the page. I tested
this with another column and it doubled the last number in the
report as well.
How can I get it to properly Sum my Count_Column?Hi,
You need to check note 208366.1 which explains why totals can be blank. Without knowing the detail of you decode function it is hard to say what needs to be changed. Try putting a sum in front of the decode e.g.
sum(decode(period, 'Jan period', value, 0))
Hope that helps,
Rod West -
I'm trying to write a function to compare two different counts.
I have patient on multiple drugs...some drugs are allergy drugs and at least one allergy drug must be marked as primary.
select count(drug_role) as vAllergyCount, count(primary_flag) as PF, ap.patient_id
from ae_all_drugs aed, ae_patient ap
where drug_role = 'Allergy'
and aed.patient_id = ap.patient_id
and ap.patient_id like '2012%'
group by ap.patient_id,primary_flag
order by ap.patient_id
when grouped by patient id and primary flag I would receive a record like this:
VAlllergyCount | PF | patient_id
1 | 0 | 2012AB1
3 | 1 | 2012AB1
If i only group by patient_id I get this (which is what I want):
VAlllergyCount | PF| patient_id
4 | 1 | 2012AB1
But if I try to compare the two:
IF vAllergyCount > 1 and PF = 1 THEN 'SUCCESS' ELSIF 'FAIL'
I receive the error message "exact fetch returns more than requested number of rows" becuase the results are really my first query.
Can anyone help me re-write my query to compare the values?
P.S. I'm very new to this and this forum and I apologize for not inserting code tags. I don't know how.Hi,
979105 wrote:
Thank you for your hlep.
I need to write a function because the SQL query is going to be used as an edit check for another program. So yes, it will have to worth my time.
You are correct. I have a patient on multiple drugs. I want to know if there is at least one allergy drug (drug_role) > 1 if there is at least ONE primary allergy drug.
I did initially write my function with the patient_id as an input value. Why would I not need the vAllergyCount or the pf if that is an argument?If all you want to know is 'SUCCESS' or 'FAIL', then write a query that produces 'SUCCESS' or 'FAIL'. Inside the query, it may be handy to have something like vAllergyCount or pf, but that doesn't mean that they will be returned by the query, or that you need PL/SQL variables for them.
Here's my original function:That's a PROCEDURE, not a function. Sometimes, people use the word "procedure" lossely, to mean any kind of stored code, including functions, but never the other way around.
\Don't forget to put another \ tag at the end of the formatted section.
\ tags are supposed to occur in pairs. If you have an odd number of \ tags, the last one has no effect, and is displayed as regular text.
PROCEDURE COUNT_ALLERGY_MED (
ivPatient_ID IN VARCHAR2,
ovErrorText OUT VARCHAR2 )
IS
vCase_ID VARCHAR2 (25);
vSusCount NUMBER (3);
vSuspectMed VARCHAR2 (1);
--vPrimaryFlag VARCHAR2(1);
cursor c1 is
select count(*) as vSusCount
from ae_suspect_drugs aed
where drug_role = 'S'
and aed.case_id = ivCase_ID
--group by ivcase_id;
BEGIN
open c1;
fetch c1 into vSusCount;
close c1;
IF vSusCount < 1 /*AND vPrimaryFlag IS NULL*/ THEN
ovErrorText := 'Suspect Drug Count is less than 1' || 'SusCount: ' || vSusCount;
ELSIF vSusCount = 1 /*AND vPrimaryFlag IS NULL*/ THEN
ovErrorText := 'Suspect Drug count is equal to 1' || 'SusCount: ' || vSusCount;
ELSIF vSusCount > 1 /*AND vPrimaryFlag IS NULL*/ THEN
ovErrorText := 'Suspect Drug count is greater than 1 ' || 'SusCount: ' || vSusCount;
ELSE
ovErrorText := 'All criteria met';
END IF;
END COUNT_SUSPECT_MED;It seems like it would be more convenient to have a FUNCTION, rather than a PROCEDURE; that way you could call the function directly in a SQL statement (e.g., in the WHERE clause of a query or DML statement) as well as in PL/SQL.
The procedure you posted seems to be quite a bit differetn from the problem you asked eariler.
Here's one way you might write a fucntion for the earlier problem:CREATE OR REPLACE FUNCTION count_allergy_med
( ivPatient_ID IN VARCHAR2
RETURN VARCHAR2
IS
return_str VARCHAR2 (50); -- To be returned
BEGIN
SELECT CASE
WHEN COUNT (drug_role) > 1
AND COUNT (primary_flag) = 1
THEN 'SUCCESS'
ELSE 'FAIL'
END
INTO return_str
FROM ae_all_drugs aed
, ae_patient ap
WHERE drug_role = 'Allergy'
AND aed.patient_id = ap.patient_id
AND ap.patient_id = ivPatient_ID;
RETURN return_str;
END count_allergy_med;
Notice that there are no columns in the result set, nor variables in the function, that correspond to vAllergyCount or pf.
The function above returns 1 of 2 possible values. You couold just as well write a function that had 4 possible outcomes, or 4 basic results, each containing a variable number, such asSuspect Drug Count is less than 1. SusCount: 0
I'm not sure why you would want such as result, since 0 is the only possible count that is less than 1, but it's easy enough to produce results like that if you really want them.
Once again, if you'd like help, post a clear example of what you want. Include CREATE TABLE and INSERT statements for some sample data. Post some code where you might use the function that you're thinking of. Give a couple of different arguments for that functuion, and the results you would want for each argument given the same sample data. -
Hello All
I need help in writing a Optimized query .
Scenario :
We have several customers across multiple vendors .We want to get Customercount by vendor for a year
In this scenario we wanted to claculate
Customer Count for Vendor
Example
2014 Kroger(Vendor) has 100 customers
2014 Target(Vendor) has 50 customers
I wanted to see out of those 100 customers for Kroger who were not customer of kroger in 2012 and 2013 but customer of target
Similarly out of those 50 customers for Target who were not customer of target in 2012 and 2013 but customer of
kroger
In 2012 same customer might have bought with different vendor.We are calculating new to vendor
C1 2012 Kroger 100
C1 2012 Walmart 100
C2 2012 target 100
C2 2012 Kroger 100
C1 2014 target 100
C2 2014 Kroger 100
C2 2014 target 100
C2 2014 walmart 100
In this case C1 is customer count for target as he did not buy with target but with other vendors
and c2 canot be counted towarsd kroger or walmart becauase be bought with both of them but can be counterd towarrds walmart as he did not buy with walmart
I am trying to write a query to get 2012 and 2013 data for all vendors and then join with 2014 data set
but the thing is i am getting wrong count
Please let me knowthis?
SELECT Vendor,
YEAR(DATEADD(yy,DATEDIFF(yy,0,Datefield),0)) AS Yr,
COUNT(CustomerID) AS CustomerCount,
COUNT(CASE WHEN Cnt = 0 THEN CustomerID ELSE NULL END) AS FirstTimeCustomerCount
FROM Table t
OUTER APPLY (SELECT COUNT(*) AS Cnt
FROM Table
WHERE CustomerID = t.CustomerID
AND DATEDIFF(yy,0,Datefield) < DATEDIFF(yy,0,t.Datefield)
AND Vendor = t.Vendor)
GROUP BY Vendor,DATEDIFF(yy,0,Datefield)
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page -
I am connecting to the excel and running the following query:
Select Count(*) from [RIF$];
I is working fine and giving me total count of rows
Now when i try the following version of count
Select Count(Distinct Status) from [RIF$];
where Status is a column name.
It is giving the following error:
java.sql.SQLException: [Microsoft][ODBC Excel Driver] Syntax error (missing operator) in query expression 'Count(Distinct Status)'.
Pls help me
its as always urgentThis shouldn't execute in the quiery analyzer too, you don't have a java problem. I am not sure this is the optimal but you can try something like:
select count(c_id) from (select distinct pcid c_id from contragent) cntr HTH
Mike -
Bug SQL Dev 1.5.4 Build MAIN-5940 Exporting count(*) query results
when trying to export query results containing an analytic count the export dialog fails to open:
When running the following query
select count(*) over (partition by dummy order by rownum) xx
from dual;right clicking on the results grid and selecting export->xls (or any other format) fails to open the export dialog box.
changing the asterisk (*) to another value e.g. 1 or dummy corrects this issue for this particular query as does changing the analytic fuction to an aggregate function.
Edited by: Sentinel on Aug 12, 2009 11:05 AMI get the same error when exporting the same statement - however, there is a known problem with 1.5.4 and failing on exports of certain statements (see 1.5.3 - Export results to XLS ORA-00936 which talks about there still being problems in 1.5.4 although the thread is titled 1.5.3).
theFurryOne -
Simple Count query and fetching data
Hello,
I have a table Emp :
Name, age, sal, dateUpdated
A 21 100 6/4/10
B 21 101 6/4/10
C 32 101 2/2/2
D 20 100 3/3/3
I am trying to count the number of people in the same age group AND fetch that number.
For ex, I want to count the number of people that were updated today and then fetch their age.
for ex : something like this : select count(dateUpdated) from Emp where date updated is today
will only return 2 as result.
how should my query be if I want to even fetch the individual age along with the count ?
cheers,You can use {noformat}{noformat} tags to preserve your code format. That would help every one to read your post better.
-
Hairy count(*) query
I am trying to get a count of my rows into 'buckets' by age.
bucket 1: < 5 minutes
bucket 2: 5 to 8 minutes
bucket 3: 8 to 15 minutes
bucket 4: > 15 minutes
with c as ( select * from cases b where b.IS_ACTIVE = 'Y')
select count(*) as count from c where c.RESPONSE_SLA_START_DATE > SYSDATE - (5/24*60)
union all
select count(*) as count1 from c where c.RESPONSE_SLA_START_DATE < SYSDATE - (5/(24*60)) and c.RESPONSE_SLA_START_DATe > SYSDATE - (8/(24*60))
union all
select count(*) as count2 from c where c.RESPONSE_SLA_START_DATE < SYSDATE - (8/(24*60)) and c.RESPONSE_SLA_START_DATe > SYSDATE - (15/(24*60))
union all
select count(*) as count3 from c where c.RESPONSE_SLA_START_DATE < SYSDATE - (15/(24*60));
Everything works as expected, but my results are all being crammed into a single column "COUNT" rather than 4 separate columns "COUNT", "COUNT1", "COUNT2", "COUNT3".
COUNT
0
0
0
137
Any ideas or suggestions as to how I can get this divided into a single row with 4 columns rather than 4 rows with 1 column?Hi,
Welcome to the forum!
Here's one way:
WITH got_bucket AS
SELECT CASE
WHEN response_sla_start_date > SYSDATE - ( 5 / (24 * 60))
THEN 1
WHEN response_sla_start_date > SYSDATE - ( 8 / (24 * 60))
THEN 2
WHEN response_sla_start_date > SYSDATE - (15 / (24 * 60))
THEN 3
WHEN response_sla_start_date IS NOT NULL
THEN 4
END AS bucket
FROM cases
WHERE is_active = 'Y'
SELECT COUNT (CASE WHEN bucket = 1 THEN 1 END) AS under_5
, COUNT (CASE WHEN bucket = 2 THEN 1 END) AS from_5_to_8
, COUNT (CASE WHEN bucket = 3 THEN 1 END) AS from_8_to_15
, COUNT (CASE WHEN bucket = 4 THEN 1 END) AS from_15_up
FROM got_bucket
;If something is exactly (down to the second) 5, or 8, or 15 minutes old, it will go into the higher-numbered bucket.
Be careful! When using 2 or more arithmetic operators in the same expression, use parentheses to group them the way you want. "SYSDATE - (5/24*60)" means 12 and a half days ago.
Whenever you have a question, say which version of Oracle you're running. The query above will work in Oracle 9.1 and higher, but if you have Oracle 11.1 or higher, then you could make it a little simpler, using the SELECT ... PIVOT feature in the main query.
Maybe you are looking for
-
Can anyone tell me how to print ical todo items by category?
-
Photoshop CS - Saving file as jpg
Hi I am using CS v8.0 and using a javascript action script. I seem to be having all kinds of problems saving the document as a .jpg though. I simply want to open all the files in a folder, resize the image and save the file with a different name (in
-
W530 running Windows 7: can't use optimal resolution on Samsung S27A850DS monitor
Hello, I use a Lenovo W530 laptop with the NVIDIA Quadro K1000M video card running Windows 7. Recently, I got a Samsung S27A850DS monitor with an optimal resolution of 2560 x 1440. However, the best resolution for this monitor on my system is listed
-
Buildtms on Solaris 8(SPARC)
Help!!! Environment:oracle 9i,Solaris 8(SPARC,64),Tuxedo 8.0,GCC3.2 Input:buildtms -o ORA_TMS9i -r Oracle_XA Output: gcc: language arch=v9 not recognized gcc: language arch=v9 not recognized gcc: language arch=v9 not recognized gcc: language arch=v9
-
Change font with a bottom.
Hi, I couldn't find an answer for this but: Is there a way to chnage the font in the front panel by pressing a button? As you can see, I am building a farily simple starwars-theme-application, what I want to to is to use that funky starwar font ( lik