 |
สอบถามการ SUM Cross Tab แต่ละเดือนด้านล่าง ต้องทำอย่างไรคะ |
|
 |
|
|
 |
 |
|
Code (SQL)
SELECT jt.jt_type As ประเภท,sum(IF(Month(j.job_complete)
=1,1,0)) AS มกราคม,sum(IF(Month(j.job_complete)=2,1,0)) AS
กุมภาพันธ์,sum(IF(Month(j.job_complete)=3,1,0)) AS
มีนาคม,sum(IF(Month(j.job_complete)=4,1,0)) AS
เมษายน,sum(IF(Month(j.job_complete)=5,1,0)) AS
พฤษภาคม,sum(IF(Month(j.job_complete)=6,1,0)) AS
มิถุนายน,sum(IF(Month(j.job_complete)=7,1,0)) AS
กรกฎาคม,sum(IF(Month(j.job_complete)=8,1,0)) AS
สิงหาคม,sum(IF(Month(j.job_complete)=9,1,0)) AS
กันยายน,sum(IF(Month(j.job_complete)=10,1,0)) AS
ตุลาคม,sum(IF(Month(j.job_complete)=11,1,0)) AS
พฤศจิกายน,sum(IF(Month(j.job_complete)=12,1,0)) AS
ธันวาคม,Count(j.job_complete) as Total
FROM job as j INNER JOIN job_type as jt ON
j.job_type=jt.jt_id GROUP BY j.job_type
ผลที่ได้

จากรูป อยากได้ผลรวมด้านล่างของแต่ละเดือน ต้องเขียนเพิ่มอย่างไรคะ
ขอบคุณคะ
Tag : PHP, MySQL
|
|
 |
 |
 |
 |
Date :
2014-08-25 16:07:21 |
By :
Nicorobin |
View :
1783 |
Reply :
9 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เราได้ทำได้แล้วนะคะ และนี้เป็นตัวอย่างเผื่อใครเจอปัญหาแบบเดียวกันคะ
Code (SQL)
(SELECT jt.jt_type As ประเภท,sum(IF(Month(j.job_complete)
=1,1,0)) AS มกราคม,sum(IF(Month(j.job_complete)=2,1,0)) AS
กุมภาพันธ์,sum(IF(Month(j.job_complete)=3,1,0)) AS
มีนาคม,sum(IF(Month(j.job_complete)=4,1,0)) AS
เมษายน,sum(IF(Month(j.job_complete)=5,1,0)) AS
พฤษภาคม,sum(IF(Month(j.job_complete)=6,1,0)) AS
มิถุนายน,sum(IF(Month(j.job_complete)=7,1,0)) AS
กรกฎาคม,sum(IF(Month(j.job_complete)=8,1,0)) AS
สิงหาคม,sum(IF(Month(j.job_complete)=9,1,0)) AS
กันยายน,sum(IF(Month(j.job_complete)=10,1,0)) AS
ตุลาคม,sum(IF(Month(j.job_complete)=11,1,0)) AS
พฤศจิกายน,sum(IF(Month(j.job_complete)=12,1,0)) AS
ธันวาคม,Count(j.job_complete) as Total
FROM job as j RIGHT JOIN job_type as jt ON
j.job_type=jt.jt_id GROUP BY j.job_type)
UNION
(Select 'TOTAL' As TOTAL,sum(IF(Month(j.job_complete)
=1,1,0)) AS มกราคม,sum(IF(Month(j.job_complete)=2,1,0)) AS
กุมภาพันธ์,sum(IF(Month(j.job_complete)=3,1,0)) AS
มีนาคม,sum(IF(Month(j.job_complete)=4,1,0)) AS
เมษายน,sum(IF(Month(j.job_complete)=5,1,0)) AS
พฤษภาคม,sum(IF(Month(j.job_complete)=6,1,0)) AS
มิถุนายน,sum(IF(Month(j.job_complete)=7,1,0)) AS
กรกฎาคม,sum(IF(Month(j.job_complete)=8,1,0)) AS
สิงหาคม,sum(IF(Month(j.job_complete)=9,1,0)) AS
กันยายน,sum(IF(Month(j.job_complete)=10,1,0)) AS
ตุลาคม,sum(IF(Month(j.job_complete)=11,1,0)) AS
พฤศจิกายน,sum(IF(Month(j.job_complete)=12,1,0)) AS
ธันวาคม,Count(j.job_complete) as Total
FROM job as j )
|
 |
 |
 |
 |
Date :
2014-08-26 17:15:56 |
By :
Nicorobin |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
SQL Server 2008R2
Code (SQL)
IF OBJECT_ID('dbo.yourTable', 'U') IS NOT NULL
DROP TABLE dbo.yourTable
CREATE TABLE yourTable (Val1 int, Val2 int, Tran_Date datetime, Job_Type varchar(1))
INSERT INTO yourTable (Val1, Val2, Tran_Date, Job_Type)
VALUES
(1, 0, '2012-01-01 00:00:00', 'A'),
(1, 1, '2012-02-01 00:00:00', 'A'),
(2, 1, '2012-03-01 00:00:00', 'A'),
(2, 2, '2012-04-01 00:00:00', 'A'),
(2, 3, '2012-05-01 00:00:00', 'A'),
(2, 4, '2012-06-01 00:00:00', 'A'),
(2, 5, '2012-07-01 00:00:00', 'A'),
(0, 2, '2012-08-01 00:00:00', 'A'),
(4, 2, '2012-09-01 00:00:00', 'A'),
(7, 1, '2012-10-01 00:00:00', 'A'),
(2, 0, '2012-11-01 00:00:00', 'A'),
(0, 1, '2012-12-01 00:00:00', 'A'),
(6, 4, '2012-12-01 00:00:00', 'B'),
(3, 7, '2012-01-31 00:00:00', 'B')
select *
from
(
Select Val1 + Val2 as Total,
month(Tran_Date) as 'm01Tom12_Per_One_Year',
yourTable.Job_Type
from yourTable
where year(Tran_Date) = 2012
) tmpQuery
pivot
(
sum(Total)
for m01Tom12_Per_One_Year in ([1], [2],[3], [4], [5],[6],[7],[8],[9],[10],[11],[12])
) ResultQuery
IF OBJECT_ID('dbo.yourTable', 'U') IS NOT NULL
DROP TABLE dbo.yourTable
|
 |
 |
 |
 |
Date :
2014-08-27 10:23:46 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อสงสัยที่มีประโยชน์ ถ้าต้องการมากกว่า 1 ปี? Group By Job_Type Anchor By Month (01-12)
YEAR JobType 01 02... 12
2012 A
2012 B
2013 A
2014 A
xxx
|
 |
 |
 |
 |
Date :
2014-08-27 10:57:57 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จาก #NO6 ข้อสงสัยที่มีประโยชน์ ถ้าต้องการดูรายละเอียดของแต่ละ X
หรือ Sub Query Reports (จำแนกเป็น A x B Dimension บนอาเรย์ไร้มิติ)
+55555
|
ประวัติการแก้ไข 2014-08-27 13:44:01
 |
 |
 |
 |
Date :
2014-08-27 13:43:21 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
จาก #NO 7 Crystal Report/MS Reports/ etc Reports
ทุกฯ Reports & TOR & Designers.
มันทำได้จริงหรือไม่?
จินตะนากาล

|
ประวัติการแก้ไข 2014-08-27 13:53:42
 |
 |
 |
 |
Date :
2014-08-27 13:48:58 |
By :
หน้าฮี |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|