日期:2014-05-18  浏览次数:20384 次

谁能看明白这句sql是什么意思....好难啊~
SELECT Provinces.Pr_name, 
  CityRefer.cr_name, 
  BasicOffice.bo_name, 
  JdName.jd_name, 
  Equipment.sb_no, 
  ISNULL(derivedtbl_2.sumalldata, 0) AS sumalldata, 
  ISNULL(derivedtbl_2.suminperioddata, 0) AS suminperioddata, 
  Equipment.sb_state, 
  Equipment.sb_position, 
  Equipment.sb_person, 
  Equipment.sb_tel, 
  Equipment.sb_coordinates, 
  ISNULL(derivedtbl_2.meterscount, 0) AS meterscount, 
  ISNULL(derivedtbl_2.Alerted, 0) AS Alerted 
 FROM (
SELECT MetersName_Ori.ConcentrationID, 
COUNT(derivedtbl_1.mn_id) AS meterscount, 
SUM(MetersName_Ori.AccuData) AS sumalldata, 
SUM(derivedtbl_1.SumDataAdd) AS suminperioddata, 
ISNULL(MAX(MetersName_Ori.Alerted), 0) AS Alerted 
FROM (SELECT SumDataAdd, mn_id FROM dbo.MetersDataSumByPastMonth(0) AS MetersDataSumByPastMonth_1) 
AS derivedtbl_1 
INNER JOIN MetersName_Ori ON derivedtbl_1.mn_id = MetersName_Ori.id 
GROUP BY MetersName_Ori.ConcentrationID) 
  AS derivedtbl_2 
  RIGHT OUTER JOIN Equipment ON derivedtbl_2.ConcentrationID = Equipment.sb_no 
  INNER JOIN BasicOffice ON Equipment.cr_id = BasicOffice.cr_id AND Equipment.bo_id = BasicOffice.bo_id AND Equipment.pr_id = BasicOffice.pr_id 
  INNER JOIN Provinces ON Equipment.pr_id = Provinces.Pr_id 
  INNER JOIN CityRefer ON Equipment.cr_id = CityRefer.cr_id AND Equipment.pr_id = CityRefer.pr_id 
  INNER JOIN JdName ON Equipment.pr_id = JdName.pr_id AND Equipment.cr_id = JdName.cr_id AND 
Equipment.bo_id = JdName.bo_id AND Equipment.jd_id = JdName.jd_id

------解决方案--------------------
帮你整理一下,LZ如果知道这几个表示什么意思,大概就知道这个语句的含义了。 
SQL code
SELECT  Provinces.Pr_name ,
        CityRefer.cr_name ,
        BasicOffice.bo_name ,
        JdName.jd_name ,
        Equipment.sb_no ,
        ISNULL(derivedtbl_2.sumalldata, 0) AS sumalldata ,
        ISNULL(derivedtbl_2.suminperioddata, 0) AS suminperioddata ,
        Equipment.sb_state ,
        Equipment.sb_position ,
        Equipment.sb_person ,
        Equipment.sb_tel ,
        Equipment.sb_coordinates ,
        ISNULL(derivedtbl_2.meterscount, 0) AS meterscount ,
        ISNULL(derivedtbl_2.Alerted, 0) AS Alerted
FROM    ( SELECT    MetersName_Ori.ConcentrationID ,
                    COUNT(derivedtbl_1.mn_id) AS meterscount ,
                    SUM(MetersName_Ori.AccuData) AS sumalldata ,
                    SUM(derivedtbl_1.SumDataAdd) AS suminperioddata ,
                    ISNULL(MAX(MetersName_Ori.Alerted), 0) AS Alerted
          FROM      ( SELECT    SumDataAdd ,
                                mn_id
                      FROM      dbo.MetersDataSumByPastMonth (0) AS MetersDataSumByPastMonth_1
                    ) AS derivedtbl_1
                    INNER JOIN MetersName_Ori ON derivedtbl_1.mn_id = MetersName_Ori.id
          GROUP BY  MetersName_Ori.ConcentrationID
        ) AS derivedtbl_2
        RIGHT OUTER JOIN Equipment ON derivedtbl_2.ConcentrationID = Equipment.sb_no
        INNER JOIN BasicOffice ON Equipment.cr_id = BasicOffice.cr_id
                                  AND Equipment.bo_id = BasicOffice.bo_id
                                  AND Equipment.pr_id = BasicOffice.pr_id
        INNER JOIN Provinces ON Equipment.pr_id = Provinces.Pr_id
        INNER JOIN CityRefer ON Equipment.cr_id = CityRefer.cr_id
                                AND Equipment.pr_id = CityRefer.pr_i