Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,028

HOME > PHP > PHP Forum > ขออภัยที่ต้องยกมาขอความช่วยเหลืออีกครั้งครับ พยายามแล้วแต่ไม่สำเร็จ group by และ sum จำนวนแยกชนิด



 

ขออภัยที่ต้องยกมาขอความช่วยเหลืออีกครั้งครับ พยายามแล้วแต่ไม่สำเร็จ group by และ sum จำนวนแยกชนิด

 



Topic : 124298



โพสกระทู้ ( 111 )
บทความ ( 0 )



สถานะออฟไลน์




ขออภัยที่ต้องยกมาขอความช่วยเหลืออีกครั้งครับ พยายามแล้วแต่ไม่สำเร็จ group by และ sum จำนวนแยกชนิด ตามรูปครับ

tt2


อยากทำให้เป็นอย่างนี้ครับ

ttt


Code php

Code (PHP)
<body>
<?php include('config.php');?>
<form  action="" method="post" name="adminForm" id="adminForm">
<table width="57%" height="32" border="0" align="left" cellpadding="0" cellspacing="0" id="gridview">
       <tr style="font-family:Arial, Helvetica, sans-serif;font-style:normal;font-weight:normal; font-size:13px;color:black">
		<td width="62" align="center"> <span class="ff2">Date : </span></td> 
         <td width="155">
          <input type="text" id= "datepicker"  name="date_start" value="<?=$_POST['date_start'];?>"
               size="10"  style="width:150px;" onchange="document.adminForm.submit();" ></td>
          <td width="87" align="right" class="ff2"> Until Date : </td>
          <td width="460" align="left" class="ff2">
           <input type="text" id= "datepicker2"  name="date_stop" value="<?=$_POST['date_stop'];?>"
               size="10"  style="width:150px;" onchange="document.adminForm.submit();" /></td>
          </tr> </table>
</form>
<table width="64%" align="left" border="1" cellpadding="0" cellspacing="0">
  <thead>
    <tr>
      <th height="29" colspan="7" align="left" class="bb2"style="font-family:Arial, Helvetica, sans-serif;font-style:normal; font-size:13px;color:blackgray">Monthly Report</th>
    </tr>
    <tr bgcolor="#C3C3C3"style="font-family:Arial, Helvetica, sans-serif;font-style:normal;font-weight:normal; font-size:13px;color:black">
      <th width="9%" > Customer </th>
      <th width="7%" height="29" ><span class="adminlist55"><span class="gf222"><span class="ff2"> Credit</span></span></span></th>
      <th width="6%" ><span class="adminlist55"><span class="gf222"><span class="ff2"> Room</span></span></span></th>
      <th width="6%" ><span class="ff2">Cash </span></th>
      <th width="6%" ><span class="ff2">Package</span></th>
      <th width="6%" >House used</th>
      <th width="6%" >TOTAL</th>
    </tr>
  </thead>
  
      <?php
       $sql_show = "SELECT customer_table.customer,ots_table.*,
	   IF(chargedby='Credit',amount,'') AS price1,
	   IF(chargedby='Room',amount,'') AS price2,
       IF(chargedby='Cash',amount,'') AS price3, 
       IF(chargedby='Package',amount,'') AS price4,
       IF(chargedby='House used',amount,'') AS price5
       FROM customer_table,ots_table INNER JOIN charge_by_table ON ots_table.chargedby =charge_by_table.charge_by ";
     
   if($_POST)
	 { 
	 $date_start = $_POST['date_start'];
     $date_stop = $_POST['date_stop']; 
     $Credit="Credit";
	 $Room="Room";
	 $Cash="Cash";
	 $Package="Package";
     $House="House used";
	
             $sql_show   .=" where  (pickup_start_date between '".$_POST['date_start']."' and '".$_POST['date_stop']."')
	                         and chargedby in ('".$Room."' ,'".$Credit."','".$Cash."', '".$Package."', '".$House."')
							 and ots_table.customeragency = customer_table.customer
	                         GROUP BY customer_table.customer,ots_table.amount  ASC";
                 }
     $result_show = mysql_query($sql_show) or die(mysql_error());
     $RoomSum= 0;
     $CreditSum= 0;
	 $CashSum= 0;
     $PackageSum= 0;
	

  while($rs = mysql_fetch_array($result_show))
  {
	
    $Credit = $rs["price1"];
	$Room = $rs["price2"];
	$Cash = $rs["price3"];
	$Package = $rs["price4"];
	$House = $rs["price5"];
	$customer = $rs["customer"];
	
	$Total = $Credit+$Room+$Cash+$Package+$House;
    $CreditTotal += $Credit;
	$RoomTotal += $Room;
	$CashTotal += $Cash;
	$PackageTotal += $Package;
	$HouseTotal += $House;
	$GrandTotal += $Total;
	
		?>
    <tr align="center">
    <td height="38"><? echo $customer;?></td>
     <td height="38"><? echo $Credit;?></td>
      <td height="38"><? echo $Room;?></td>
       <td height="38"><? echo $Cash;?></td>
       <td height="38"><? echo $Package;?></td>
        <td height="38"><? echo $House;?></td>
       <td><? echo $Total;?></td>
  </tr>
    <? } ?> 
    <tr bgcolor="#C3C3C3"style="font-family:Arial, Helvetica, sans-serif;font-style:normal;font-weight:normal; font-size:13px;color:black">
      <th width="9%" bgcolor="#999999">Grand Total</th>
      <th width="7%" height="29" ><? echo $CreditTotal;?></th>
      <th width="6%" ><? echo $RoomTotal;?></th>
      <th width="6%" ><? echo $CashTotal;?></th>
      <th width="6%" ><? echo $PackageTotal;?></th>
      <th width="6%" ><? echo $HouseTotal;?></th>
      <th width="6%" ><? echo $GrandTotal;?></th>
    </tr>
    </table>
<p>&nbsp;</p>
</body>
<script type="text/javascript">
   $(function(){
	 $("#datepicker").datepicker({ dateFormat: "yy-mm-dd" }).val('<?=$_POST['date_start'];?>')
	 $("#datepicker2").datepicker({ dateFormat: "yy-mm-dd" }).val('<?=$_POST['date_stop'];?>')
	});
</script>


หมายเหตุ ...ถ้าตัด ots_table.amount ออกจาก GROUP BY ชื่อ customer จะเหลือเพียงอย่างละชื่อ (ชื่อเดียวไม่แสดงซ้ำตามต้องการ) แต่ยอดเงิน ไม่รวมมา (ตามรูปที่วงกลมสีแดงไว้) จะต้องเขียนคำสั่งใน code อย่างไร ตรงไหน ช่วยทีครับ พยายามทำหลายแบบ แต่ไม่สำเร็จ ต้องใช้ SUM ตรงไหน ครับ ขอบพระคุณมากครับ



Tag : PHP









ประวัติการแก้ไข
2016-08-19 17:34:45
2016-08-19 17:36:09
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-08-19 17:33:01 By : panya@ots View : 702 Reply : 4
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ถ้า Result แบบนี้จะต้อง Group แต่ล่ะ Type ก่อนครับ แล้วค่อยนำมา JOIN กันครับ เช่น

Code (SQL)
SELECT a.CustomerID,b.SumCredit ,c.SumRoom  FROM (SELECT CustomerID FROM orders GROUP BY CustomerID) a
LEFT JOIN
	-- Credit
	(SELECT CustomerID , SUM(Credit) AS SumCredit FROM table_name GROUP BY CustomerID) b INNER JOIN a.CustomerID = b.CustomerID
LEFT JOIN
	-- Room
	(SELECT CustomerID , SUM(Room) AS SumRoom FROM table_name GROUP BY CustomerID) c INNER JOIN a.CustomerID = c.CustomerID



ประมาณนี้ครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-08-24 14:30:55 By : mr.win
 


 

No. 2



โพสกระทู้ ( 111 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 1 เขียนโดย : mr.win เมื่อวันที่ 2016-08-24 14:30:55
รายละเอียดของการตอบ ::
ขอบคุณครับ พี่win

ตรงนี้แสดงว่า Join 3 ตาราง หรือครับ

a.CustomerID,b.SumCredit ,c.SumRoom

คือ ตาราง a b c ใช่ป่าวครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-08-24 20:19:24 By : panya@ots
 

 

No. 3



โพสกระทู้ ( 484 )
บทความ ( 3 )



สถานะออฟไลน์


a,b,c คือ alias name
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-08-25 08:09:54 By : fossil31
 


 

No. 4



โพสกระทู้ ( 111 )
บทความ ( 0 )



สถานะออฟไลน์


ตอบความคิดเห็นที่ : 3 เขียนโดย : fossil31 เมื่อวันที่ 2016-08-25 08:09:54
รายละเอียดของการตอบ ::
ขอบคุณมากครับคุร fossil เดี๋ยวลองศึกษาดู ถ้าติดอะไรคงต้องขอรบกวนใหม่ครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-08-25 09:28:45 By : panya@ots
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ขออภัยที่ต้องยกมาขอความช่วยเหลืออีกครั้งครับ พยายามแล้วแต่ไม่สำเร็จ group by และ sum จำนวนแยกชนิด
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่