Count of Column
I'm using Derby 10.5 via Command Interpreter
I stucked at the count of column
I need to know the command for getting number of columns used in a table .
ibanezplayer85 wrote:
Do you mean JTable? If so, check out the API. There's a method called [getColumnCount()|http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTable.html#getColumnCount()] . If you're using a table model, there should also be a getColumnCount() method.
If this isn't what you meant, you will probably have to clarify.My guess: that this isn't a Java question but rather is a Derby question, but this is just a SWAG. Who knows?
Similar Messages
-
All,
Could anyone please clarify me on , how we can count the column's which were used in a query.
Ex: SELECT ENAME, EMPNO, SAL , JOB FROM EMP;
--Here in the above query, I have taken 4 columns(counted manually) . Instead of counting it manually is there any other way.
ThanksIt sounds like you're creating dynamic SQL. Why are you doing that? Dynamic SQL's would seem to indicate you don't know the structure of your own database or that your application design is trying to be generic rather than being designed in proper modularized units (1 unit does 1 task, not multiple generic tasks). 99.999% of the time, people using dynamic SQL indicates that they are misusing the database or haven't designed things properly.
If there's really a valid reason for using dynamic SQL, then of course you can do it properly and use all the features of Oracle to get information about the dynamic SQL, but that means not using something as poor as EXECUTE IMMEDIATE (which is often abused by most people), or trying to use ref cursors within PL/SQL (which are more intended for 3rd party application layers). The power of dynamic SQL is gained from using the DBMS_SQL package... as in the following simplistic example...
SQL> set serverout on
SQL> create or replace procedure run_query(p_sql IN VARCHAR2) is
2 v_v_val varchar2(4000);
3 v_n_val number;
4 v_d_val date;
5 v_ret number;
6 c number;
7 d number;
8 col_cnt integer;
9 f boolean;
10 rec_tab dbms_sql.desc_tab;
11 col_num number;
12 v_rowcount number := 0;
13 begin
14 -- create a cursor
15 c := dbms_sql.open_cursor;
16 -- parse the SQL statement into the cursor
17 dbms_sql.parse(c, p_sql, dbms_sql.native);
18 -- execute the cursor
19 d := dbms_sql.execute(c);
20 --
21 -- Describe the columns returned by the SQL statement
22 dbms_sql.describe_columns(c, col_cnt, rec_tab);
23 --
24 -- Bind local return variables to the various columns based on their types
25
26 dbms_output.put_line('Number of columns in query : '||col_cnt);
27 for j in 1..col_cnt
28 loop
29 case rec_tab(j).col_type
30 when 1 then dbms_sql.define_column(c,j,v_v_val,2000); -- Varchar2
31 when 2 then dbms_sql.define_column(c,j,v_n_val); -- Number
32 when 12 then dbms_sql.define_column(c,j,v_d_val); -- Date
33 else
34 dbms_sql.define_column(c,j,v_v_val,2000); -- Any other type return as varchar2
35 end case;
36 end loop;
37 --
38 -- Display what columns are being returned...
39 dbms_output.put_line('-- Columns --');
40 for j in 1..col_cnt
41 loop
42 dbms_output.put_line(rec_tab(j).col_name||' - '||case rec_tab(j).col_type when 1 then 'VARCHAR2'
43 when 2 then 'NUMBER'
44 when 12 then 'DATE'
45 else 'Other' end);
46 end loop;
47 dbms_output.put_line('-------------');
48 --
49 -- This part outputs the DATA
50 loop
51 -- Fetch a row of data through the cursor
52 v_ret := dbms_sql.fetch_rows(c);
53 -- Exit when no more rows
54 exit when v_ret = 0;
55 v_rowcount := v_rowcount + 1;
56 dbms_output.put_line('Row: '||v_rowcount);
57 dbms_output.put_line('--------------');
58 -- Fetch the value of each column from the row
59 for j in 1..col_cnt
60 loop
61 -- Fetch each column into the correct data type based on the description of the column
62 case rec_tab(j).col_type
63 when 1 then dbms_sql.column_value(c,j,v_v_val);
64 dbms_output.put_line(rec_tab(j).col_name||' : '||v_v_val);
65 when 2 then dbms_sql.column_value(c,j,v_n_val);
66 dbms_output.put_line(rec_tab(j).col_name||' : '||v_n_val);
67 when 12 then dbms_sql.column_value(c,j,v_d_val);
68 dbms_output.put_line(rec_tab(j).col_name||' : '||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'));
69 else
70 dbms_sql.column_value(c,j,v_v_val);
71 dbms_output.put_line(rec_tab(j).col_name||' : '||v_v_val);
72 end case;
73 end loop;
74 dbms_output.put_line('--------------');
75 end loop;
76 --
77 -- Close the cursor now we have finished with it
78 dbms_sql.close_cursor(c);
79 END;
80 /
Procedure created.
SQL> exec run_query('select empno, ename, deptno, sal from emp where deptno = 10');
Number of columns in query : 4
-- Columns --
EMPNO - NUMBER
ENAME - VARCHAR2
DEPTNO - NUMBER
SAL - NUMBER
Row: 1
EMPNO : 7782
ENAME : CLARK
DEPTNO : 10
SAL : 2450
Row: 2
EMPNO : 7839
ENAME : KING
DEPTNO : 10
SAL : 5000
Row: 3
EMPNO : 7934
ENAME : MILLER
DEPTNO : 10
SAL : 1300
PL/SQL procedure successfully completed.
SQL> -
Set count of column in database......
hi All,
i just wanna to ask, it is possible that i can set count of column depends on data/input.
for example;
in common, when 2 column, code sql like:
String query3 = "INSERT INTO Sheet5(Rule, Weight)" + "VALUES ('"+ finalRule+"', '"+weight+"')";but if n column, how?
anybody knows or give me some idea to handle that..
thanks.what do you means by using preparedStatement?
is it like this
String sql1 = "SELECT empno FRom emp WHERE empno = ?";
String sql2 = "INSERT INTO emp VALUES (?,?,?,?,?,?,?,?)";
PreparedStatement pstmt1 = conn.prepareStatement(sql1);
PreparedStatement pstmt2 = conn.prepareStatement(sql2);
pstmt1.setInt(1, 9999);
ResultSet rset = pstmt1.executeQuery();
if(rset.next()){
System.out.println("The employee");
rset.close();
else {
pstmt2.setInt(1, 99990);
pstmt2.setString(2, "CHARLIE");
pstmt2.setString(3, "ANALYST");
pstmt2.setInt(4, 7566);
pstmt2.setString(5, "01-jan-01");
pstmt2.setFloat(6, 12000);
pstmt2.setFloat(7, (float)10.5);
pstmt2.setInt(8, 10);
pstmt2.executeUpdate();
}so, i still need to write 8 times '?' for 8 column.
but how if i don't know count of column? -
Query to count of Columns??
How to count the number of columns of a table??
Thanks in Advance,
Babjan.Ora wrote:
select count(1) from user_tab_cols where table_name = 'TEMP'
There is a slight difference between user_tab_cols and user_tab_columns. And user_tab_cols is not suitable for counting table columns:
SQL> create table tbl(n number)
2 /
Table created.
SQL> select count(*)
2 from all_tab_cols
3 where owner = 'SCOTT'
4 and table_name = 'TBL'
5 /
COUNT(*)
1
SQL> select count(*)
2 from all_tab_columns
3 where owner = 'SCOTT'
4 and table_name = 'TBL'
5 /
COUNT(*)
1
SQL> create index tbl_fbi1
2 on tbl(nvl(n,0))
3 /
Index created.
SQL> select count(*)
2 from all_tab_cols
3 where owner = 'SCOTT'
4 and table_name = 'TBL'
5 /
COUNT(*)
2
SQL> select count(*)
2 from all_tab_columns
3 where owner = 'SCOTT'
4 and table_name = 'TBL'
5 /
COUNT(*)
1
SQL> SY. -
IR - if count (primary key) cannot change to count different column
Apex 4.1.1.00.23 Windows 7 IE8 / Firefox 16
If I add a Group By and Count to an Interactive report and choose the primary key column as the one to Count, run the report and then edit the Group By to count a different column, the result set does not change, and if I edit the Group By again it shows that the Counted column has reverted back to the primary key column.
If I initially choose a different column I can change it and rerun the report successfully, but once I choose the primary key column it cannot be changed. This seems to happen on all applications and all browsers. Is it a bug in Apex?
Thanks,
Nick.I've tried this in 4.1.0.00.32: works normally. In 4.1.1.00.23 however i'm getting the weird behaviour: you can count on any column, but the moment you count the same column as the one grouped on you can not change the column back to another one anymore. Something which works fine in 4.1.0.00.32.
-
Count of Column based on Value
Hi All
In our database table we have 3 columns closedate,empcode,status.
I am using the following query to fetch the data required :
select closedate,count(empcode) COUNT,EMPCODE,status from dbcleaning WHERE STATUS='Fixed' and EMPCODE='E315' group by closedate,STATUS,EMPCODE.
the result is like this :
closedate count EMPCODE STATUS
2010-10-10 98 E315 Fixed
2010-10-11 15 E315 Fixed
However i want the data to be based on DISTINCT EMPCODE , fo Eample :
closedate EMPCODE(ST001) EMPCODE(ST002) STATUS
2010-10-10 *98 125* Fixed
2010-10-11 *128 225* Fixed
I hope i had explained properly
Replies are highly appreciated.
Thank You
Edited by: user9366471 on Jun 30, 2010 12:29 AMIt's always a better idea to post DDL of your table and INSERT statement. Also don't forget to mention the output you required. It's even better if you use tag for those DDL and DMLs.
Your database version is a important thing for exact solution. -
Count of total number of rows, distinct count of column
Hi ,
I am looking for a procedure that will insert following data into columns :
Table_name Column_name COUNT(*) , count(DISTINCT COL_NAME)
EX: Employee emp_name 5000 4500
Employee emp_location 5000 10
I want this for the whole schema.
I have a procedure which give me count(*) , but i am not able to write a procedure for count(DISTINCT COL_NAME)
If some one can help me, that would be the really helpful
ThanksSOmething like this could also work ->
satyaki>
satyaki>select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
Elapsed: 00:00:00.50
satyaki>
satyaki>
satyaki>SELECT table_name,
2 column_name,
3 DBMS_XMLGEN.getxmltype ('SELECT Count(*) c FROM ' || table_name).EXTRACT ('//text()').getnumberval() tot_rows,
4 DBMS_XMLGEN.getxmltype ('SELECT Count( distinct '||column_name||') d FROM ' || table_name).EXTRACT ('//text()').getnumberval() unique_rows
5 FROM all_tab_columns
6 WHERE owner = 'SCOTT'
7 AND table_name in ('DEPT','SAL_GRADE','GRADE');
TABLE_NAME COLUMN_NAME TOT_ROWS UNIQUE_ROWS
DEPT DEPTNO 4 4
DEPT DNAME 4 4
DEPT LOC 4 3
GRADE SUBJECT 2 2
GRADE UPN 2 2
GRADE L_EVEL 2 2
GRADE GRADE 2 2
GRADE TID 2 2
GRADE ACADEMICYEAR 2 1
SAL_GRADE MAX_SAL 6 6
SAL_GRADE MIN_SAL 6 6
TABLE_NAME COLUMN_NAME TOT_ROWS UNIQUE_ROWS
SAL_GRADE GRADE 6 6
12 rows selected.
Elapsed: 00:00:01.06
satyaki>
satyaki>
satyaki>You may have to work a little - if you want to use this for all the case.
Regards.
Satyaki De.
Added column name
Off course credit goes to Michael. ;)
Edited by: Satyaki_De on Jan 27, 2009 12:19 AM -
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 -
Counter in column GUI ALV GRID
Hello every one, I have a alv grid, and i have a column where i need to put a counter in the cell, so every time a new row is added, the cell of that column will be incrementing in a number and show it in the cell automatic.
Im new in abap, so i hope could show me some code to do it. ThanksHi Naimesh, thak you for your answer, it helps me to solve the problem, i let the code here for other people could use it.
DATA:
vl_consec TYPE I,
p_data TYPE REF TO cl_alv_changed_data_protocol.
wa_rowid TYPE lvc_s_roid,
wa_inserted_rows TYPE lvc_s_roid,
wa_dll LIKE LINE OF vg_ti_dll,
wa_insert LIKE LINE OF vg_ti_dll.
"get the row inserted using the class CL_ALV_CHANGED_DATA_PROTOCOL.
LOOP AT p_data->mt_roid_front INTO wa_rowid.
READ TABLE p_data->mt_inserted_rows INTO wa_inserted_rows WITH KEY row_id = wa_rowid-row_id.
CHECK sy-subrc = 0.
"get the highest number from the table
SORT vg_ti_dll BY consec DESCENDING.
READ TABLE vg_ti_dll INTO wa_dll INDEX 1.
wa_insert-consec = wa_dll-consec.
INSERT wa_insert INTO vg_ti_dll INDEX wa_insert-consec.
CALL METHOD p_data->modify_cell
EXPORTING
i_row_id = wa_rowid-row_id
i_fieldname = 'CONSEC'
i_value = wa_dll-consec.
ENDLOOP. -
Problem with performing calculations on COUNT-aggregated columns
Hi guys,
I have something weird - 2 columns with Aggregation set to Count, both are from the same Fact table. Indicators are in the same Fact table.
Count column A (with FILTER key_column USING Indicator1='yes')
Count column B (with FILTER key_column USING Indicator1='yes' and Indicator2='yes')
They show fine by themselves.
However, when I create a column C where:
Column B / Column A - I get nulls.
I tried both filtering logical content with Filter and also running CASE WHEN , etc.
Anyone else had it?Well, it certainly has to do something with IFNULL. Now, when I did what you suggested, I get correct numbers, but only after i drill-down from top level. I'll try to tweak with levels and see what's up.
Success:
not only it's important to use IFNULL, but also it's important to set level aggregation for A and B (i set it to Fiscal Year). Thanks for assistance
Message was edited by:
wildmight -
Adding count total column .
Hi,
I'm dealing with 9.2.0.8 so some restrictions may apply :).
here is the case
SQL> create table t (id number) ;
Table created.
SQL> insert into t values(1);
1 row created.
SQL> insert into t values(1);
1 row created.
SQL> insert into t values(1);
1 row created.
SQL> insert into t values(2);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from t;
ID
1
1
1
2
SQL> select count(*), id , ratio_to_report(count(*)) over () *100 pct , count(*) over () from t group by id;
COUNT(*) ID PCT COUNT(*)OVER()
3 1 75 2
1 2 25 2
but I need last column as all rows in table so should be
SQL> select count(*), id , ratio_to_report(count(*)) over () *100 pct , count(*) over () from t group by id;
COUNT(*) ID PCT COUNT(*)OVER()
3 1 75 4
1 2 25 4
how to rewrite that, ratio_to_report gives percentage which is quite good but need all rows .Regards
GregGHi,
Do you need this?
select count(*)
,id
,ratio_to_report(count(*)) over () *100 as pct
,sum(count(*)) over()
from t
group by id; -
Count(*) returns 'Column 'Count' not found' SQLException
I am trying to get the count from a table using the following:
String query = "SELECT COUNT(*) FROM myTable;"
I know that the connection is open and working because I am getting back an SQLException:
'Column 'Count' not found' .
The same command works fine at the command line. I would be grateful for any suggestions as to why JDBC sends my app searching for a field named count in my table whereas the command line returns the expected number.
Thanks.Found the problem, which is mainly my own stupidity . Is it one of Murphy's laws that the quickest way to find the solution to a problem if first embarrass yourself by asking foolish questions? Sorry to have bothered anyone.
-
Please dear sirs, I want to count the lov columns, and also i want to get the column title of the lov, and i use forms 6i , i know that there is "UTL_LOV.Get_LOV_Column_Property" but i can not use this package because i use forms 6i please please help me
One solution I have encountered to get around this limitation is to LOOP from 1 .. X (where X is the max amount of LOV columns you would ever expect a LOV to have, like 10 for example).
And make a call to SET_LOV_COLUMN_PROPERTY, and then check FORM_SUCCESS immediately afterwards, If FORM_SUCCESS=TRUE, then you know the set worked, so you know that LOV column must exist.
Not ideal by any means, but better than nothing.
It works for most forms, alas for one form it is not working and I cannot figure out why (it throws FRM-41364, which I cannot catch). -
Problem counting dinamic column!
Hi,
I am trying to make something like a statistic of a table.
i have a table :
table(M) {
column(priority) data = hight ,medium, low
column(departement) = departement A, departement B, departement C
column(id_message)} = 1, 2 ,3 ..............
i wanted to creat a report output like this:
------------------------Hight---------Medium--------Low---------- Total
departement A ------1---------------2----------------2--------------5
departement B-------3---------------3----------------1--------------7
departement C-------2---------------2----------------2--------------6
with this code i obtain this output, but it is a static solution, that only works for 3 types of priorities and i nedd a dinamic solution
select department,
count(decode(priority,'high',id_message,null)) high,
count(decode(priority,'medium',id_message,null)) medium,
count(decode(priority,'low',id_message,null)) low,
count(id_message) Total
from m
group by department
order by department
can i make something like this:
create or replace function statistic_dep_prior
return clob
is
declare
i number;
j number;
begin
for i in 0..dp_id_departament loop
for j in 0..id_priority loop
count(id_ticket) departement,
end loop;
end loop;
count(id_ticket) Total
from V_TICKET
group by dp_id_departament
order by dp_id_departament
return('
<table>
<tr>
<td>Departament</td>
<td>Hight</td>
</tr>
<tr>
<td>Dep. A</td>
<td>10</td>
<td>20</td>
</tr>
</table>
end;
Thank´s
Pepe
Message was edited by:
PepeWhat you appear to be trying to do likely will work far better using a pipelined table function.
Try this:
EXPLAIN PLAN FOR
SELECT table_name
FROM user_tables;
SELECT * FROM TABLE(dbms_xplan.display);
[pre]
If that kind of control over formatting an output looks good then check out the demos here:
http://www.psoug.org/reference/pipelined.html
asktom.oracle.com and the docs at http://tahiti.oracle.com are also great resources for learning about them. -
Returning 2 counts for columns contents in X/Y axis
I have a table (Workorders) from which I need to display information about 2 columns. Let's say one column is workorder.status and the other one is workorder.worktype. There are about 30 different workorder status and 30 different workorder worktypes. I need to write a query that will return counts for all workorder types for every workorder status (effectively returning, for example, workorder status count on a y axis and worktype count on an x axis.
Example:
Workorder
worktype New Installation Repair Reroute . . . .. . . . . .
Workorder
Status
Requested 15 12 3
Approved 20 2 96
In Progress 25 8 12
I thought of doing a stored procedure so i could take advantage of cursors and make this a little easier. However, i'd like to stay away from this because it will make the process more complex on my reporting tool (Actuate.)
Does anyone have any suggestions for doing this?Do You want just
SELECT status, worktypes, count(*)
FROM workorders
GROUP BY status, worktypes;and formatting in your reporting tool or formatting by sql:
SELECT status
, sum(decode(worktypes,'New', 1, null)) New
, sum(decode(worktypes,'Installation', 1, null)) Installation
, sum(decode(worktypes,'Repair', 1, null)) Repair
, sum(decode(worktypes,'Reroute', 1, null)) Reroute
FROM workorders
GROUP BY status;?
Maybe you are looking for
-
I have 3 apple ids and I want to merge them into one account.
I have 3 apple id's and I want to merge them into one account. Any suggestions?
-
Agents in not ready state too long drops calls.
Using CRS 4.0(3) with agent desktop. Issue is that when agents go to lunch they go into the not-ready state until they come back from lunch. Upon coming back from lunch, they proceed to go ready and then they start dropping calls as they come into th
-
Here's my disappointing story: I bought one of the 24" iMacs the day it came out. About 10 days later I received it and one of the first things I did was install windows XP. I followed the bootcamp/xp installation procedure exactly but once through t
-
Bluetooth System Preference Gone / Can't Find Keyboard
i booted up my mac pro today and it couldn't find my wireless keyboard. I went to System Preferences and clicked on bluetooth and got the spinning beachball. Restarted and now my bluetooth preference is gone. Changed the batteries on the keyboard and
-
Active content updater not working
hello, i am having alot of trouble getting the active content updater for my flash to work, i have followed all directions and i still have to click to activate the content? i am using Flash 8 professional. Any help please?