$year=2011;
// หา id สินค้าที่เข้าเงื่อนไขก่อน
$sql="select id_product from tb_product where name_product like '%milk%' ";
$query=mssql_db_query($sql);
while($result=mssql_fetch_array($query))
{
$id_product=$result[id_product];
//หาค่าเดือน 1
$sql1="select sum(total) as total1 from tb_total where id_product=$id_product AND MONTH(docudate) = '01' AND YEAR(docudate) = '$year'
";
$query1=mssql_db_query($sql1);
while($result1=mssql_fetch_array($query1))
{
$total1=$result1[total1];
}
//หาค่าเดือน 2
$sql2="select sum(total) as total2 from tb_total where id_product=$id_product AND MONTH(docudate) = '02' AND YEAR(docudate) = '$year'
";
$query2=mssql_db_query($sql2);
while($result2=mssql_fetch_array($query2))
{
$total2=$result2[total2];
}
//หาค่าเดือน 3
$sql3="select sum(total) as total3 from tb_total where id_product=$id_product AND MONTH(docudate) = '03' AND YEAR(docudate) = '$year' ";
$query3=mssql_db_query($sql3);
while($result3=mssql_fetch_array($query3))
{
$total3=$result3[total3];
}
//หาค่าเดือน 4
$sql4="select sum(total) as total4 from tb_total where id_product=$id_product AND MONTH(docudate) = '04' AND YEAR(docudate) = '$year' ";
$query4=mssql_db_query($sql4);
while($result4=mssql_fetch_array($query4))
{
$total4=$result4[total4];
}
//หาค่าเดือน 5
$sql5="select sum(total) as total5 from tb_total where id_product=$id_product AND MONTH(docudate) = '05' AND YEAR(docudate) = '$year' ";
$query5=mssql_db_query($sql5);
while($result5=mssql_fetch_array($query5))
{
$total5=$result5[total5];
}
//หาค่าเดือน 6
$sql6="select sum(total) as total6 from tb_total where id_product=$id_product AND MONTH(docudate) = '06' AND YEAR(docudate) = '$year' ";
$query6=mssql_db_query($sql6);
while($result6=mssql_fetch_array($query6))
{
$total6=$result6[total6];
}
//หาค่าเดือน 7
$sql7="select sum(total) as total7 from tb_total where id_product=$id_product AND MONTH(docudate) = '07' AND YEAR(docudate) = '$year' ";
$query7=mssql_db_query($sql7);
while($result7=mssql_fetch_array($query7))
{
$total7=$result7[total7];
}
//หาค่าเดือน 8
$sql8="select sum(total) as total8 from tb_total where id_product=$id_product AND MONTH(docudate) = '08' AND YEAR(docudate) = '$year' ";
$query8=mssql_db_query($sql8);
while($result8=mssql_fetch_array($query8))
{
$total8=$result8[total8];
}
//หาค่าเดือน 9
$sql9="select sum(total) as total9 from tb_total where id_product=$id_product AND MONTH(docudate) = '09' AND YEAR(docudate) = '$year' ";
$query9=mssql_db_query($sql9);
while($result9=mssql_fetch_array($query9))
{
$total9=$result9[total9];
}
//หาค่าเดือน 10
$sql3="select sum(total) as total10 from tb_total where id_product=$id_product AND MONTH(docudate) = '10' AND YEAR(docudate) = '$year' ";
$query10=mssql_db_query($sql10);
while($result10=mssql_fetch_array($query10))
{
$total10=$result10[total10];
}
//หาค่าเดือน 11
$sql11="select sum(total) as total11 from tb_total where id_product=$id_product AND MONTH(docudate) = '11' AND YEAR(docudate) = '$year' ";
$query11=mssql_db_query($sql11);
while($result11=mssql_fetch_array($query11))
{
$total11=$result11[total11];
}
//หาค่าเดือน 12
$sql12="select sum(total) as total12 from tb_total where id_product=$id_product AND MONTH(docudate) = '12' AND YEAR(docudate) = '$year' ";
$query12=mssql_db_query($sql12);
while($result12=mssql_fetch_array($query12))
{
$total12=$result12[total12];
}
}
SELECT
sumif(MONTH(docudate)='01', total, 0) as total01
sumif(MONTH(docudate)='02', total, 0) as total02
sumif(MONTH(docudate)='03', total, 0) as total03
.........
sumif(MONTH(docudate)='12', total, 0) as total12
FROM tb_total
WHERE id_product=$id_product AND YEAR(docudate) = '$year' ";
$sql="select tb_product.id_product,MONTH(tb_total.docudate) as month_num,sum(tb_total.total) as totalx from tb_product
inner join tb_total on tb_product.id_product=tb_total.id_product
where tb_product.name_product like '%milk%' and AND YEAR(tb_total.docudate) = '$year'";
group by tb_product.id_product,MONTH(tb_total.docudate)
order by 1,2";
$query=mssql_db_query($sql);
while($result=mssql_fetch_array($query)){
$total_all_id_produce_12month[$result[id_product]][intval($result[month_num])]=$result[totalx];
}
ตอนเอาไปใช้ก็
foreach($total_all_id_produce_12month as $id_product=>$arr_total){
for($i=1;$i<=12;$i++){
echo"<br>$id_product ==> month($i)==> ".$arr_total[$i];
}
}
ก็ประมาณนี้ (เอาไป run แล้วอาจมี error บ้างเพราะเขียนสด ไม่ได้ test ครับ)