2016-08-10 10 views
0

MySQLで生成される結果は非常に小さいです。これらの結果は、SSRSのレポート行グラフに表示する必要がありますが、現在の形式では不可能と思われます。MySQLでテーブルをピボットして列見出しを追加する

これは、現在の結果セットである:

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

あなただけの2つのカラムを持つ結果セットを生成するクエリを共有することができます「月」と「成果」? –

+0

スクリーンショットで使用されている現在のクエリをお見せください – Ultimater

+0

こんにちはTimグラフの作成時にSSRSがうまく理解できるように完成品がどのように表示されるべきか、私の考えです。私の現在のコーディングは、最初のテーブルに結果が表示され、2番目の – Mac

答えて

0

されている、事前にありがとうあなたのSELECTクエリの複雑さは、簡単のため、のは、クエリがいたとしましょう:

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

簡単な解決策はcolumを保存するためにSELECT INTOを使用することです変数としてNSその後、別のクエリで、その後、あなたのように変数をしてください。SELECT:つまり

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; 
+0

を生成するために列見出しを追加することを望んでいます。Ultimaterに感謝します。数字は毎月変わります。私は1040を 'SumOfCurrentMonth'と置き換えますか? 'INTO'セクションで@SumOfCurrentMonthとしますか? – Mac

+0

ハードコードされた数字は概念の証明です。私が最後に投稿したスニペットは、クエリを変更する方法です。 '...'を残りのクエリと置き換えるだけです。意味は '(Select OutcomeTimes.organisation_name'と' IfNull(outcome、No Outcome "))でOutcomeTimes2'で終わるコピーを貼り付けます。後でセミコロンを必ず追加してください。 – Ultimater

+0

私のコードにこれをどのように組み込むのか考えてもらえますか?これは私の現在の選択クエリの上に座っていますか? – Mac

関連する問題