select c.BudgettypeID, sum(b.Clearbillpay), sum(อื่นๆ 1), sum(อื่นๆ 2)
from tbbudgetquarter q
left join tbclearbill b on b.Clearbilldate>=q.Budgetquarterstart and b.Clearbilldate<=q.Budgetquarterend
left join tbbudgetcategory c on c.(ตรงนี้ไม่รู้จะจอยยังไงไม่เห็น ชื่อ field ที่เกีียวข้องกัน 55555)
group by c.BudgettypeID, q.Budgetquarterstart
having q.Budgetquaterstart=ไตรมาสที่ต้องการ
select c.BudgettypeID, q.BudgetquarterID quarter, c.Budgetcategoryamount amt, sum(b.Clearbillpay) pay
from tbbudgetquarter q
left join tbclearbill b on b.Clearbilldate>=q.Budgetquarterstart and b.Clearbilldate<=q.Budgetquarterend
left join tbbudgetcategory c on c.BudgettypeID=b.BudgettypeID
group by c.BudgettypeID, q.BudgetquarterID
select tbbudgetcategory.BudgettypeID, tbbudgetquarter.BudgetquarterID Quarter, tbbudgetcategory.Budgetcategoryamount amt, sum(tbclearbill.Clearbillpay) pay
from tbbudgetquarter
left join tbclearbill on tbclearbill.Clearbilldate>= tbbudgetquarter.Budgetquarterstart and tbclearbill.Clearbilldate<=tbbudgetquarter.Budgetquarterend
left join tbbudgetcategory on tbbudgetcategory.BudgettypeID=tbclearbill.BudgettypeID
group by tbbudgetcategory.BudgettypeID, tbbudgetquarter.BudgetquarterID
select c.BudgettypeID, q.quarter, c.Budgetcategoryamount amt, sum(b.Clearbillpay) pay from (select 1 as quarter, '2012-10-01' qstart, '2012-12-31' qend) q
left join tbclearbill b on b.Clearbilldate between q.start and q.qend
left join tbbudgetcategory c on c.BudgettypeID=b.BudgettypeID
group by c.BudgettypeID, q.quarter
ก็ใช้ตรงสีแดงคุมวันเวลา
หรืออาจจะเพิ่ม ไตรมาสตามชอบก็ได้อีกเหมือนกัน from (
select 1 as quarter, '2012-10-01' qstart, '2012-12-31' qend
union all
select 2, '2013-01-01', '2013-03-31'
union all
select 3, '2013-04-01', '2013-06-30'
union all
select 4, '2013-07-01', '2013-09-30'
) q
select c.BudgettypeID, q.BudgetquarterID quarter, c.Budgetcategoryamount amt, sum(b.Clearbillpay) pay
from tbbudgetquarter q
left join tbclearbill b on b.Clearbilldate>=q.Budgetquarterstart and b.Clearbilldate<=q.Budgetquarterend
left join tbbudgetcategory c on c.BudgettypeID=b.BudgettypeID
group by c.BudgettypeID, q.BudgetquarterID
select c.BudgettypeID, q.BudgetquarterID quarter, c.Budgetcategoryamount amt, sum(b.Clearbillpay) pay
from tbbudgetquarter q
left join tbclearbill b on b.Clearbilldate>=q.Budgetquarterstart and b.Clearbilldate<=q.Budgetquarterend
left join tbbudgetcategory c on c.BudgettypeID=b.BudgettypeID
group by c.BudgettypeID, q.BudgetquarterID
SELECT tbbudgetcategory.BudgettypeID, tbbudgettype.Budgettype,tbbudgetcategory.Budgetcategoryamount เงินต้น,ABS( SUM( tbclearbill.Clearbillpayer ) - Budgetcategoryamount ) AS เงินคงเหลือ, SUM( tbclearbill.Clearbillpayer ) ไตรมาส1
FROM tbbudgetquarter
LEFT JOIN tbclearbill ON tbclearbill.Clearbilldate >= tbbudgetquarter.Budgetquarterstart
AND tbclearbill.Clearbilldate <= tbbudgetquarter.Budgetquarterend
LEFT JOIN tbbudgetcategory ON tbbudgetcategory.BudgettypeID = tbclearbill.BudgettypeID
LEFT JOIN tbbudgettype ON tbbudgetcategory.BudgettypeID = tbbudgettype.BudgettypeID
WHERE tbbudgetcategory.BudgetyearID = 41
GROUP BY tbbudgetcategory.BudgettypeID
ผมมีเงื่อนไข select ไตรมาสทั้ง 4 ไตรมาสแล้วนะครับ
เราจะเอามาใส่ ในคำสั่ง SELECT ยังไงดีครับ
Code (SQL)
" BETWEEN '$yeareng2-10-1' AND '$yeareng2-12-31' ";
"BETWEEN '$yeareng-01-1' AND '$yeareng-03-31' ";
"BETWEEN '$yeareng-04-1' AND '$yeareng-06-30' ";
"BETWEEN '$yeareng-07-1' AND '$yeareng-09-30' ";