在我的頭撞到牆上之後,我試圖使用此示例表內容獲取數據透視數據。如何在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;
這裏是我的嘗試輸出:
我想要的輸出,如果pivot date
是等於rd
那麼樞紐date content
就變成了value
的total
,但如果不相等,則值變爲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`
任何幫助表示讚賞。
在此先感謝
是在一個表中的數據?在你的代碼中,你似乎在生成數據而不是從表中讀取數據。桌子的名字是什麼? –
我正在準備要更新的數據,但它來自我的表,它是一樣的..你可以隨時運行查詢。沒有任何需要的表格。 – redzsol
@redzol:這是否適合你?如果是這樣,請標記爲已接受並投票。如果沒有,讓我知道你有什麼錯誤,或者什麼都不起作用。 –