Mysql内部连接来自两个结果

时间:2014-11-02 15:25:45

标签: mysql

我在MySQL中遇到问题。我能够创建这些结果。

SELECT X.data, 
       SUM(X.costo_totale) AS 'COSTO TOTALE', 
       X.reparto 
FROM  (SELECT DISTINCT( DATE(start_date) ) 
                            AS 
                            'DATA', 
                      TRUNCATE(IFNULL(SUM(TIMESTAMPDIFF(minute, start_date, 
                                          end_date)) 
                               , 0) / 60, 2) AS 
                      'ORE', 
                      costo_orario, 
                      TRUNCATE(( IFNULL(SUM(TIMESTAMPDIFF(minute, start_date, 
                                            end_date 
                                            )), 0) / 60 ) * 
                               costo_orario, 2) 
                            AS 'costo_totale', 
                      event_name, 
                      reparto 
       FROM   `agoragroup_cgo_events` a 
              INNER JOIN `agoragroup_cgo_dipendenti` b 
                      ON a.section_id = b.id_utente 
       WHERE  reparto = 'CASSA' 
       GROUP  BY section_id, 
                 data, 
                 reparto) X 
GROUP  BY X.data, 
          X.reparto 
DATA    COSTO TOTALE    REPARTO
2014-10-27  645.85  CASSA
2014-10-28  677.99  CASSA
2014-10-29  496.42  CASSA
2014-10-30  586.43  CASSA
2014-10-31  744.76  CASSA
2014-11-01  874.75  CASSA
2014-11-02  702.15  CASSA

这个查询

SELECT data,familia, 
       valore, 
       TRUNCATE(( valore * conseguito ) / 100, 2) AS 'Conseguito', 
       TRUNCATE(( valore * budget ) / 100, 2)     AS 'Budget' 
FROM   `agoragroup_dati_incidenza` f 
       INNER JOIN `agoragroup_chronoforms_data_margine_lordo_reparto` g 
               ON f.familia = g.reparto 
WHERE  familia = 'CASSA' 
       AND f.smk = 'LE OFFICINE' 
      data  familia valore      Conseguito  Budget 
2014-10-27  CASSA   21060.99    4052.13 3999.48 
2014-10-28  CASSA   23333.93    4489.44 4431.11 
2014-10-29  CASSA   24103.83    4637.57 4577.31 
2014-10-30  CASSA   26683.23    5133.85 5067.14
2014-10-31  CASSA   33008.14    6350.76 6268.24
2014-11-01  CASSA   54064.79    10402.06 10266.90 

我想通过日期字段

交叉数据
   data familia valore      Conseguito  Budget COSTO TOTALE
2014-10-27  CASSA   21060.99    4052.13 3999.48 645.8
2014-10-28  CASSA   23333.93    4489.44 4431.11 677.99
2014-10-29  CASSA   24103.83    4637.57 4577.31 496.42
2014-10-30  CASSA   26683.23    5133.85 5067.14 586.43
2014-10-31  CASSA   33008.14    6350.76 6268.24 586.43  

我在很多方面尝试过,但我找不到解决方案

1 个答案:

答案 0 :(得分:0)

我认为如果你把它们加在一起就很容易。您的情况可能是“数据”和“家庭/回归”是相等的。

试试这个:

SELECT w.data, t.familia, t.valore, t.conseguito, t.budget, w.costo_totale
FROM(SELECT X.data, SUM(X.costo_totale) AS 'COSTO TOTALE', X.reparto 
     FROM (SELECT DISTINCT(DATE(start_date)) AS 'DATA', 
                      TRUNCATE(IFNULL(SUM(TIMESTAMPDIFF(minute, start_date, end_date)) , 0) / 60, 2) AS 'ORE', 
                      costo_orario, 
                      TRUNCATE(( IFNULL(SUM(TIMESTAMPDIFF(minute, start_date, end_date)), 0) / 60 ) * costo_orario, 2) AS 'costo_totale', 
                      event_name, reparto 
           FROM   `agoragroup_cgo_events` a 
           INNER JOIN `agoragroup_cgo_dipendenti` b ON a.section_id = b.id_utente 
           WHERE  reparto = 'CASSA' 
           GROUP  BY section_id, data, reparto) X 
     GROUP  BY X.data, X.reparto) w
JOIN(SELECT data,familia, valore, 
            TRUNCATE(( valore * conseguito ) / 100, 2) AS 'Conseguito', 
            TRUNCATE(( valore * budget ) / 100, 2)     AS 'Budget' 
     FROM   `agoragroup_dati_incidenza` f 
     INNER JOIN `agoragroup_chronoforms_data_margine_lordo_reparto` g 
            ON f.familia = g.reparto 
     WHERE  familia = 'CASSA' 
     AND f.smk = 'LE OFFICINE') t
 ON t.data = w.data AND t.familia = w.reparto;