Jump to content

Recommended Posts

Hi Everyone,

 

I am using the following code to count the number of unique clients by ID that made payments during a specified period of time. What I really would like is a report that automatically does this for monthly periods eg: Jan 2006 - Jan 2007, Feb 2006 - Feb 2007 etc. and lists out a table of results for an entire year. Being relatively new to SQL programming I expect I would sort this out eventually but any shortcuts would be appreciated. This code is running on MySQL 4.1.20.

 

Also, any tips on better code or optimisation would be appreciated. The original data comes from elsewhere and is imported into MySQL as a CSV file - one issue I have had to deal with for this code is that some dates have a two-digit year value and some have 4 digits! Thanks for any advice offered.

 

SELECT  count( distinct a.clientid ) as numrows

FROM (

 

SELECT clientid,

CASE

WHEN length( date ) =8

THEN to_days( STR_TO_DATE( date, '%d-%c-%y' ) )

WHEN length( date ) =10

THEN to_days( STR_TO_DATE( date, '%d-%c-%Y' ) )

ELSE to_days( '1970-01-01' )

END AS maindate

FROM accbook

)a

/*

***** Set the date range below in the format YYYY-MM-DD *****

*/

 

WHERE a.maindate >= to_days( '2006-10-01' ) and a.maindate <= to_days('2007-10-01')

group by null

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.