Reply to Re: help with a query

Your name:

Reply:


Posted by Ed Murphy on 02/27/07 17:59

Sam wrote:

> MyTable {field1, field2, startdate, enddate}
>
> I want to have the count of field1 between startdate and enddate, and
> the count of field2 where field2 = 1 between startdate and enddate,
> all in the same query.

Based on your sample query, it appears that you want

1) all rows where (startdate to enddate) falls within given limits
2) separate subtotals for each distinct value of field1 where 1) is true

(This is why Erland, Celko, et al. keep ranting about "post your
CREATE TABLE, sample data, and desired output" - because it largely
eliminates the need to make educated guesses such as the above.)

Anyway, this should cover it:

select field1, count(field1), sum(case field2 when 1 then 1 else 0)
from MyTable
where startdate >= '01-24-2007' and enddate <= '02-25-2007'
group by field1

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация