Home » SQL & PL/SQL » SQL & PL/SQL » how to return a row based on count using multiple tables (SQL Query)
how to return a row based on count using multiple tables [message #633869] Thu, 26 February 2015 10:31 Go to next message
suji6281
Messages: 151
Registered: September 2014
Senior Member
Hello All,

Please help us to write a query to return 'X' with the following criteria.
1. count(*) from PS_VENDOR_COST_TBL table should be less than LINE_NBR_LIMIT from TBL1
2. count(*) from PS_VENDOR_COST_TBL1 table should be greater than LINE_NBR_LIMIT from TBL2

EXAMPLE:
SELECT 'X' FROM PS_VENDOR_COST_TBL, PS_VENDOR_COST_TBL1 WHERE COUNT(*) < TBL1.LINE_NBR_LIMIT AND COUNT(*) > TBL2.LINE_NBR_LIMIT

Create a table.


SQL> CREATE TABLE TBL1 (STRING_ID VARCHAR2(11) NOT NULL, 
  2  LINE_NBR_LIMIT INTEGER  );

Table created

SQL> CREATE TABLE TBL2 (STRING_ID VARCHAR2(11) NOT NULL, 
  2  LINE_NBR_LIMIT INTEGER  );

Table created

SQL> CREATE TABLE PS_VENDOR_COST_TBL (EMPLID VARCHAR2(11) NOT NULL, 
  2  LINE_NBR INTEGER , STATUS VARCHAR2(1) NOT NULL, AMOUNT_COL INTEGER );

Table created

SQL> CREATE TABLE PS_VENDOR_COST_TBL1 (EMPLID VARCHAR2(11) NOT NULL, 
  2  LINE_NBR INTEGER , STATUS VARCHAR2(1) NOT NULL, AMOUNT_COL INTEGER );

Table created



Insert rows into table.


SQL> INSERT INTO TBL1  VALUES ('LN_LIMIT', 1);

1 row created.

SQL> INSERT INTO TBL1  VALUES ('SH_LIMIT', 4);

1 row created.

SQL> INSERT INTO PS_VENDOR_COST_TBL  VALUES ('12345', 1, 'A', 100);

1 row created.

SQL> INSERT INTO PS_VENDOR_COST_TBL  VALUES ('12345', 2, 'A', 200);

1 row created.

SQL> INSERT INTO PS_VENDOR_COST_TBL1  VALUES ('12345', 1, 'A', 100);

1 row created.

SQL> INSERT INTO PS_VENDOR_COST_TBL1  VALUES ('12345', 2, 'A', 200);

1 row created.

SQL> INSERT INTO PS_VENDOR_COST_TBL1  VALUES ('12345', 3, 'A', 300);

1 row created.





Thank You!

Re: how to return a row based on count using multiple tables [message #633870 is a reply to message #633869] Thu, 26 February 2015 10:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Create a test case, test the test case but post only the test case code NOT its execution.
We cannot copy and paste the execution to test our solution.
For your understanding:
SQL> SQL> CREATE TABLE TBL1 (STRING_ID VARCHAR2(11) NOT NULL,
SP2-0734: unknown command beginning "SQL> CREAT..." - rest of line ignored.
SQL>   2  LINE_NBR_LIMIT INTEGER  );
SQL>
SQL> Table created
SP2-0734: unknown command beginning "Table crea..." - rest of line ignored.
SQL>
SQL> SQL> CREATE TABLE TBL2 (STRING_ID VARCHAR2(11) NOT NULL,
SP2-0734: unknown command beginning "SQL> CREAT..." - rest of line ignored.
SQL>   2  LINE_NBR_LIMIT INTEGER  );
SQL>
SQL> Table created
SP2-0734: unknown command beginning "Table crea..." - rest of line ignored.
SP2-0044: For a list of known commands enter HELP
and to leave enter EXIT.
SQL>
SQL> SQL> CREATE TABLE PS_VENDOR_COST_TBL (EMPLID VARCHAR2(11) NOT NULL,
SP2-0734: unknown command beginning "SQL> CREAT..." - rest of line ignored.
SQL>   2  LINE_NBR INTEGER , STATUS VARCHAR2(1) NOT NULL, AMOUNT_COL INTEGER );
SQL>
SQL> Table created
SP2-0734: unknown command beginning "Table crea..." - rest of line ignored.
SQL>
SQL> SQL> CREATE TABLE PS_VENDOR_COST_TBL1 (EMPLID VARCHAR2(11) NOT NULL,
SP2-0734: unknown command beginning "SQL> CREAT..." - rest of line ignored.
SQL>   2  LINE_NBR INTEGER , STATUS VARCHAR2(1) NOT NULL, AMOUNT_COL INTEGER );
SQL>
SQL> Table created
SP2-0734: unknown command beginning "Table crea..." - rest of line ignored.
SP2-0044: For a list of known commands enter HELP
and to leave enter EXIT.
SQL>

[Updated on: Thu, 26 February 2015 10:37]

Report message to a moderator

Re: how to return a row based on count using multiple tables [message #633871 is a reply to message #633869] Thu, 26 February 2015 10:37 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
1. count(*) from PS_VENDOR_COST_TBL table should be less than LINE_NBR_LIMIT from TBL1
2. count(*) from PS_VENDOR_COST_TBL1 table should be greater than LINE_NBR_LIMIT from TBL2


What "should" means here?
You want to check if it is verified or you want to ensure that at any time it is true?
In the former, what should be the result if it is OK and if it is not OK?

Re: how to return a row based on count using multiple tables [message #633872 is a reply to message #633871] Thu, 26 February 2015 10:49 Go to previous messageGo to next message
suji6281
Messages: 151
Registered: September 2014
Senior Member
Hi Michel,

COUNT (*) means total number of rows of PS_VENDOR_COST_TBL.

Thank You
Re: how to return a row based on count using multiple tables [message #633873 is a reply to message #633872] Thu, 26 February 2015 10:51 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Yes I know what count(*) means, I want to know what "should" means.
I posted 3 questions, you answered none.

Re: how to return a row based on count using multiple tables [message #633874 is a reply to message #633873] Thu, 26 February 2015 11:06 Go to previous messageGo to next message
suji6281
Messages: 151
Registered: September 2014
Senior Member
Hi Michel,

I am looking for a sql query to return some value ('X') based on row count of main tables PS_VENDOR_COST_TBL and PS_VENDOR_COST_TBL1. And the count is should be from TBL1 and TBL2. Since I don't want hard-code the minimum and maximum numbers in the query.
If criteria matches then it should return 'X' else nothing.

Answers are: I want to verify the value and return some value ('X') if it is Ok.

Thank You!


Re: how to return a row based on count using multiple tables [message #633875 is a reply to message #633874] Thu, 26 February 2015 11:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

select 'X' from dual
where (select count(*) from PS_VENDOR_COST_TBL ) < (select LINE_NBR_LIMIT from TBL1)
  and (select count(*) from PS_VENDOR_COST_TBL1) > (select LINE_NBR_LIMIT from TBL2)
/
Re: how to return a row based on count using multiple tables [message #633877 is a reply to message #633875] Thu, 26 February 2015 11:29 Go to previous message
suji6281
Messages: 151
Registered: September 2014
Senior Member
Thank You Michel. I am looking for this query only.
Previous Topic: Using oracle object
Next Topic: need the out put based on max date
Goto Forum:
  


Current Time: Thu Aug 27 17:01:11 CDT 2026