CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Grid with SQL Data Source and Records Count (CountSQL)

Print topic Send  topic

Author Message
datadoit
Posted: 08/20/2008, 9:46 AM

CCS 3.2; MySQL5; PHP5

I have a grid with a Data Source of SQL, and here's the SQL:

SELECT clid, calldate, FROM_UNIXTIME(UNIX_TIMESTAMP(calldate) -
UNIX_TIMESTAMP(calldate) % 240) AS slice
FROM cdr
GROUP BY clid, slice
ORDER BY calldate desc

What this does is group similar records within a certain time period
(couple of minutes). This SQL works perfectly getting the records sought.

However, when the grid is displayed, it shows grid error:

Database Error: Unknown column 'slice' in 'group statement'

This is because CodeCharge doesn't know how to calculate the CountSQL.
So, in the grid's Before Execute Select event, I add:

$Container->ds->CountSQL = "SELECT clid, calldate,
FROM_UNIXTIME(UNIX_TIMESTAMP(calldate) - UNIX_TIMESTAMP(calldate) % 240)
AS slice FROM cdr GROUP BY clid, slice";

Which just produces some seemingly meaningless number that doesn't match
the actual number of records that were pulled and display in the grid.

How do I get the CountSQL right? or better yet, why does CodeCharge pull
up the correct records, but can't get the count of those right?
wkempees


Posts: 1679
Posted: 08/20/2008, 10:11 AM

Quick reaction on my way out:
  
$Container->ds->CountSQL = "SELECT Count(*)  
FROM cdr   
  
add the COUNT()
loose the group by
create a WHERE clause that comes close to your obtained result

Walter


_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
datadoit
Posted: 08/20/2008, 12:54 PM

Well I've concluded (right or wrong) that it's impossible to get the
result set I need without grouping. Even trying a subselect with the
grouping doesn't pass syntax.

I can get the number of records from the SQL with no problems. Even in
Before Execute Select I can:

$sql = "SELECT clid, calldate, FROM_UNIXTIME(UNIX_TIMESTAMP(calldate) -
UNIX_TIMESTAMP(calldate) % 240) AS slice FROM cdr GROUP BY clid, slice"
$db = new clsDBPBXware();
$Records = mysql_num_rows($db->query($SQL));
$db->close();

and it gives me the correct number of records. Now if I try:

$Container->ds->CountSQL = $Records;

it won't let me. Can I at least 'force' the record count into CountSQL
in some way?
wkempees


Posts: 1679
Posted: 08/21/2008, 5:22 AM

I have tried a few (obvious) solutions but cannot validate the resutl against your tables.
Last question first:
Quote :
Can I at least 'force' the record count into CountSQL
in some way?
- create a small table dd_count, ( ID varchar , ROWCNTD integer)

  
Before Execute Select:  
  
$sql = "SELECT clid, calldate, FROM_UNIXTIME(UNIX_TIMESTAMP(calldate) -   
UNIX_TIMESTAMP(calldate) % 240) AS slice FROM cdr GROUP BY clid, slice"  
  
$db = new clsDBPBXware();  
  
$Records = mysql_num_rows($db->query($SQL));  
  
$SQL = "REPLACE into dd_record set ID='slicecount', ROWCNTD=". $db->ToSQL($Records, ccsInteger);  
$db-execute($SQL);  
$db->close();  

Now you can do the
$Container->ds->CountSQL = 'Select ROWCNTD from dd_record where ID = "slicecount" ';
in BeforeExecuteSelect


Walter
_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
wkempees


Posts: 1679
Posted: 08/21/2008, 5:30 AM

The above, shorthand version, without dd_count table:

Before Execute Select:
    
$sql = "SELECT clid, calldate, FROM_UNIXTIME(UNIX_TIMESTAMP(calldate) -     
UNIX_TIMESTAMP(calldate) % 240) AS slice FROM cdr GROUP BY clid, slice"    
$db = new clsDBPBXware();    
$Records = mysql_num_rows($db->query($SQL));    
$db->close();    
  
$Container->ds-CountSQL='Select ' . $Records ;   
Walter
Keep me posted.



_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
datadoit
Posted: 08/21/2008, 7:33 AM

BRILLIANT!!

Here's the final code in BeforeExecuteSelect:

<code>
//Remove the order by - not needed for the count anyways.
$SQL = rtrim($Container->ds->SQL, "{SQL_OrderBy}");

//Do a separate query to get the row count only.
$db = new clsDBPBXware();
$Records = mysql_num_rows($db->query($SQL));
$db->close();

//Force in a new row count.
$Container->ds->CountSQL = "SELECT " . $Records;
</code>

I already had the original SQL in $Container->ds->SQL, just had to strip
out the OrderBy clause - not needed to get a row count.

The 'trick' in all of this is the "SELECT" in the CountSQL.

*****NOTE: Anytime you have a grid that will have a Data Source of SQL
and a GROUP BY clause in it, you'll have to do the above so that
CodeCharge can give you an accurate row count, and subsequently an
accurate Navigator. *****

Thanks Walter! I'll now pull for The Netherlands to win a medal in
something. :) I'm diggin' the women's handball team.


wkempees


Posts: 1679
Posted: 08/21/2008, 10:04 AM

Thanks.
As to the medals, we did some good ones.
Women's handball nice, but check out the hockey girls!

'Till next challenge!

Walter


_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
datadoit
Posted: 08/21/2008, 11:03 AM

Just whooped the US in women's water polo!
wkempees


Posts: 1679
Posted: 08/21/2008, 1:26 PM

8-)
How about this one:
Male 10K Swim marathon.
First time event, Dutch Gold!
Check out the guys history.........
http://www.maartenvanderweijden.com/
Respect!

_________________
Origin: NL, T:GMT+1 (Forumtime +9)
CCS3/4.01.006 PhP, MySQL .Net/InMotion(Vista/XP, XAMPP)

if you liked this info PAYPAL me: http://donate.consultair.eu
View profile  Send private message
laneoc

Posts: 154
Posted: 02/03/2009, 8:15 PM

Another approach...

$oldSQL=$YOUR_DATA_SOURCE_NAME->DataSource->SQL;
$myCountSQL="select count(*) from (".$oldSQL.") as CountTable";
$YOUR_DATA_SOURCE_NAME->DataSource->CountSQL=$myCountSQL;


Lane
_________________
Lane
View profile  Send private message
jjrjr1


Posts: 942
Posted: 02/04/2009, 5:59 AM

Hi

Another thought..

Use Dlookup action with the value looked up being the count.

This helps make more portable code than using custom code.

Have fun

_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.