If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Informix > Get the rows of a sql query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-05-07, 10:07
soujiro soujiro is offline
Registered User
 
Join Date: Sep 2007
Posts: 4
Get the rows of a sql query

I am developing a component that makes a list with pagination, in order to do that I have an sql generator.

I need to get the number of rows that the sql query will produce, but I need to count by several columns:

SELECT COUNT(A.id, B.id, ...)
FROM table1 as A, table2 as B
WHERE .....

I am using Informix 10, then this behavior is not supported, I think that I can use a subquery:

SELECT COUNT(*)
FROM (SELECT COUNT(A.id, B.id, ...)
FROM table1 as A, table2 as B
WHERE .....) as D

But is unsupported too in Informix 10 (in Mysql the two approaches are supported)

Some ideas?
Reply With Quote
  #2 (permalink)  
Old 09-06-07, 05:25
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Code:
SELECT count(*) FROM table_a a, table_b b WHERE a.pkey = b.fkey ...
With a regular join the count function returns the number of rows that satisfy the conditions. But this is very straightforward, can't imagine MySql needs an alternate construction for that, do you need a count of rows involved in an outer join perhaps? Then you need to use something like:
Code:
SELECT count(a.pkey), count(b.pkey) FROM table_a a, OUTER table_b b 
WHERE a.pkey = b.fkey ...
Regards
Reply With Quote
  #3 (permalink)  
Old 09-06-07, 11:47
soujiro soujiro is offline
Registered User
 
Join Date: Sep 2007
Posts: 4
Smile I have found a solution

Tyveleyn, some times a DISTINCT clause can filter the results (the sql generator generates DISCTINT if the user ask for it), this is the reason I need to count by several columns, I have found a solution using multiset:

select count(*) as rownum
from table(multiset(SELECT DISTINCT.....)) s1;

Then I can get the number of rows of any sql query using a multiset, or I am wrong, there should be any restriction in make a subquery for a multiset?

Last edited by soujiro; 09-06-07 at 11:50.
Reply With Quote
  #4 (permalink)  
Old 09-18-07, 12:33
soujiro soujiro is offline
Registered User
 
Join Date: Sep 2007
Posts: 4
Unhappy The solution is very slow

As I wrote, I have found a solution for my problem, this is using TABLE and MULTISET:

SELECT COUNT(*)
FROM TABLE(MULTISET(SELECT DISTINCT a,b,c .....))

But this solution is very slow, the subquery is not a very optimal approach, some ideas to improve the solution?

Regards
Reply With Quote
  #5 (permalink)  
Old 09-18-07, 18:07
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
I don't know if the MULTISET datatype is appropriate in your case. It's a datatype for a collection column, that stores multiple values in one attribute and makes the table a kind of multidimensional.
What else you could do is perform your task with subqueries, like:
Code:
SELECT DISTINCT a.some_column,
	(SELECT count(*) FROM table1 b
	 WHERE b.some_column = a.some_column),
	(SELECT count(*) FROM table2 c
	 WHERE c.foreign_key = a.primary_key)
FROM table1 a
In this case the count of the rows in the subqueries are independent from the number of results in the main query.

Regards
Reply With Quote
  #6 (permalink)  
Old 10-11-07, 10:14
soujiro soujiro is offline
Registered User
 
Join Date: Sep 2007
Posts: 4
The problem

I think that I have not explained successfully the problem, I need to get the rows of a generated query, e.g. the query may be:

select DISTINCT companyid, employeeid from report

Then I need to get the rows generated by this query, a query like:

select COUNT(DISTINCT companyid, employeeid) from report

But this donīt works in Informix 10, in mysql this executes without problems.

I have found a solution with MULTISET:

select COUNT(*)
from TABLE(MULTISET(select DISTINCT companyid, employeeid from report))

But this query is really slow. Some ideas?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On