Translate

แสดงบทความที่มีป้ายกำกับ PIVOT แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ PIVOT แสดงบทความทั้งหมด

วันเสาร์ที่ 3 ตุลาคม พ.ศ. 2558

SQL Pivot Table (English)

Make SQL Pivot Table


Pivot Table is result data table from transaction data is processed by Aggregate function (Ex. Sum, Avg, Max, Min) Pivot table is use for analyst each point of view. For easy to understand it. We see example now.



This structur table contains.
SellerName : Name of Seller.
InvoiceNO : No of invoice or bill.
InvoiceDate : Date of Invoice or bill.
Amount : Total circulation of Invoice.

Assume We have question is we look for sales volume of each seller and separate by month.
General query we maybe with WHERE MONTH(InvoiceDate) = 1 , WHERE MONTH(InvoiceDate) = 2 til complete And maybe use GROUP BY Together.

Example Query Month = 1 Only
SELECT SellerName,SUM(Amount) as Amount
  FROM InvoiceList
  WHERE 
  MONTH(InvoiceDate)=1

  GROUP BY SellerName 

View of report is not comfortable to analyst because we will use
several month table. For comfortable to analyst we should use PIVOT

Example Query Pivot 
SELECT * FROM
(SELECT MONTH(InvoiceDate) as Month, Amount ,SellerName 
FROM InvoiceList
) as SourceTable
PIVOT
(
SUM(Amount) for SellerName in (Dang,Dum,Jumpee,SreeDa)

) as PV



*** Next problem is null value on the table. Focus at month 1 of Dum It has Null because all of month 1 Dum not have circulation. If we use this query on program maybe found error. We can use ISNULL for improve our query.

SELECT Month 
, ISNULL(Dang,0) as Dang 
, ISNULL(Dum,0) as Dum 
, ISNULL(Jumpee,0) as Jumpee 
, ISNULL(SreeDa,0) as SreeDa 
 FROM
(SELECT MONTH(InvoiceDate) as Month, Amount ,SellerName 
FROM InvoiceList
) as SourceTable
PIVOT
(
SUM(Amount) for SellerName in (Dang,Dum,Jumpee,SreeDa)
) as PV

After Improve query...









SQL Pivot Table (ภาษาไทย)

การทำ SQL Pivot Table


Pivot Table คือตารางข้อมูลที่ได้มาจาก Transaction Data ผ่านกระบวนการใน Aggregate function อย่างเช่น Sum, Avg, Max และ Min ก็จะได้ตารางสรุปแต่ละแบบออกมา รูปแบบตารางที่ได้จาก Query นั้นจะเป็นรายงานที่นำไปใช้ในการวิเคราะห์ในแง่ต่างๆ ต่างจาก Query Transaction ปกติ เพื่อให้ง่ายต่อการทำความเข้าใจ เราไปดูตัวอย่างประกอบกันเลยครับ



จากตารางดังกล่าว ประกอบด้วย
SellerName : รายชื่อพนักขาย
InvoiceNO : เลขที่ใบขายสินค้า
InvoiceDate : วันที่ของใบขายสินค้า
Amount : ยอดขายในใบขายสินค้า

สมมุติเรามีโจทย์ว่า เราต้องการดูยอดขายของพนักงานขายแต่ละคน โดยแยกตามเดือน การ Query แบบทั่วไป
อาจจะต้อง Query โดยใช้ WHERE MONTH(InvoiceDate) = 1 
, WHERE MONTH(InvoiceDate) = 2  ไปจนครบ และจะต้องใช้ Group By ร่วมไปด้วย

ตัวอย่างการ Query เฉพาะยอดรวมของเดือนที่ 1
SELECT SellerName,SUM(Amount) as Amount
  FROM InvoiceList
  WHERE 
  MONTH(InvoiceDate)=1

  GROUP BY SellerName 

การแสดงผลอาจจะใช้ในการเปรียบเทียบค่อนข้างลำบาก เพราะต้องดูแบบแยกเดือน
เพื่อความสะดวกในการเปรียบเทียบรายงานจึงแนะนำให้ใช้การ PIVOT แทน

ตัวอย่างการ Query แบบ Pivot 
SELECT * FROM
(SELECT MONTH(InvoiceDate) as Month, Amount ,SellerName 
FROM InvoiceList
) as SourceTable
PIVOT
(
SUM(Amount) for SellerName in (Dang,Dum,Jumpee,SreeDa)

) as PV



Pivot ค่อนข้างจะมีประโยชน์ในแง่ของการวิเคราะห์ เช่น อยากทราบว่าในแต่ละเดือนพนักคนไหนจะมียอดขายสูงสุด

*** ปัญหาต่อมาคือ ถ้าไปใช้ใน code แล้วจะเห็นได้ว่า ในเดือนที่ 1 นาย Dum มีค่าเป็น Null เพราะ ไม่มียอดขายเลย เมื่อไปใช้ในโปรแกรมอาจจะเกิด Error ขึ้นได้ เพราะฉะนั้นเราจึงปรับปรุงโดยใช้คำสั่ง ISNULL 

SELECT Month 
, ISNULL(Dang,0) as Dang 
, ISNULL(Dum,0) as Dum 
, ISNULL(Jumpee,0) as Jumpee 
, ISNULL(SreeDa,0) as SreeDa 
 FROM
(SELECT MONTH(InvoiceDate) as Month, Amount ,SellerName 
FROM InvoiceList
) as SourceTable
PIVOT
(
SUM(Amount) for SellerName in (Dang,Dum,Jumpee,SreeDa)
) as PV

เมื่อ Query ก็จะได้ข้อมูลที่สมบูรณ์ตามรูปด้านล่างนี้


หวังว่าบทความนี้คงมีประโยชน์ไม่มากก็น้อยสำหรับโปรแกรมเมอร์นะครับ