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

 Report to display row even when record not available

Print topic Send  topic

Author Message
karen

Posts: 99
Posted: 08/31/2011, 6:13 PM

Hi all,

I have a table where users can enter amounts under their user id for the quarters selected, i.e., March, June, September or December of a year. I currently have a report on this which churns out the data like this:

User 1 | Mar 2010 | NNNN.NN | NNNN.NN | NNNN.NN
User 1 | Jun 2010 | NNNN.NN | NNNN.NN | NNNN.NN
User 1 | Sep 2010 |NNNN.NN | NNNN.NN | NNNN.NN
User 1 | Dec 2010 |NNNN.NN | NNNN.NN | NNNN.NN
User 2 | Jun 2010 | NNNN.NN | NNNN.NN | NNNN.NN
User 2 | Sep 2010| NNNN.NN | NNNN.NN | NNNN.NN

As you can see, User 2 did not enter any records for Mar or Dec 2010 hence no display for those records. However, the requirement is to display an empty row if no records are available like this:

User 1 | Mar 2010 | NNNN.NN | NNNN.NN | NNNN.NN
User 1 | Jun 2010 | NNNN.NN | NNNN.NN | NNNN.NN
User 1 | Sep 2010 |NNNN.NN | NNNN.NN | NNNN.NN
User 1 | Dec 2010 |NNNN.NN | NNNN.NN | NNNN.NN
User 2 | Mar 2010 | | |
User 2 | Jun 2010 | NNNN.NN | NNNN.NN | NNNN.NN
User 2 | Sep 2010| NNNN.NN | NNNN.NN | NNNN.NN
User 2 | Dec 2010| | |

I'm thinking of using a temporary table to store all the quarters and doing a left join on the actual table with the data. I think the temporary table may also have to contain all unique combinations of users and quarters.

I hope someone can advise how I can implement this code in CCS. Or if there's a better way of doing this. I hope so.

Thanks in advance for any advice.

Cheers,
Karen
CCS3 / PHP5 / MySQL5
View profile  Send private message
phpminer

Posts: 28
Posted: 08/31/2011, 9:54 PM

You shouldn't have to create any temporary tables.

Try a right outer-join, in that way the null prices can be picked up as actual columns.

Also, it may be of help to list you 'user' and 'price' table structures
in a follow-on reply.
View profile  Send private message
karen

Posts: 99
Posted: 09/01/2011, 12:16 AM

Thanks for your response, phpminer. Those rows without any values aren't actually records with null values, the records don't exist at all. Here are more details about the tables I already have.

-- Table structure for table `members_members`  
--  
  
CREATE TABLE IF NOT EXISTS `members_members` (  
  `members_id` int(11) unsigned NOT NULL auto_increment,  
  `members_firstname` varchar(128) NOT NULL default '',  
  `members_lastname` varchar(128) NOT NULL default '',  
  `members_assembly_id` int(11) NOT NULL default '0',  
  `members_emailadd` varchar(128) default NULL,  
  `members_spouse` varchar(128) default NULL,  
  `members_phone` varchar(128) default NULL,  
  `members_mobile` varchar(128) default NULL,  
  `members_address` varchar(128) default NULL,  
  `members_suburb` varchar(128) default NULL,  
  `members_state` varchar(128) default NULL,  
  `members_postcode` varchar(10) default NULL,  
  `members_rnet_leader` int(1) NOT NULL default '0',  
  PRIMARY KEY  (`members_id`)  
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=331 ;  
  
-- Table structure for table `fin_fbal`  
--  
  
CREATE TABLE IF NOT EXISTS `fin_fbal` (  
  `fbal_id` int(11) NOT NULL auto_increment,  
  `fbal_members_id` int(11) NOT NULL,  
  `fbal_date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,  
  `fbal_user_id` int(11) NOT NULL,  
  `fbal_month` int(2) NOT NULL,  
  `fbal_year` year(4) NOT NULL,  
  `fbal_open` decimal(15,2) NOT NULL,  
  `fbal_alloc` decimal(15,2) NOT NULL,  
  `fbal_addinc` decimal(15,2) NOT NULL,  
  `fbal_interest` decimal(15,2) NOT NULL,  
  `fbal_gstrefund` decimal(15,2) NOT NULL,  
  `fbal_repayments` decimal(15,2) NOT NULL,  
  `fbal_insure` decimal(15,2) NOT NULL,  
  `fbal_ratestax` decimal(15,2) NOT NULL,  
  `fbal_furniture` decimal(15,2) NOT NULL,  
  `fbal_fuel` decimal(15,2) NOT NULL,  
  `fbal_phone` decimal(15,2) NOT NULL,  
  `fbal_maint` decimal(15,2) NOT NULL,  
  `fbal_improve` decimal(15,2) NOT NULL,  
  `fbal_hosp` decimal(15,2) NOT NULL,  
  `fbal_books` decimal(15,2) NOT NULL,  
  `fbal_stationery` decimal(15,2) NOT NULL,  
  `fbal_clothing` decimal(15,2) NOT NULL,  
  `fbal_events` decimal(15,2) NOT NULL,  
  `fbal_gifts` decimal(15,2) NOT NULL,  
  `fbal_dev` decimal(15,2) NOT NULL,  
  `fbal_travel` decimal(15,2) NOT NULL,  
  `fbal_transfer` decimal(15,2) NOT NULL,  
  `fbal_carpurchase` decimal(15,2) NOT NULL,  
  `fbal_carinsure` decimal(15,2) NOT NULL,  
  `fbal_carmaint` decimal(15,2) NOT NULL,  
  `fbal_carfuel` decimal(15,2) NOT NULL,  
  `fbal_carreg` decimal(15,2) NOT NULL,  
  `fbal_driverslic` decimal(15,2) NOT NULL,  
  `fbal_medtreat` decimal(15,2) NOT NULL,  
  `fbal_medinsure` decimal(15,2) NOT NULL,  
  `fbal_bankfees` decimal(15,2) NOT NULL,  
  `fbal_schoolfees` decimal(15,2) NOT NULL,  
  `fbal_gstpaid` decimal(15,2) NOT NULL,  
  `fbal_drawings` decimal(15,2) NOT NULL,  
  `fbal_close` decimal(15,2) NOT NULL,  
  `fbal_taxdeducted` decimal(15,2) NOT NULL,  
  `fbal_alopen` decimal(15,2) NOT NULL,  
  `fbal_alaccrued` decimal(15,2) NOT NULL,  
  `fbal_altaken` decimal(15,2) NOT NULL,  
  `fbal_alclose` decimal(15,2) NOT NULL,  
  `fbal_alcommenced` datetime default NULL,  
  `fbal_alcompleted` datetime default NULL,  
  PRIMARY KEY  (`fbal_id`),  
  UNIQUE KEY `fbal_members_id` (`fbal_members_id`,`fbal_month`,`fbal_year`)  
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=147 ;  
  

This is what happens for the transactions. Every quarter, the users will enter records for their related member id. However, this is not mandatory so there will be members without records for select quarters.

I have a table with all the unique members which I can refer for all the fin_fbal records. But I don't have a table with all the quarters of the years. Therefore, if there are no records for a particular member for the quarter selected, nothing will be displayed for this member and quarter in the report. I want the report to behave such that there are records for every member in every quarter so that it appears as though the amounts are null.

So I'm thinking of creating a temporary table with member id, month and year for every member and quarter of the selected period. Or maybe I just have to create a temporary table with all the month and year for the quarters and iterate this through every member of the permanent table members_members. Is this the right way to go? Thanks so much.

Cheers,
Karen
View profile  Send private message
phpminer

Posts: 28
Posted: 09/01/2011, 12:30 AM

You may also want to consider performing a SQL group operation on table `fin_fbal`
by user, year and month - aggregating on the actual cost counts . . . this can act as your
temporary table (or view).

Then, perform a left-join with this grouped/aggregated table to get your final results.
View profile  Send private message
karen

Posts: 99
Posted: 09/01/2011, 12:39 AM

Thanks, phpminer. I'm still not sure how to accommodate those records that aren't actually there so the group operation will not produce them either. For example, going back to my simple example, User 2 will not have any resulting record for Mar & Dec 2010. And for example, another User 3, who did not enter anything will not have any records at all but needs to be shown on the report for all the quarters selected. Any ideas?

Also, where would one normally put all these 'preparatory code' in CCS so that the select statement can make use of temporary table or view? Thanks again!

Cheers,
Karen
View profile  Send private message
datadoit
Posted: 09/01/2011, 7:39 AM

Haven't really studied your query, but the first thing that comes to
mind is to do a subselect using WHERE NOT EXISTS. We do that often here.
karen

Posts: 99
Posted: 09/01/2011, 4:04 PM

Thanks, datadoit. I think this would be another option to the Left Join on the temporary table. Where should I put the code to generate the temporary table? Maybe in the before show event? Thanks.

Cheers,
Karen
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.