Ich versuche, eine Pivot-Tabelle mit zwei Abfrageergebnissen aus einer MySql-Datenbank abzurufen
Das ist meine SQL-Abfrage:
SELECT * FROM ( SELECT 'NotSpot' as NotSpot,SUM(builds.Duration) as Duration , DATE_FORMAT(builds.date, "%Y-%c-%d") as date FROM builds JOIN labels ON builds.Labels_label_id = labels.label_id JOIN CITools ON CITools.tool_id=labels.CITools_tool_id WHERE labels.label_name NOT LIKE '%SPOT%' AND CITools.tool_name='jenkins' AND builds.date AND builds.date >= '2022-03-01' AND builds.date <= '2022-03-10' GROUP BY DAY(date) UNION ALL SELECT 'Spot' as Spot,SUM(builds.Duration) as Duration , DATE_FORMAT(builds.date, "%Y-%c-%d") as date FROM builds JOIN labels ON builds.Labels_label_id = labels.label_id JOIN CITools ON CITools.tool_id=labels.CITools_tool_id WHERE labels.label_name LIKE '%SPOT%' AND CITools.tool_name='jenkins' AND builds.date AND builds.date >= '2022-03-01' AND builds.date <= '2022-03-10' GROUP BY DAY(date) ) result
Das ist das Ergebnis:
__________________________________ NotSpot | Duration | date ________ __________ ______ NotSpot 1756343919 2022-03-01 NotSpot 1710800867 2022-03-02 NotSpot 1672806894 2022-03-03 NotSpot 859574350 2022-03-04 . . . . . . . . . Spot 693071042 2022-03-01 Spot 728884095 2022-03-02 Spot 872995684 2022-03-03 . . . . . . . . . __________________________________
Das Ergebnis, das ich will:
_______________________________________________ NotSpot Duration | Spot Duration | date __________________ ________________ _________ 1756343919 1756343919 2022-03-01 1710800867 1672806894 2022-03-02 1672806894 859574350 2022-03-03 859574350 4267822656 2022-03-04 . . . . . . . . . ________________________________________________
Können Sie mir helfen, dieses Ergebnis zu erzielen? Vielen Dank im Voraus.
您始终可以使用 case 函数来模拟数据透视表
其中“结果”正是您的查询
使用
case
表达式进行条件聚合: