2017-05-08 322 views
1

在我的頭撞到牆上之後,我試圖使用此示例表內容獲取數據透視數據。如何在Mysql中的Group_Concat中使用if語句

id--------------lastname-----total----rd    
0511-02-0318 Salvador 8.99 2015-09-08 
0214-02-0616 Alfaro 0  2015-09-08 
0214-02-0616 Alfaro 7.95 2015-09-07 
0214-02-0616 Alfaro 8.69 2015-09-04 
0214-02-0616 Alfaro 6.81 2015-09-01 
0214-02-0616 Alfaro 8.86 2015-08-29 
0214-02-0616 Alfaro 6.16 2015-08-28 
0214-02-0616 Alfaro 8.66 2015-08-27 
0214-02-0616 Alfaro 8.03 2015-08-26 
0214-02-0616 Alfaro 8.68 2015-08-25 
0214-02-0616 Alfaro 8.75 2015-08-24 
0511-02-0318 Salvador 9.17 2015-09-09 
0511-02-0318 Salvador 9.28 2015-09-10 
0415-02-0960 Martin 0  2015-09-18 
0415-02-0960 Martin 10.39 2015-09-17 
0415-02-0960 Martin 8.44 2015-09-16 
0415-02-0960 Martin 10.9 2015-09-15 
0415-02-0960 Martin 10.91 2015-09-14 
0415-02-0960 Martin 9.91 2015-09-11 
0415-02-0960 Martin 10.31 2015-09-10 
0415-02-0960 Martin 11.16 2015-09-09 
0415-02-0960 Martin 12.04 2015-09-08 
0415-02-0960 Martin 9.42 2015-09-07 
0415-02-0960 Martin 10.43 2015-09-04 
0415-02-0960 Martin 10.39 2015-09-03 

這是我工作的學嘗試查詢,但我想我錯過了一些東西:

SET @sql = NULL; 
set @sdate = '2015-09-01'; 
set @edate = '2015-09-06'; 

SELECT GROUP_CONCAT(

distinct concat('total=',list_of_dates) 

/*distinct concat('if(p.rd=''',list_of_dates,''',p.total',0,')')*/ 

) INTO @sql from 
(SELECT ADDDATE(date(@sdate), INTERVAL @i:[email protected]+1 DAY) AS list_of_dates 
FROM (
SELECT a.a 
FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a 
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b 
CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c 
) a 
JOIN (SELECT @i := -1) r1 
WHERE 
@i < DATEDIFF(date(@edate),date(@sdate))) as a; 





set @sql = concat('select id, lastname, total, rd,',@sql,' from 
(select ''0511-02-0318'' as id, ''Salvador'' as lastname, 8.99 as total, ''2015-09-08'' as rd union all 
select ''0214-02-0616'', ''Alfaro'', 0 ,''2015-09-08'' union all 
select ''0214-02-0616'', ''Alfaro'', 7.95, ''2015-09-07'' union all 
select ''0214-02-0616'', ''Alfaro'', 8.69, ''2015-09-04'' union all 
select ''0214-02-0616'', ''Alfaro'', 6.81, ''2015-09-01'' union all 
select ''0214-02-0616'', ''Alfaro'', 8.86, ''2015-08-29'' union all 
select ''0214-02-0616'', ''Alfaro'', 6.16, ''2015-08-28'' union all 
select ''0214-02-0616'', ''Alfaro'', 8.66, ''2015-08-27'' union all 
select ''0214-02-0616'', ''Alfaro'', 8.03, ''2015-08-26'' union all 
select ''0214-02-0616'', ''Alfaro'', 8.68, ''2015-08-25'' union all 
select ''0214-02-0616'', ''Alfaro'', 8.75, ''2015-08-24'' union all 
select ''0511-02-0318'', ''Salvador'', 9.28, ''2015-09-10'' union all 
select ''0415-02-0960'', ''Martin'', 0, ''2015-09-18'' union all 
select ''0415-02-0960'', ''Martin'', 10.39, ''2015-09-17'' union all 
select ''0415-02-0960'', ''Martin'', 8.44, ''2015-09-16'' union all 
select ''0415-02-0960'', ''Martin'', 10.9, ''2015-09-15'' union all 
select ''0415-02-0960'', ''Martin'', 10.91, ''2015-09-14'' union all 
select ''0415-02-0960'', ''Martin'', 9.91, ''2015-09-11'' union all 
select ''0415-02-0960'', ''Martin'', 10.31, ''2015-09-10'' union all 
select ''0415-02-0960'', ''Martin'', 11.16, ''2015-09-09'' union all 
select ''0415-02-0960'', ''Martin'', 12.04, ''2015-09-08'' union all 
select ''0415-02-0960'', ''Martin'', 9.42, ''2015-09-07'' union all 
select ''0415-02-0960'', ''Martin'', 10.43, ''2015-09-04'' union all 
select ''0415-02-0960'', ''Martin'', 10.39, ''2015-09-03'')data'); 



PREPARE stmt FROM @sql; 
EXECUTE stmt; 
DEALLOCATE PREPARE stmt; 

這裏是我的嘗試輸出:

enter image description here

我想要的輸出,如果pivot date是等於rd那麼樞紐date content就變成了valuetotal,但如果不相等,則值變爲0.還有id and lastname become distinct也與關鍵日期total=應該刪除。

我試過這個示例註釋的代碼,但沒有運氣。

這一個工作,但它只返回一行。

GROUP_CONCAT(DISTINCT CONCAT("SUM(IF(data.rd='",DATE(list_of_dates),"',`total`,0)) AS `",list_of_dates,"`")) as `dates` 

enter image description here

任何幫助表示讚賞。

在此先感謝

+0

是在一個表中的數據?在你的代碼中,你似乎在生成數據而不是從表中讀取數據。桌子的名字是什麼? –

+0

我正在準備要更新的數據,但它來自我的表,它是一樣的..你可以隨時運行查詢。沒有任何需要的表格。 – redzsol

+0

@redzol:這是否適合你?如果是這樣,請標記爲已接受並投票。如果沒有,讓我知道你有什麼錯誤,或者什麼都不起作用。 –

回答

1

UPDATE:我改變部分代碼的命令

我開始通過創建一個select聲明產生最終結果的日期子合計列,然後寫代碼來生成select語句。我還創建了一個SQL Fiddle來生成聲明和結果。

而不是僞造過程中的數據,我創建了一個真正的表。您將不得不修改代碼以適應您的特定需求,因爲我沒有您要使用的表格的名稱。

這工作分幾個步驟:

  • 設置參數(開始/結束日期)
  • 獲取從指定的範圍的數據日期的列表。
  • 生成SQL語句。
  • 執行SQL語句。

這裏是我寫的模型測試SQL語句:

SELECT 
    `id`, 
    `lastname`, 
    SUM(`total`) AS `total`, 
    SUM(if(`rd` = '2015-09-01',`total`,0)) AS `total:2015-09-01`, 
    SUM(if(`rd` = '2015-09-02',`total`,0)) AS `total:2015-09-02`, 
    SUM(if(`rd` = '2015-09-03',`total`,0)) AS `total:2015-09-03`, 
    SUM(if(`rd` = '2015-09-04',`total`,0)) AS `total:2015-09-04`, 
    SUM(if(`rd` = '2015-09-05',`total`,0)) AS `total:2015-09-05`, 
    SUM(if(`rd` = '2015-09-06',`total`,0)) AS `total:2015-09-06` 
FROM `test_table` 
WHERE `rd` BETWEEN @sdate AND @edate 
GROUP BY `id`,`lastname` 
ORDER BY `id`,`lastname` 

而這裏的代碼來生成並運行該SQL語句:

/* Set the parameters */ 
SET @sql = NULL; 
SET @dates = NULL; 
SET @sdate = '2015-09-01'; 
SET @edate = '2015-09-06'; 

/* Get a list of dates, and format as the column def for the SQL SELECT */ 
SELECT 
    GROUP_CONCAT(CONCAT("SUM(IF(`rd` = '",DATE(`rd`),"',`total`,0)) AS `total:",DATE(`rd`),"`")) as `dates` 
FROM (SELECT `rd` FROM `test_table` GROUP BY `rd` ORDER BY `rd`) AS a 
WHERE a.`rd` BETWEEN @sdate AND @edate 
INTO @dates; 

/* Generate the SQL SELECT */ 
SELECT CONCAT("SELECT `id`,`lastname`,SUM(`total`) AS `total`,",@dates," FROM `test_table` WHERE `rd` BETWEEN @sdate AND @edate GROUP BY `id`,`lastname` ORDER BY `id`,`lastname`") 
INTO @sql; 

/* Execute the SELECT */ 
PREPARE stmt FROM @sql; 
EXECUTE stmt; 
DEALLOCATE PREPARE stmt; 
+0

感謝您的帖子,我會試試這個。 – redzsol

+0

[Err] 1054 - '字段列表'中的未知列'rd' – redzsol

+0

Where?哪個SQL語句? –