Select Count
My query:SELECT distinct ap.project_files_location, ddh.file_name, dur.*
Of admin_project ap
INNER JOIN hard dmand_user_report on dur.admin_project_oid = ap.admin_project_oid
INNER JOIN dmand_docsys_hier on ddh.admin_report_oid = dur.admin_report_oid ddh
WHERE ddh.file_name = '_TESTFILE.pdf' and ap.project_files_location = '_Monthly_Reports\\' and dur.admin_user_oid = 983
Product 5 lines. I want to choose the number of rows produced by this query in a variable. How do you do that?
Thank you
Well, there are many possible solutions...
The simplest one:
If you already know that the query returns 5 lines then you can:
myvar := 5;
You can choose the number in a variable:
select count(*)
into myvar
from
(
SELECT distinct ap.project_files_location, ddh.file_name, dur.*
FROM admin_project ap
INNER JOIN dmand_user_report dur on dur.admin_project_oid = ap.admin_project_oid
INNER JOIN dmand_docsys_hier ddh on ddh.admin_report_oid = dur.admin_report_oid
WHERE ddh.file_name = "_TESTFILE.pdf" and ap.project_files_location = "_Monthly_Reports
" and dur.admin_user_oid=983
)
You can have a variable from PL/SQL table and bulk get all inside recods. Then you can check the number of records in the table using PL/SQL. COUNTING method:
SELECT distinct ap.project_files_location, ddh.file_name, dur.*
BULK COLLECT INTO mytabofrecords
FROM admin_project ap
INNER JOIN dmand_user_report dur on dur.admin_project_oid = ap.admin_project_oid
INNER JOIN dmand_docsys_hier ddh on ddh.admin_report_oid = dur.admin_report_oid
WHERE ddh.file_name = "_TESTFILE.pdf" and ap.project_files_location = "_Monthly_Reports
" and dur.admin_user_oid=983;
myvar := mytabofrecords.COUNT;
You can also use "implicit cursor attribute" SQL ROWCOUNT % as in the example below:
INSERT INTO mynewtable
SELECT distinct ap.project_files_location, ddh.file_name, dur.*
FROM admin_project ap
INNER JOIN dmand_user_report dur on dur.admin_project_oid = ap.admin_project_oid
INNER JOIN dmand_docsys_hier ddh on ddh.admin_report_oid = dur.admin_report_oid
WHERE ddh.file_name = "_TESTFILE.pdf" and ap.project_files_location = "_Monthly_Reports
" and dur.admin_user_oid=983;
myvar := SQL%ROWCOUNT;
Need more options?
Tags: Database
Similar Questions
-
What is the difference between count (1) selection of the tab and select count (*) tab;
994122 wrote:
Hello
SQL > set timing on
SQL > select count (*) of the emp
2 where deptno = 30;
COUNT (*)
---------------
6
Elapsed time: 00:00:00.01
SQL > select count (1) of the emp
2 * where deptno = 30
SQL > /.
COUNT (1)
-------------
6
Elapsed time: 00:00:00.01
See that both give the same time
Do you really think the 100th's of a second it takes 6 rows from counting will truly representative of the difference it takes actually?
Such a small data set cannot possibly show no difference to these larger units of time (in the calculation of terms where we have millions of instructions per second in lieu)
As the link to the FAQ provided in the response of Anton (first response on this thread) Watch, count (1) and count (*) are essentially the same, except that count (1) actually get re-written by the query re - write the step of the optimization to make it count (*).
Generally 'count (*)' makes more sense language, because it reads like "count" while "count (1)" reads as "count we ', which does not have as much good sense when you actually count all.
-
Get the select count (*) including zero '0 '.
Hi gurus,
I have the problem here, I have this request:
SELECT COUNT (t.column1) AS mycount.
To_char(t.CREATION_DATE,'MONTH') AS themonth
Table 1 t
WHERE the t.creation_date BETWEEN TO_DATE('01/01/2011') AND TO_DATE('09/07/2011') AND t.column1 > 0
GROUP OF TO_CHAR (t.creation_date, 'MONTH')
ORDER BY TO_DATE (TO_CHAR (t.creation_date, 'MONTH'), 'MONTH')
then I get this query result
MYCOUNT. THEMONTH
-------------- ----------------
JANUARY | 3
MARCH | 6
APRIL | 5
CAN | 9
JULY | 2
and I need like this
MYCOUNT. THEMONTH
-------------- ----------------
JANUARY | 3
FEBRUARY | 0
MARCH | 6
APRIL | 5
CAN | 9
JUNE | 0
JULY | 2
what I am doing wrong? or what I can add to the code? Please, I hope you can help with this one.
Best regards
MentorMentor wrote:
Hey and when I don't have a date range and did well id how can I get the result?
SELECT NVL(COUNT(column1),0) mycount, TO_CHAR(TRUNC(creation_date,'MM'),'MONTH') themonth FROM ( SELECT column1, creation_date FROM table1 WHERE column_id IN (2,3,12,67,49,182) AND column1 > 0 UNION ALL SELECT NULL column1, ADD_MONTHS(min_creation_date,LEVEL - 1) creation_date FROM ( SELECT TRUNC(MIN(creation_date),'MM') min_creation_date, TRUNC(MAX(creation_date),'MM') max_creation_date FROM table1 WHERE column_id IN (2,3,12,67,49,182) AND column1 > 0 ) CONNECT BY ADD_MONTHS(min_creation_date,LEVEL - 1) <= LAST_DAY(max_creation_date) ) GROUP BY TRUNC(creation_date,'MM') ORDER BY TRUNC(creation_date,'MM') /
>
and another question, how can I get the two counts of the same column where the condition its diferent?
SELECT NVL(COUNT(CASE column2 WHEN 'JOHN' THEN 1 END),0) count_a, NVL(COUNT(CASE column2 WHEN 'MICHAEL' THEN 1 END),0) count_b, TO_CHAR(TRUNC(creation_date,'MM'),'MONTH') themonth FROM ( SELECT column2, creation_date FROM table1 WHERE column2 IN ('JOHN','MICHAEL') UNION ALL SELECT NULL column2, ADD_MONTHS(min_creation_date,LEVEL - 1) creation_date FROM ( SELECT TRUNC(MIN(creation_date),'MM') min_creation_date, TRUNC(MAX(creation_date),'MM') max_creation_date FROM table1 WHERE column2 IN ('JOHN','MICHAEL') ) CONNECT BY ADD_MONTHS(min_creation_date,LEVEL - 1) <= LAST_DAY(max_creation_date) ) GROUP BY TRUNC(creation_date,'MM') ORDER BY TRUNC(creation_date,'MM') /
SY.
-
Set a variable to Select count (*)
Hello
I have the following statement that produce a result of '8' in Oracle SQL Developer.
SELECT count (*)
LIB_ISBN_T p,
Table (XMLSequence)
extract (p.ISBN_PAYLOAD,
((("/ ISBNdb/BookList/BookData/subjects/subject")));
How can I get this value in a numeric variable so that I can make some statements about the variable If?
I am using Oracle 10 g and APEX 4
Thanks a lot for your help.
Published by: PhilMan2 on November 29, 2010 16:34I learned that I had to use the INTO clause.
-
Pl/sql script and select count()
Hello!
Is it possible somehow to put the result of (Select Select) count (h.id) in the "utl_file.put_line" (?)
in this script, plsql?
declare
cursor kursori is
SELECT count (h.id), n.state
OF popperson h, popnationality n
WHERE n.state > 3 and (n.state < 246) or n.state > 246
and h.registerstate = 1 and h.id = n.state group n.poppersonid;
Tietue kursori % ROWTYPE;
tiedosto utl_file.file_type;
Start
tiedosto: = utl_file.fopen (' TEST_OUTPUTDIR ',' test.txt ', 'W');
Open kursori;
loop
extract the kursori in tietue;
When the output kursori % NOTFOUND;
() UTL_FILE.put_line
tiedosto, 'Code '. Tietue. State | ' ' || Tietue. ?) ;
end loop;
close kursori;
UTL_FILE.fclose (tiedosto);
end;
/
Thank you! OVdeclare cursor kursori is SELECT count(h.id) cnt, n.state FROM popperson h, popnationality n WHERE n.state > 3 and (n.state < 246 or n.state > 246) and h.registerstate=1 and h.id=n.poppersonid Group by n.state; tietue kursori%ROWTYPE; tiedosto utl_file.file_type; begin tiedosto := utl_file.fopen ('TEST_OUTPUTDIR','test.txt', 'W'); open kursori; loop fetch kursori into tietue; exit when kursori%NOTFOUND; utl_file.put_line ( tiedosto, 'Code ' || tietue.state || ' ' || tietue.cnt); end loop; close kursori; utl_file.fclose(tiedosto); end; /
p.s: not tested.
Concerning
REDA
-
Select count (1) in empty_table
Hi all
I would like to know why "select count (1) of empty_table ' extractions value 0 instead of return null when the empty_table isn't contains anydata?
Thank youHello
A query that uses an aggregation (such as COUNT) function will produce a line of production by group, unless you discard some groups with a HAVING clause.
If you do not have a GROUP BY clause, there will be a group (which includes the entire results set) and therefore a row of output.
COUNT always returns an integer, never NULL.If you really need 0 lines of output in this situation, add a HAVING clause:
SELECT COUNT (1) FROM empty_table HAVING COUNT (*) > 0 ;
If you really need an output line, but NULL in the column NUMBER, use NULLIF:
SELECT NULLIF ( COUNT (1) , 0 ) FROM empty_table ;
-
Select count (1) vs count (*) select
I apologize if this has been requested previously. Can someone tell me what is the difference between
as opposed toselect count(1) from <mytable>
?select count (*) from <mytable>
Thank youSelect count (1) in the table
is faster than
Select count (*) in the table
but as long as it is to write the statement - reason: you don't have to hit the shift key for writing a 1, while you need the SHIFT key to get a * ;-)
-
SELECT COUNT (*) FROM...
Hello
I added an oracle db in my project, but I want to have the number of lines in the comics so I decided to use the SELECT Count (*) FROM... command in the BeginIn, but it does not recognize the * so I wrote it in a business auto and nothing.
the problem says:
' * ' should not: S: S an idea?
Thank youHello
Try:
numberOfRows as Int for each element in SELECT count(*) as cnt FROM customer order by name do numberOfRows = element.cnt end
Hope this helps,
Dan -
Stored error for a SELECT COUNT... in proc
DB version: 10 gr 2
I was getting error when compiling for a stored procedure for the
under IF condition
I don't get this error in DB2. It seems that in Oracle, I need put the result of this COUNT in a variable and use it in the IF condition. Is there an alternative? Why Oracle can't handle such a SELECTION within the AOC?if (select count(1) from ship_trk_dtl where order_id = p_orderid and order_status =110) > 0 then ...
the error was
Published by: kraljic on March 4, 2009 04:05LINE/COL -------------------------------------------------------------------------------- ERROR -------------------------------------------------------------------------------- 128/9 PLS-00103: Encountered the symbol "SELECT" when expecting one of the following: ( - + case mod new not null others <an identifier> <a double-quoted delimited-identifier> <a bind variable> avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date <a string literal with character set specification> <a number> <a single-quoted SQL string> pipe LINE/COL -------------------------------------------------------------------------------- ERROR -------------------------------------------------------------------------------- <an alternatively-quoted string literal with character set specification> <an alternativ 129/35 PLS-00103: Encountered the symbol ")" when expecting one of the following: * & - + ; / at for mod remainder rem <an exponent (**)> and or group having intersect minus order start union where connect || multiset
Hello
Born to be used IN the clause to store the code SQL train in a variable. You can use the varuiable with IF.
SQL> DECLARE 2 a NUMBER(2); 3 BEGIN 4 SELECT COUNT(*) INTO a FROM DUAL; 5 DBMS_OUTPUT.PUT_LINE(a); 6 END; 7 / PL/SQL procedure successfully completed. SQL> set serveroutput on SQL> / 1 PL/SQL procedure successfully completed. SQL>
See you soon,.
-
Is it necessary that 'we should select count (*)' before deleting the recor
Hai...
I need to delete a record in my application for which I wrote a process of pl/sql below...
DECLARE
Symbol varchar2 (20);
CTN number (1);
BEGIN
Symbol: =: P1_HIDDEN_FLAG;
Select count (*) in ctn to emp1 where EMPNO = symbol;
If NTC > 0 then
Remove from emp1 where EMPNO = symbol;
End if;
End;
why we must select count (*) table... If we try to remove a record that is not available in the table... How can we handle this Exception? There are also that "we should select count () before you delete the record from the table... *"
Please tell me...
David...Hello
You can use SQL % number of LINES:
delete from emp where empno = p; if sql%rowcount = 0 then -- Nothing deleted! end if;
Greetings,
Roelhttp://roelhartman.blogspot.com/
http://www.bloggingaboutoracle.org/
http://www.Logica.com/ -
SELECT count VS table1 (column1) select count (*) from table1
SELECT count VS table1 (column1) select count (*) from table1
Which is faster, or are they equal in the aspect of the effectiveness and the cost of memory?Hello
Reading this thread Øvre Asktom. You do not need to read anything except this, that would be perfectly answer to your question.
http://asktom.Oracle.com/pls/asktom/f?p=100:11:P11_QUESTION_ID:1156159920245
HTH
Aman... -
How COUNT (*) SELECT all first before returning SELECT *.
Our interface involves a grid filled by a SELECT query. The records are retrieved via a complex query with many tables involved and spent many filtering parameters. A simplified example:
SELECT col1, col2,... colx
OF tab1 tab2, tab 3 t3 t2, t1, etc.
WHERE
T1.Key1 = t2.key1
and t2.key2 = t3.key2,
and... etc.
and t1.coldate > p_FilterDateFrom
and t1.coldate < p_FilterDateTo
and t2.somefield = p_FilterSomeFilter
and t3.someotherfield = p_FilterSomeOtherField
and... etc.
Generally, the user enters parameters, so a small, manageable resultset is returned.
But sometimes the user will enter the parameters that returns thousands of lines. As a result, the frontend in IE makes a break from time to time say ' Stop running this script? A script on this page slows down your web browser to run slowly... »
I would like to run the above example query with a COUNT (*) SELECT all first before running the actual SELECT col1, col2, col3. This way if the rows returned exceeds a certain threshold, say, 500 lines, I could pop a message saying "more than 500 rows will be returned. Do you want to continue? ». If the user clicks Yes, then I go ahead and perform the actual query and fill the grid. If the user clicks on no, then the user gets a chance to adjust its settings.
Y at - it an easy way to do short of coding (essentially) two versions of the same complex query, one for "SELECT COUNT (*)" and the other for "SELECT?"
Thank you.
Hey, Justin (or someone else). I quite agree with you on this... However, just to play the lawyer of the devil for a second... (or maybe not... so why I asked)
The COUNT (*) Analytics as a separate and additional column has been added to the query would be a significant hit to the performance? I have simply discarded with a little, but set of samples was not big enough - I'll try a larger, later when I get time.
something like:
- SELECT *.
- DE)
- SELECT col1, col2,... colx, count (*) NTC)
- OF tab1 tab2, tab 3 t3 t2, t1, etc.
- WHERE
- T1.Key1 = t2.key1
- and t2.key2 = t3.key2,
- and... etc.
- and t1.coldate > p_FilterDateFrom
- and t1.coldate<>
- and t2.somefield = p_FilterSomeFilter
- and t3.someotherfield = p_FilterSomeOtherField
- and... etc.
- order of something)
- where rownum<=>=>
So, you can then enter the total number of any line (probably the first) and know how much you have?
I'm sure it's more work than simply without... However, I guess that the real question... How much better it is than to the separate account? (if applicable)
-
How to get the total line count COUNT (*) SELECT and put on a page?
Hello
I use JDeveloper 10.1.3.4. I need get the total number of rows in a table and display it on a page and the problem in doing so. At the sqlplus prompt the row count simply would be, for example:
I wonder if having this simple number must be so complicated and if there is more simple, better ways. Here's how I do it and the problem encountered.select count(*) from BILL;
1. the name of the page to display the number is summary.jspx. It has a grain of support that "summed" as the managed bean name in faces - config.xml, and the name of the bean class is "summary." The component output on the page is:
2. in summary the bean code is:<h:outputText value="#{summary.totalStudentsCount}" binding="#{summary.outputText5}" id="outputText5"/>
The idea of calling a stored function usnig as a "helper" method is 25.5.3 in the Developer's guide, which is closest to you in my need. It's for the functions with the one IN argument; but in my case, the function is not any argument IN. That's why, when you call the helper method CallStoredFunction(), I gave an empty as the last argument and amazing array that caused the problem:private Number totalStudentsCount; public static int NUMBER = Types.NUMERIC; public void setTotalStudentsCount(Number totalStudentsCount) { this.totalStudentsCount = totalStudentsCount; } public Number getTotalStudentsCount() { ZBLCModuleImpl zblcam = getZBLCModuleImpl(); LoggedInStudentImpl studentTable = (LoggedInStudentImpl)zblcam.getLoggedInStudent(); String sql = "select count(lsap_uid) from BILL"; studentTable.setQuery(sql); return (Number)CallStoredFunction(NUMBER, "get_total_students(?)", new Object[] {}); } private ZBLCModuleImpl getZBLCModuleImpl() { FacesContext fc = FacesContext.getCurrentInstance(); ValueBinding vb = fc.getApplication().createValueBinding("#{data}"); BindingContext bc = (BindingContext)vb.getValue(fc); DCDataControl dc = bc.findDataControl("ZBLCModuleDataControl"); ApplicationModule am = (ApplicationModule)dc.getDataProvider(); return (ZBLCModuleImpl)am; } protected Object CallStoredFunction(int sqlReturnType, String stmt, Object[] bindVars) { CallableStatement st = null; ZBLCModuleImpl zblcam = getZBLCModuleImpl(); try { st = zblcam.getDBTransaction().createCallableStatement("begin ? := " + stmt + "; end", 0); st.registerOutParameter(1, sqlReturnType); if (bindVars != null) { for (int z = 0; z < bindVars.length; z++) { st.setObject(z + 2, bindVars[z]); } } st.executeUpdate(); return st.getObject(1); } catch (SQLException e) { throw new JboException(e); } finally { if (st != null) { try { st.close(); } catch (SQLException e) { throw new JboException(e); } } } }
3. the registered function has been tested and works fine at the sqlplus prompt:return (Number)CallStoredFunction(NUMBER, "get_total_students(?)", new Object[] {});
4. when the summary.jspx page is run, the browser is full of error messages, the first long line is here (I have split several online for ease of reading):create or replace function get_total_students return NUMBER AS v_student_count NUMBER; BEGIN select count(ldap_uid) into v_student_count from bill; return v_student_count; END;
Thus,.javax.faces.el.EvaluationException: javax.faces.el.EvaluationException: Error getting property 'totalStudentsCount' from bean of type zblc.viewcontroller.backing.staff.Summary: oracle.jbo.JboException: JBO-29000: Unexpected exception caught: java.sql.SQLException, msg=Missing IN or OUT parameter at index:: 2
(1) what is the problem? What is this parameter IN or OUT of {color: red} index: 2 {color} consult? It has to do with the empty array as the last argument in the call to the helper method?
(2) this approach is an overdose, and are there more simple and better ways?
Thank you very much for help!
NewmanHello
Is there a specific reason why you don't simply create read only object to display with some count (*) as OFCASES from MYTABLE and then just drag and drop the attribute ofcases in page?
Kind regards
Branislav
-
count all values with a special WHERE clause in a select group?
Hello
I have the following table 1:
code, month, value
* 1,1,40 *.
* 1,2,50 *.
* 1,3,0 *.
* 1,4,0 *.
* 1,5,20 *.
* 1,6,30 *.
* 1,7,30 *.
* 1,8,30 *.
* 1,9,20 *.
* 1,10,20 *.
* 1,11,0 *.
* 1,12,0 *.
* 2,1,10 *.
* 2,2,10 *.
* 2,3,20 *.
* 2,4,20 *.
* 2,5,20 *.
* 2,6,30 *.
* 2,7,40 *.
* 2,8,50 *.
* 2,9,20 *.
* 2,10,20 *.
* 2,11,20 *.
* 2,12,20 *.
This is a table with 3 columns, first column is a code, second is the number of months, third, one is a value.
Now, I want to select records for each code. For example, all records for the code = 1.
I want to count how many values = 0 for this code = 1. After this count, I want to update the value with the number 0.
For my example:
Code 1, there are 4 fields with the value 0. So, I want to update all values from code 1 to 4.
The second code = 2 there is no value = 0. So, I want to update the values of code2 to 0.
This should be the result:
code, month, value
* 1,1,4 *.
* 1,2,4 *.
* 1,3,4 *.
* 1,4,4 *.
* 1,5,4 *.
* 1,6,4 *.
* 1,7,4 *.
* 1,8,4 *.
* 1,9,4 *.
* 1,10,4 *.
* 1,11,4 *.
* 1,12,4 *.
* 2,1,0 *.
* 2,2,0 *.
* 2,3,0 *.
* 2,4,0 *.
* 2,5,0 *.
* 2,6,0 *.
* 2,7,0 *.
* 2,8,0 *.
* 2,9,0 *.
* 2,10,0 *.
* 2,11,0 *.
* 2,12,0 *.
My question is:
Is it possible in oracle to count in a select (or in an insert/update statement) all values = 0 for a group (in this example, named code) and make an update in the same statement for this group?
I hope someone can give me a hint, if possible?
Thank you very much.
Best regards
Timsomething like that? NOT TESTED *.
Setting a DAY Table1 a SET A.Value = (SELECT COUNT (B.ROWID) FROM Table1 B WHERE A.Code = B.Code AND B.Value = 0);
-
Could this be possible SELECT MAX (COUNT (ID))?
Hi all
I created the following table:CREATE TABLE ReturnedGoods
And I have these data in the table:
(
ID NUMBER(10) PRIMARY KEY,
ReturningReason VARCHAR2(50) NOT NULL,
);
ID | ReturningReason
I want to get most ReturningReason occurrence, so I get the count of each ReturningReason, but I can not choose the most of them
-------------------------------------
1 | 'Corrupted'
2 | 'Bad'
3 | 'Corrupted'
4 | 'Antics;
. | .
. | .
. | .
--Code Select COUNT for every ReturningReason
So, how to get maximum form them?
SELECT COUNT(ID), ReturningReason
FROM Returned_Goods
GROUP BY ReturningReason
Thanks in advance :)Try this:
select *from ( SELECT COUNT(ID) cnt, ReturningReason FROM ReturnedGoods GROUP BY ReturningReason ) where rownum=1
Maybe you are looking for
-
Is that what Apple alert email legitimate?
I received an email this morning indicating my user ID was used to download an application on a device not associated with my account. The e-mail provided a link to reset my password. I have not used the link, but I did go to the Apple site and cha
-
Tecra A50-A-12F - how to set the new SSD to be recognized correctly?
Greetings, I installed a new SSD in my laptop, the operating system starts normally.I bought a caddy for the old drive. Before editing deleted the partition and all connected with it. After all mounted when I start the system can not find because it
-
OfficeJet 6700 Premium: HP Officejet 6700, black ink fades
I had my bonus of 6700 Officejet for over a year, so it's just more under warranty. About 3 days ago, out of the blue, the black ink started fading out if I printed a page with a lot on it. It does the same thing if I copy something. (I'm only printi
-
Windows Home Server + Config IP/LAN WRT54G problems
Hello I tried to fix this for days now, and I feel more I read on questions from other people who appear to be similar (at the beginning), I realize that I don't know even where to start... [: Confused:] What I know: I can't access my Windows Home Se
-
Sony MP3 player will not sync on PC
SONY MP3 PLAYER MY MP3 SONY READER WILL NOT SYNC TO MY COMPUTER