在MySQL中旋转表格并添加列标题

时间:2016-08-10 08:28:21

标签: mysql reporting-services pivot ssrs-2008-r2

我在MySQL中生成了一小组结果。这些结果需要显示在SSRS报告折线图上,但在目前的格式中,这似乎是不可能的。

这是当前的结果集:

image1

这就是我认为它可能起作用的方式

iamge2

我在想通过旋转表格并添加列标题,SSRS可能能够理解我想要实现的目标并正确显示数据。

我在这里和其他地方看过,但所有的工作似乎都很复杂。有没有办法简单地转动数据和添加列标题,还是我最好重新使用MySQL来生成不同的数据?

这是我目前的代码,我提前道歉,但我是这个

的新手
Select SUM(OutcomeTimes2.CurrentMonth) as SumOfCurrentMonth,     SUM(OutcomeTimes2.CurrentLess1) as SumOfCurrentLess1, SUM(OutcomeTimes2.CurrentLess2) as SumOfCurrentLess2, SUM(OutcomeTimes2.CurrentLess3) as SumOfCurrentLess3, SUM(OutcomeTimes2.CurrentLess4) as SumOfCurrentLess4, SUM(OutcomeTimes2.CurrentLess5) as SumOfCurrentLess5, SUM(OutcomeTimes2.CurrentLess6) as SumOfCurrentLess6
from (SELECT OutcomeTimes.organisation_name, OutcomeTimes.organisation_id, OutcomeTimes.name, OutcomeTimes.order_no, ifnull(OutcomeTimes.budgetcode,"No Budget Code")as budgetcode, ifnull(OutcomeTimes.budgetname,"No Budget Name")as budgetname, Sum(OutcomeTimes.Budget_Duration) AS SumOfBudget_Duration, Sum(OutcomeTimes.Actual_Duration) AS SumOfActual_Duration, OutcomeTimes.the_date, Ifnull(outcome,"No Outcome") AS Outcome_rec
, if(date_format(date(OutcomeTimes.the_date), '%m %Y')=date_format(date_sub(now(), interval 0 MONTH), '%m %Y'),1,0) as CurrentMonth
, if(date_format(date(OutcomeTimes.the_date), '%m %Y')=date_format(date_sub(now(), interval 1 MONTH), '%m %Y'),1,0) as CurrentLess1, if(date_format(date(OutcomeTimes.the_date), '%m %Y')=date_format(date_sub(now(), interval 2 MONTH), '%m %Y'),1,0) as CurrentLess2, if(date_format(date(OutcomeTimes.the_date), '%m %Y')=date_format(date_sub(now(), interval 3 MONTH), '%m %Y'),1,0) as CurrentLess3
, if(date_format(date(OutcomeTimes.the_date), '%m %Y')=date_format(date_sub(now(), interval 4 MONTH), '%m %Y'),1,0) as CurrentLess4, if(date_format(date(OutcomeTimes.the_date), '%m %Y')=date_format(date_sub(now(), interval 5 MONTH), '%m %Y'),1,0) as CurrentLess5, if(date_format(date(OutcomeTimes.the_date), '%m %Y')=date_format(date_sub(now(), interval 6 MONTH), '%m %Y'),1,0) as CurrentLess6
FROM (SELECT qry_bookings.organisation_id, qry_bookings.organisation_name, qry_bookings.order_no, qry_bookings.the_date, qry_bookings.start_Time, qry_bookings.end_Time, TIMESTAMPDIFF(MINUTE,start_Time,end_Time) AS Budget_Duration, if(qry_bookings.name ="","No Name",qry_bookings.name) as name , qry_bookings.actual_start_Time, qry_bookings.actual_end_Time, TIMESTAMPDIFF(MINUTE,actual_start_Time,qry_bookings.actual_end_Time) AS Actual_Duration, qry_bookings.budgetcode, qry_bookings.budgetname, qry_bookings.outcome, schedule_overview.outcome_code_desc
FROM qry_bookings INNER JOIN schedule_overview ON qry_bookings.schedule_id = schedule_overview.schedule_id
WHERE ((qry_bookings.business_unit_id="2") AND (qry_bookings.deleted_from_schedule=0) and (qry_bookings.the_date Between CAST(DATE_FORMAT(date_sub(now(), interval 6 MONTH) ,'%Y-%m-01') as DATE) And Now())) and (((qry_bookings.organisation_id)="797007013984") OR 
(((qry_bookings.organisation_id)="363079430613984")) OR 
(((qry_bookings.organisation_id)="137952779314169")) OR 
(((qry_bookings.organisation_id)="996006860914169")) OR 
(((qry_bookings.organisation_id)="289833198813984")) OR 
(((qry_bookings.organisation_id)="581692616814417")) OR
(((qry_bookings.organisation_id)="70247802713984")) OR
(((qry_bookings.organisation_id)="917771077113984")) OR
(((qry_bookings.organisation_id)="317283772114056")) OR
(((qry_bookings.organisation_id)="592108421914555")) OR
(((qry_bookings.organisation_id)="177551075713984")) OR
(((qry_bookings.organisation_id)="28576585213984")) OR
(((qry_bookings.organisation_id)="180051500814593")) OR
(((qry_bookings.organisation_id)="472612326714612")) OR
(((qry_bookings.organisation_id)="865056550613984")) OR
(((qry_bookings.organisation_id)="50126601513984")) OR
(((qry_bookings.organisation_id)="124179841214194")) OR
(((qry_bookings.organisation_id)="407940379014254")) OR
(((qry_bookings.organisation_id)="409966399013984")) OR
(((qry_bookings.organisation_id)="747474374413984")) OR
(((qry_bookings.organisation_id)="788147281813987")))
ORDER BY qry_bookings.organisation_name, qry_bookings.name) AS OutcomeTimes
GROUP BY OutcomeTimes.organisation_name, OutcomeTimes.name, OutcomeTimes.order_no, OutcomeTimes.budgetcode, OutcomeTimes.budgetname, OutcomeTimes.the_date, IfNull(outcome,"No Outcome")) as OutcomeTimes2

先谢谢你们,你们很聪明,并且一直都很有帮助

1 个答案:

答案 0 :(得分:0)

考虑到SELECT查询的复杂性,为简单起见,假设您的查询是:

SELECT 1040 AS 'a', 3279 AS 'b', 3582 AS 'c';

一个简单的解决方案是使用SELECT INTO将列存储为变量,然后使用另一个查询随后选择变量:

SELECT 1040 AS 'a', 3279 AS 'b', 3582 AS 'c' INTO @a, @b, @c;
SELECT 'SumOfCurrentMonth' AS 'Month', @a AS Outcomes UNION ALL
SELECT 'SumOfCurrentLess1' AS 'Month', @b AS Outcomes UNION ALL
SELECT 'SumOfCurrentLess2' AS 'Month', @c AS Outcomes

换句话说:

SELECT SUM(OutcomeTimes2.CurrentMonth) as SumOfCurrentMonth,
       SUM(OutcomeTimes2.CurrentLess1) as SumOfCurrentLess1,
       SUM(OutcomeTimes2.CurrentLess2) as SumOfCurrentLess2,
       SUM(OutcomeTimes2.CurrentLess3) as SumOfCurrentLess3,
       SUM(OutcomeTimes2.CurrentLess4) as SumOfCurrentLess4,
       SUM(OutcomeTimes2.CurrentLess5) as SumOfCurrentLess5,
       SUM(OutcomeTimes2.CurrentLess6) as SumOfCurrentLess6
       INTO @a,@b,@c,@d,@e,@f,@g
FROM ... ;
SELECT 'SumOfCurrentMonth' AS 'Month', @a AS Outcomes UNION ALL
SELECT 'SumOfCurrentLess1' AS 'Month', @b AS Outcomes UNION ALL
SELECT 'SumOfCurrentLess2' AS 'Month', @c AS Outcomes UNION ALL
SELECT 'SumOfCurrentLess3' AS 'Month', @d AS Outcomes UNION ALL
SELECT 'SumOfCurrentLess4' AS 'Month', @e AS Outcomes UNION ALL
SELECT 'SumOfCurrentLess5' AS 'Month', @f AS Outcomes UNION ALL
SELECT 'SumOfCurrentLess6' AS 'Month', @g AS Outcomes;