_spaz Posted November 26, 2009 Share Posted November 26, 2009 Hi all, Can someone point me in the right direction with the below MYSQL statement? I'm trying to create buckets of "timeframes" that a specific item is received and when it is released (cycle time). I want to be able to count the amount of items that fall within each time frame. The below statement works but its not the best way to do since i do want to add a time period to it later such as sample set for each month. "Select (select count(*) from Table where DATEDIFF(Release, Arrival ) < 0) as less0, (select count(*) from Table where DATEDIFF(Release, Arrival ) = 0) as 0day, (select count(*) from Table where DATEDIFF(Release, Arrival ) BETWEEN 1 and 5) as 1to5, (select count(*) from Table where DATEDIFF(Release, Arrival ) BETWEEN 6 and 10) as 6to10" Quote Link to comment https://forums.phpfreaks.com/topic/183069-cycle-time-datediff/ Share on other sites More sharing options...
JustLikeIcarus Posted November 27, 2009 Share Posted November 27, 2009 You could try something along the lines of this. Which would enable you to come back and add in a where clause. Also this only requires reading in the table once. Select sum(if(DATEDIFF(Release, Arrival ) < 0, 1, 0)) as less0, sum(if(DATEDIFF(Release, Arrival ) = 0, 1, 0)) as 0day, sum(if(DATEDIFF(Release, Arrival ) BETWEEN 1 and 5, 1, 0)) as 1to5, sum(if(DATEDIFF(Release, Arrival ) BETWEEN 6 and 10, 1, 0)) as 6to10 FROM table Quote Link to comment https://forums.phpfreaks.com/topic/183069-cycle-time-datediff/#findComment-966430 Share on other sites More sharing options...
_spaz Posted November 30, 2009 Author Share Posted November 30, 2009 Thank you very much, i was able to get it working with you're syntax. Quote Link to comment https://forums.phpfreaks.com/topic/183069-cycle-time-datediff/#findComment-967794 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.