Count of rows from different tables.
Hi Friends,
I have 4 tables with a common column name "ID". I would like to get the count of rows from all the four tables which has the same ID.
Ex
select count(a.id) from table1 a,table2 b,table3 c,table4 d where a.id=b.id=c.id=d.id=5;
please suggest me some solution
may be thsi?
select count(a.id) from table1 a,table2 b,table3 c,table4 d
where a.id=b.id and a.id=c.id and a.id=d.id and a.id=5;
Similar Messages
-
Count of rows from different Columns
SELECT (SELECT COUNT (empno) empno
FROM emp) empno, (SELECT COUNT (mgr) mgr
FROM emp) mgr, (SELECT COUNT (sal) sal
FROM emp
WHERE sal > 2000) sal
FROM DUAL;Hi friends
Please let me know any better solutions for this query..??user10594152 wrote:
Please let me know any better solutions for this query..??Why not just..
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL> ed
Wrote file afiedt.buf
1 select count(empno) as emps
2 , count(mgr) as mgrs
3 , sum(case when sal > 2000 then 1 else 0 end) as sals
4* from emp
SQL> /
EMPS MGRS SALS
14 13 6
SQL> -
Count rows from several tables
hello,
im trying to count row from multiple tables
for example i need the select statement to produce the following
table_name count
table1 5
table2 6
table3 3
i came up with the following script but it counts the number of tables i have
select object_name, (select count(*) from user_tables where table_name = object_name) from all_objects
where object_type = 'TABLE'Manik wrote:
May be possible:
Check this:
SELECT table_name,
TO_NUMBER (
EXTRACTVALUE (
xmltype (
DBMS_XMLGEN.getxml ('select count(*) c from ' || table_name)),
'/ROWSET/ROW/C'))
COUNT
FROM (select * from all_tables where table_name in ('TABLE1','TABLE2'))
WHERE owner = 'SCOTT';Cheers,
Manik.Awesome Manik... Just too good. Thanks.
I wish i could have given you the 'Correct' points. ;-)
Can you please explain the logic in brief? Will be helpful for everybody to understand... -
How to compare two rows from two table with different data
how to compare two rows from two table with different data
e.g.
Table 1
ID DESC
1 aaa
2 bbb
3 ccc
Table 2
ID DESC
1 aaa
2 xxx
3 ccc
Result
2Create
table tab1(ID
int ,DE char(10))
Create
table tab2(ID
int ,DE char(10))
Insert
into tab1 Values
(1,'aaa')
Insert
into tab1 Values
(2,'bbb')
Insert
into tab1 Values(3,'ccc')
Insert
into tab1 Values(4,'dfe')
Insert
into tab2 Values
(1,'aaa')
Insert
into tab2 Values
(2,'xx')
Insert
into tab2 Values(3,'ccc')
Insert
into tab2 Values(6,'wdr')
SELECT
tab1.ID,tab2.ID
As T2 from tab1
FULL
join tab2 on tab1.ID
= tab2.ID
WHERE
BINARY_CHECKSUM(tab1.ID,tab1.DE)
<> BINARY_CHECKSUM(tab2.ID,tab2.DE)
OR tab1.ID
IS NULL
OR
tab2.ID IS
NULL
ID column considered as a primary Key
Apart from different record,Above query populate missing record in both tables.
Result Set
ID ID
2 2
4 NULL
NULL 6
ganeshk -
How to display multiple data from different table in one table? please help
Hi
I got sun java studio creator 2(the separate installation not the one in the net beans)....
My question is about displaying data that have been taken from the database.... I know how to display data in a table(just click on the table "bind data" )... but my question is that:
when i want to use a sql statement that taken the data from different table...
how can i display that data in the table(that will be shown in the web) ??? when i click bind data on the table i can only select one table i can't select more than one....
Note:
1) i'm using the rowset for displaying the data in the table, since the sql statement is depending on a condition(i.e. select a from b where c= ? )...
2) i mean by different table is that( i.e. select a from table1,table2 )..
thanks in advance...Hi,
937440 wrote:
Hi every one, this is my first post in this portal. Welcome to the forum!
Be sure to read the forum FAQ {message:id=9360002}
I want display the details of emp table.. for that I am using this SQL statement.
select * from emp where mgr=nvl(:mgr,mgr);
when I give the input as 7698 it is displaying the corresponding records... and also when I won't give any input then it is displaying all the records except the mgr with null values.
1)I want to display all the records when I won't give any input including nulls
2)I want to display all the records who's mgr is null
Is there any way to incorporate to include all these in a single query..It's a little unclear what you're asking.
The following query always includes rows where mgr is NULL, and when the bind variable :mgr is NULL, it displays all rows:
SELECT *
FROM emp
WHERE LNNVL (mgr != :mgr)
;That is, when :mgr = 7698, it displays 6 rows, and when :mgr is NULL it displays 14 rows (assuming you're using the Oracle-supplied scott.emp table).
The following query includes rows where mgr is NULL only when the bind variable :mgr is NULL, in which case it displays all rows:
SELECT *
FROM emp
WHERE :mgr = mgr
OR :mgr IS NULL
;When :mgr = 7698, this displays 5 rows, and when :mgr is NULL it displays 14 rows.
The following query includes rows where mgr is NULL only when the bind variab;e :mgr is NULL, in which case it displays only the rows where mgr is NULL. That is, it treats NULL as a value:
SELECT *
FROM emp
WHERE DECODE ( mgr
, :mgr, 'OK'
) = 'OK'
;When :mgr = 7698, this displays 5 rows, and when :mgr is NULL, it displays 1 row. -
Deleting a row from parent table
Dear Guru's
I am having two table with parent - child relationship. My problem is when I am deleting a row from parent table the curresponding child row from child table also should be deleted.
My Primary table 'Employee, EMPID Primary key
Child table 'Privilage' inthis EMPID referencing the EMPID of Employee table
My need is when I am deleting a row from parent table the curresponding child row from child table also should be deleted
I issued the SQL query like,
delete from employee where empid='12345' cascade constraints;
Then it showing me error like,
ERROR at line 1:
ORA-00933: SQL command not properly ended
Please resolve my issue , Its Top urgent
Thanks & Cheers
AntonyChoosing How Foreign Keys Enforce Referential Integrity
Oracle Database allows different types of referential integrity actions to be enforced, as specified with the definition of a FOREIGN KEY constraint:
Prevent Delete or Update of Parent Key The default setting prevents the deletion or update of a parent key if there is a row in the child table that references the key. For example:
CREATE TABLE Emp_tab (
FOREIGN KEY (Deptno) REFERENCES Dept_tab);Delete Child Rows When Parent Key Deleted The ON DELETE CASCADE action allows parent key data that is referenced from the child table to be deleted, but not updated. When data in the parent key is deleted, all rows in the child table that depend on the deleted parent key values are also deleted. To specify this referential action, include the ON DELETE CASCADE option in the definition of the FOREIGN KEY constraint. For example:
CREATE TABLE Emp_tab (
FOREIGN KEY (Deptno) REFERENCES Dept_tab
ON DELETE CASCADE); Set Foreign Keys to Null When Parent Key Deleted The ON DELETE SET NULL action allows data that references the parent key to be deleted, but not updated. When referenced data in the parent key is deleted, all rows in the child table that depend on those parent key values have their foreign keys set to null. To specify this referential action, include the ON DELETE SET NULL option in the definition of the FOREIGN KEY constraint. For example:
CREATE TABLE Emp_tab (
FOREIGN KEY (Deptno) REFERENCES Dept_tab
ON DELETE SET NULL);
SQL> conn scott/tiger
Connected.
SQL> create table ppk ( no number primary key);
Table created.
SQL> begin for inn in 1..10 loop insert into ppk values (inn); end loop; end;
PL/SQL procedure successfully completed.
SQL> create table ffk ( no number references ppk(no));
Table created.
SQL> begin for inn in 1..10 loop insert into ffk values (inn); end loop; end;
PL/SQL procedure successfully completed.
SQL> drop table ppk cascade constraints;
Table dropped.Message was edited by:
user52
Message was edited by:
user52
Message was edited by:
user52 -
Deleting a row from a table containing CLOB as one of the columns
When i delete a row from a table which contains a CLOB (internal clob) i.e. CLOB or BLOB column, Will the CLOB data will also be deleted ? I understand that what exactly stored in the CLOB column is the clob locator which points to the actual data.
So, when I delete this row, the clob locator will be deleted, but will the actual data what this locator is pointing to is also deleted ??? if not what is the process to delete the data the locator is pointing to when the row containing the locator is deleted ? If this is not happening then the actual data might become an orphan data which nobody has access to, will automatic garbage cleaning occurs on a frequent intravels to delete unaddressed data residing on the database server ?
Thanks in advance for the help, can email me at [email protected] alternatively.
Regards,
Srinivasa C.Michael,
Thanks very much for your inputs, here are the results i got when i tried the way you explained in your answer, the TRUNCATE command made the actual size back to normal, but the delete is not the same, so, how can i delete the data that a particular clob locator may point to ?
truncate would delete all the rows of the table, which might not serve my purpose, i would like to delete a row and also it's associated clob data from the database! is there anyway to do this ?
is there any limitation on the ool_sample size? i am basically a c++ programmer, i am looking for some function like FREE which would free the allocated memory to the clob once the locator is deleted.
your help is greatly appreciated - Thanks!
:-) Srini.
==========================
My Results:
==========================
SQL> create table sample (
2 id integer primary key,
3 the_data CLOB default empty_clob() )
4 lob (the_data) store as ool_sample;
Table created.
SQL> select segment_name, round(sum(bytes)/1024, 2) || 'K' as sotrage_consumed
2 from user_segments
3 where segment_name in ('SAMPLE', 'OOL_SAMPLE')
4 group by segment_name;
SEGMENT_NAME
SOTRAGE_CONSUMED
OOL_SAMPLE
20K
SAMPLE
10K
SQL> select count(*) from sample;
COUNT(*)
0
SQL> begin
2 for i in 1..1000
3 loop
4 insert into sample values (i, RPAD('some data', 4000) );
5 end loop;
6 end;
7 /
PL/SQL procedure successfully completed.
SQL> select segment_name, round(sum(bytes)/1024, 2) || 'K' as sotrage_consumed
2 from user_segments
3 where segment_name in ('SAMPLE', 'OOL_SAMPLE')
4 group by segment_name;
SEGMENT_NAME
SOTRAGE_CONSUMED
OOL_SAMPLE
6420K
SAMPLE
70K
SQL> delete sample;
1000 rows deleted.
SQL> select segment_name, round(sum(bytes)/1024, 2) || 'K' as sotrage_consumed
2 from user_segments
3 where segment_name in ('SAMPLE', 'OOL_SAMPLE')
4 group by segment_name;
SEGMENT_NAME
SOTRAGE_CONSUMED
OOL_SAMPLE
6420K
SAMPLE
70K
SQL> commit;
Commit complete.
SQL> select segment_name, round(sum(bytes)/1024, 2) || 'K' as sotrage_consumed
2 from user_segments
3 where segment_name in ('SAMPLE', 'OOL_SAMPLE')
4 group by segment_name;
SEGMENT_NAME
SOTRAGE_CONSUMED
OOL_SAMPLE
6420K
SAMPLE
70K
SQL> begin
2 for i in 1..1000
3 loop
4 insert into sample values (i, rpad('some data', 4000));
5 end loop;
6 end;
7 /
PL/SQL procedure successfully completed.
SQL> select segment_name, round(sum(bytes)/1024, 2) || 'K' as sotrage_consumed
2 from user_segments
3 where segment_name in ('SAMPLE', 'OOL_SAMPLE')
4 group by segment_name;
SEGMENT_NAME
SOTRAGE_CONSUMED
OOL_SAMPLE
9616K
SAMPLE
70K
SQL> truncate table sample;
Table truncated.
SQL> select segment_name, round(sum(bytes)/1024, 2) || 'K' as sotrage_consumed
2 from user_segments
3 where segment_name in ('SAMPLE', 'OOL_SAMPLE')
4 group by segment_name;
SEGMENT_NAME
SOTRAGE_CONSUMED
OOL_SAMPLE
20K
SAMPLE
10K -
How NOT to restrict no of rows from two tables
I have two identical tables Invoice and Payment. The only difference is Invoice_id,Invoice_Amt and Payment_id,Payment_Amt columns that shows different ids and amounts. The bank_ids, names, account_types are same. Invoice table has 3 rows and Payment has 2. Simply meaning that there were 3 invoices generated but the bank received 2 payments. I want to show Invoice_Amt and Payment_Amt using sql query. But its giving me total 6 rows. Whereas, I want 3 from Invoice and 2 rows from Payment table to show side-by-side.
CREATE TABLE invoice
( invoice_id NUMBER
bank_id NUMBER,
bank_name VARCHAR2(256),
invoice_amount NUMBER);
----Invoice table has 3 rows showing 3 Invoice Amts
CREATE TABLE payment
( payment_id NUMBER
bank_id NUMBER,
bank_name VARCHAR2(256),
payment_amount NUMBER);
----Payment table has 2 rows showing 2 Payments
After executing this sql statement below, I get 6 rows:
select inv.invoice_amount,pymt.payment_amount from invoice inv,payment pymt where inv.bank_id=pymt.bank_id;
How can I show 3 rows for Invoice and 2 for Payment..?
Thank you.Hi,
So you want
the 1st invoice to appear side-by-side with the 1st payment,
the 2nd invoice to appear side-by-side with the 2nd payment,
the nth invoice to appear side-by-side with the nth payment.
But, if there are an uneqaul numner of payments and invoices, all the payments and all the invoices must still be shown, alone on a row if necessary.
That sounds like a job for FULL OUTER JOIN.
Use the analytic ROW_NUMBER fucntion to determine which is the 1st, 2nd, ..., nth row in each table, for each bank.
WITH invoice_plus_r_num AS
SELECT bank_id, bank_name
, invoice_amount
, ROW_NUMBER () OVER ( PARTITION BY bank_id, bank_name
ORDER BY invoice_id
) AS r_num
FROM invoice
, payment_plus_r_num AS
SELECT bank_id, bank_name
, payment_amount
, ROW_NUMBER () OVER ( PARTITION BY bank_id, bank_name
ORDER BY payment_id
) AS r_num
FROM payment
SELECT NVL (i.bank_id, p.bank_id) AS bank_id
, NVL (i.bank_name, p.bank_name) AS bank_name
, i.invoice_amount
, p.payment_amount
FROM invoice_plus_r_num i
FULL OUTER JOIN payment_plus_r_num p ON i.bank_id = p.bank_id
AND i.bank_name = p.bank_name
AND i.r_num = p.r_num
ORDER BY bank_id -- you can use column aliases here
, bank_name
, NVL (i.r_num, p.r_num)
;You mentioned something about accounts, but didn't include that in the CREATE TABLE statements. You'll probably want to add that wherever I used bank_id and bank_name, above.
Are invoce and payment actually views, rather than tables? If not, you should have a separate bank table, and only include the bank_id in the other tables.
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.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
Edited by: Frank Kulash on Feb 3, 2011 12:32 PM -
Select unique rows from two tables...
Hi,
I have two tables, replies1 and replies2.
SQL> desc replies
Name Null? Type
URN VARCHAR2(36)
ADDRESS VARCHAR2(18)
FILESIZE NUMBER
AS_NUM VARCHAR2(6)
SQL> desc replies2
Name Null? Type
URN VARCHAR2(36)
ADDRESS VARCHAR2(18)
AS_NUM VARCHAR2(6)
Both of the tables have no primary keys, but I have indixes on (urn, addrss) combination on both....
I am trying to select the unique rows with (urn, address) from replies2, and then find the matching size from replies...
I am using the following query:
select distinct replies2.urn, replies2.address, replies.filesize from replies2, replies where replies2.AS_NUM like 'XYZ' and replies.urn = replies2.urn;
I cannot figure out why it won't work. the way I understand it is that, distinct will give all distinct combination of all column names that follow, which is what I want...
I know it is wrong, because the query:
select count(*) from replies2 where AS_NUM like 'XYZ' returns less number of rows than the above query.
Any help would be greatly appreciated.
Thank you
Oz.Thanks a lot Mohan for your reply.
urn is not a unique key. Several rows could have the same (urn, address) pair in both tables. What I want is retrieve all (urn, address) rows from one table, and find the size from the other table to make a (urn, address, size). I want all unique combinations of (urn, address) to appear in the output.
AS_NUM is an empty column in replies... It would've been a lot easier if it wasn't, since then I'll just say: select distinct urn, address, filesize from replies where AS_NUM like 'XYZ';
I will try your query though and let u know how it goes. It takes quite a while to run since my tables are huge. -
The biggest count of rows of sys tables
Hii ??
I want to learn how can I find the biggest count of rows of sys tables or
which table has around 900000 records on sys user database oracle 10g??Hello;
What purpose would knowing the row count in tables owned by SYS serve ? The query below will give you a fair idea of row counts in tables owned by SYS assuming that statistics have been recently gathered for these tablesselect table_name,num_rows from dba_tables where owner='SYS';Varad -
Report using Data from different tables
Hello,
I am trying to convert a Cobol batch program to Oracle 6i tabular report.
The data is fetched from many different tables and there are lots of processing(i.e, based on the value of a column from one table need additional processing from different tables) required to generate the desired columns in the final report.
I would like to know what is the best strategy to follow in Oracle Reports 6i. I heard that CREATE GLOBAL TEMPORARY TABLE is an option. ( or REF CURSOR ?) I do not know much about its usage. Can somebody guide me about this or any other better way to achieve the result.
Thank you in advance
PriyaHello,
There are many, many options available to you, each of which has advantages and disadvantages. This is why it is difficult to answer "what is best?" without alot more details about your specific circumstances.
In general, you're going to be writing PL/SQL to do any conditional logic that cannot be expressed as pure SQL. It can executed in the database, or it can executed within Reports itself. And most reports developers do some of both.
As a general rule, you want to send only the data you need from the database to the report. This means you want to do as much filtering and aggregating of the data as is readily possible within the database. If this cannot be expressed as plain SQL queries, then you'll want to create a stored procedures to help do this work.
Generally, the PL/SQL you create for executing within the report should be focused on control of the formatting, such as controlling whether a field is visible, or controlling display attributes for conditional formatting.
But these are not hard and fast rules. In some cases, it is difficult to get all the stored procedures you might like installed into the database. Perhaps the dba is reluctant to let you install that many stored procedures. Perhaps there are restrictions when and how often updates can be made to stored procedures in a production database, which makes it difficult to incrementally adjust your reports based on user feedback. Or perhaps there are restrictions for how long queries are allowed to run.
So, Reports offers lots of options and features to let you do data manipulation operations from within the report data model.
In any case, Oracle does offer temporary table capabilities. You can populate a temp table by running stored procedures that do queries, calculations and aggregations. And you can define and initiate a dynamic query statement within the database and pass a handle to this query off to the report to execute (ref cursor).
From the reports side, you can have as many queries as you want in the data model, arranged in any hierarchy via links. You can parameterize and change the queries dynamically using bind variables and lexicals. And you can add calculations, aggregations, and filters.
Again, most people do data manipulation both in the database and in Reports, using the database for what it excels at, and Reports for what it excels at.
Hope this helps.
Regards,
The Oracle Reports Team --skw -
How to get only column names from different tables as single table columns
Hi All,
I have one requirement in which we want only column names from different tables.
for example :
I have three tables T1 ,T2, T3 having
col1 clo2 clo3 --> T1 ,
col3 col5 ,clo6 --> T2 ,
Clo6 col8 col9 --> T3
columns i want to get only all Column names from all table as single Resultset not any data from that how can i get that empty resultset
because this empty result i want to bind in datagridview(front end) as Empty resultset
Please tell me anyways to do this
Niraj SevalkarIf I understand you want an empty result set, just with metadata. SET FMTONLY do the trick:
SET FMTONLY ON
SELECT Col1, Col2, Col3, ....., Coln
FROM
T1 CROSS JOIN T2 CROSS JOIN T3
SET FMTONLY OFF
Another alternative is to include an imposible contition
SELECT Col1, Col2, Col3, ....., Coln
FROM
T1 CROSS JOIN T2 CROSS JOIN T3
WHERE 1 = 0
If you are using a SqlDataAdapter in your client application. You can use the FillSchema method. the select command may be any select statement that returns the columns you want. Under the covers FillSchema will call SET FMTONLY ON.
If you are using SqlCommand.ExecuteReader you can pass SchemaOnly to CommandBehavior argument. SET FMTONLY ON is called under the covers. Again the select command may be any select statement that returns the columns you want.
"No darás tropezón ni desatino que no te haga adelantar camino" Bernardo Balbuena -
Deleting Rows From A Table After Running a Report
I've created a report that inserts rows into a table on the asp page then calls a BI Publisher report to display the data. After running the report in BI Publisher I need to delete the rows from the table that had just been inserted. How is the best way to go about this in BI Publisher?
The best way I know of would be to use an "afterReport trigger".
An afterReport trigger fires after the XML output has been generated.
Thanks,
Bipuser -
COuld anyone tell me how to delete rows from a table which has millions of rows.
TIA,
Oracle userif you are deleting all the rows, use "truncate table" in sql*plus.
or if you are deleting all but a handful of rows, then copy the rows you still want to a spare table, drop the original table, and rename the spare table back to the original table's name.
hope this helps -
Copying table rows from one table to another table form
Hi
I have a problem about Copying table rows from one table to another table form.On jsf pages if you enter command button go anather jsf page and it copy one row to another table row. But when i execute this process for table FORM it doesn't copy I wrote a code under "createRowFromResultSet - overridden for custom java data source support." Code block is:
ViewRowImpl value = super.createRowFromResultSet(qc, resultSet);
try{
AdfFacesContext fct = AdfFacesContext.getCurrentInstance();
Number abc = (Number)fct.getProcessScope().get("___");
value.setAttribute("___",abc);
}catch(Exception ex){System.out.println(ex); }
return value;Table may be copied with the
expdp and impdp utilities.
http://www.oracle.com/technology/products/database/utilities/index.html
Maybe you are looking for
-
Problem with a Dynpro field (type numc)
hi everybody. I'm developping a ModulPool application in wich i have 2 RadioButtons with 2 textbox fields. What i pretend to do is, when the user clicks a radiobutton and strikes Intro, enable the corresponding textbox field and disable the other one
-
Acrobat 9 Distiller Profile in Acrobat X Distiller
Hi All, We have received a distiller profile which was created using Acrobat 9 and likely to upgrade distiller server X. Will acrobat 9 distiller profile combatible for X and will there be any issue? Regards, Selvakumar
-
Hi, Situation: An external system sends a request to the ESB that I am going to create. This ESB transforms the incoming request to a different format and then sends the transformed request to the BPEL (need to create). The BPEL invokes an external s
-
Using postgres on OS X Mavericks
After upgrading from OS X Lion Server to OS X Mavericks Server I am having issues with Postgres. First I have a web2py application from which I connected to the Postgres database using this connection string: db = DAL('postgres://username:password@lo
-
Troubles with build qt app to non-standard location
Hello, i need to compile app (Choqok) to $HOME/somewhere I use cmake -DCMAKE_INSTALL_PREFIX=$HOME/kde4/ ../ , make, make install. But, app is broken ( http://twio.cz/ac9t ), some libraries probably missing. I was try set ld.so.conf to ~/kde4/libs and