Tuesday, 15 June 2010

php - Check of row existed previous month (comparing data between two months) where number = certain value -



php - Check of row existed previous month (comparing data between two months) where number = certain value -

ogi table

number name cost month year 152 cheese 25 10 12 153 yogurt 12 10 12 152 cheese 22 11 12 153 yogurt 15 11 12 154 apples 30 11 12

i have next query comparing rows between 2 months.

select a.price p1, b.price p2, a.distributor, a.number, a.name, (a.price - b.price) pdiff ogi a, ogi b a.number = b.number , a.month = '" . $from_date . "' , b.month = '" . $to_date . "' , a.distributor = '" . $distributor . "'

i trying check see if row did not exist in previous month, echo "did not exist" or along lines.

so, how can check see if row did not exist previous month number 154 (found in table above) , echo "did not exist"?

thanks help!

if need check cost delta of every month previous month, write query way:

select a.*, b.price, case when b.number not null a.price-b.price else 'did not exist' end pricediff ogi left bring together ogi b on a.number=b.number , b.year = case when a.month=1 a.year-1 else a.year end , b.month = case when a.month=1 12 else a.month-1 end order a.number, a.month

this compare every cost of every item cost of previous month, , homecoming 'did not exist' if there's no row previus month.

please see this fiddle.

edit: updated answer, think looking this:

select names.number, names.name, a.price pricea, b.price priceb, case when a.number null 'does not exist' when b.number null 'did not exist' else a.price-b.price end pricediff (select distinct number, name ogi (year=12 , month=11) or (year=12 , month=10)) names left bring together ogi on names.number=a.number , a.month=11 , a.year=12 left bring together ogi b on names.number=b.number , b.month=10 , b.year=12 order a.number, a.month

please notice not considering year, if needed can update query.

php mysql

No comments:

Post a Comment