2014-07-08 195 views
0

我在使用MySQL數據庫的舊版經典asp應用程序中使用以下查詢生成報表。MySql查詢加入

SELECT DISTINCT * 
FROM 
    (SELECT b.rep_year, 
      b.rep_month, 
      sum(x.book_qty)AS salecnt, 
      sum(x.book_qty*x.s_price)AS Tsales 
    FROM report_date b 
    LEFT JOIN sale x ON Month(x.s_Date) = b.rep_month 
    AND year(x.s_Date) = b.rep_year 
    WHERE x.s_date <=curdate() 
    AND x.s_date >'2010-10-31' 
    GROUP BY b.rep_month, 
      b.rep_year)AS bob, 

    (SELECT c.rep_year, 
      c.rep_month, 
      sum(y.gQty) AS Tgift, 
      sum(y.gQty*y.gPrice)AS TGiftSum 
    FROM report_date c 
    LEFT JOIN gift y ON month(y.gDate) = c.rep_month 
    AND year(y.gDate)=c.rep_year 
    WHERE y.gbooktype =1 
    AND y.gdate <=curdate() 
    AND y.gdate>'2010-10-31' 
    GROUP BY c.rep_year, 
      c.rep_month)AS cob, 

    (SELECT d.rep_year, 
      d.rep_month, 
      sum(z.gQty) AS Ttgift, 
      sum(z.gQty*z.gprice)AS TtGiftSum 
    FROM report_date d 
    LEFT JOIN gift z ON month(z.gDate) = d.rep_month 
    AND year(z.gDate)=d.rep_year 
    WHERE z.gbooktype =2 
    AND z.gdate <=curdate() 
    AND z.gDate>'2010-10-31' 
    GROUP BY d.rep_year, 
      d.rep_month)AS dob 
WHERE bob.rep_month = cob.rep_month 
    AND bob.rep_year=cob.rep_year 
    AND bob.rep_year=dob.rep_year 
    AND cob.rep_year=dob.rep_year 
    AND cob.rep_month = dob.rep_month 
GROUP BY bob.rep_year, 
     bob.rep_month 

從三個表:

report_date [rep_id, rep_month, rep_year] 
sale[sale_id,book_id,book_qty,s_date,s_price] 
gift[gift_id,book_id,gQty,gDate,gPrice,gbooktype] 

我已經使用在應用此查詢2011年以來和我已經意識到錯誤,這只是今天。

對於任何特定的年份和月份,如果gbooktype 1和gbooktype 2都沒有贈品交易,則在報告摘要中忽略該年份和月份。我將非常感謝任何幫助,以便即使有書籍1或2的贈品交易,或者同時生成該月份和年份的報告。此外,任何查詢優化幫助將不勝感激。

回答

0

我建議你建立一個日期列表,然後將其他結果加入到這個列表中。

您可以通過UNIONing固定值並將其添加到起始值來生成日期列表。

這將使你取決於你的REPORT_DATE表包含這大概可以簡化爲

SELECT month_year.aYear, month_year.aMonth, bob.salecnt, bob.Tsales, cob.Tgift, cob.TGiftSum, dob.Ttgift, dob.TtGiftSum 
FROM 
(
    SELECT YEAR(DATE_ADD(DATE_ADD('2010-10-31', INTERVAL months_add MONTH), INTERVAL years_add YEAR)) as aYear, MONTH(DATE_ADD(DATE_ADD('2010-10-31', INTERVAL months_add MONTH), INTERVAL years_add YEAR)) as aMonth 
    FROM (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11) months_add 
    CROSS JOIN (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4) years_add 
) AS month_year 
LEFT OUTER JOIN 
(
    SELECT b.rep_year, b.rep_month, sum(x.book_qty)as salecnt, sum(x.book_qty*x.s_price)as Tsales 
    FROM report_date b 
    LEFT JOIN sale x 
    ON Month(x.s_Date) = b.rep_month 
    and year(x.s_Date) = b.rep_year 
    WHERE x.s_date <=curdate() 
    and x.s_date >'2010-10-31' 
    GROUP BY b.rep_month, b.rep_year 
)as bob 
ON bob.rep_year = month_year.aYear 
AND bob.rep_month = month_year.aMonth 
LEFT OUTER JOIN 
(
    SELECT c.rep_year, c.rep_month, sum(y.gQty) as Tgift, sum(y.gQty*y.gPrice)as TGiftSum 
    From report_date c 
    LEFT JOIN gift y 
    ON month(y.gDate) = c.rep_month 
    AND year(y.gDate)=c.rep_year 
    WHERE y.gbooktype =1 and y.gdate <=curdate() and y.gdate>'2010-10-31' 
    GROUP BY c.rep_year, c.rep_month 
)as cob 
ON cob.rep_year = month_year.aYear 
AND cob.rep_month = month_year.aMonth 
LEFT OUTER JOIN 
(
    SELECT d.rep_year, d.rep_month, sum(z.gQty) as Ttgift, sum(z.gQty*z.gprice)as TtGiftSum 
    From report_date d 
    LEFT JOIN gift z 
    ON month(z.gDate) = d.rep_month 
    AND year(z.gDate)=d.rep_year 
    WHERE z.gbooktype =2 and z.gdate <=curdate() and z.gDate>'2010-10-31' 
    GROUP BY d.rep_year, d.rep_month 
)as dob 
ON dob.rep_year = month_year.aYear 
AND dob.rep_month = month_year.aMonth 

像這樣(未測試): -

SELECT month_year.rep_year, month_year.rep_month, bob.salecnt, bob.Tsales, cob.Tgift, cob.TGiftSum, dob.Ttgift, dob.TtGiftSum 
FROM report_date 
LEFT OUTER JOIN 
(
    SELECT year(x.s_Date) AS rep_year, Month(x.s_Date) AS rep_month, sum(x.book_qty) as salecnt, sum(x.book_qty*x.s_price) as Tsales 
    FROM sale x 
    WHERE x.s_date <= curdate() 
    and x.s_date >'2010-10-31' 
    GROUP BY rep_year, rep_month 
)as bob 
ON bob.rep_year = report_date.rep_year 
AND bob.rep_month = report_date.rep_month 
LEFT OUTER JOIN 
(
    SELECT year(y.s_Date) AS rep_year, Month(y.s_Date) AS rep_month, sum(y.gQty) as Tgift, sum(y.gQty*y.gPrice)as TGiftSum 
    From gift y 
    WHERE y.gbooktype =1 
    and y.gdate <= curdate() 
    and y.gdate>'2010-10-31' 
    GROUP BY rep_year, rep_month 
)as cob 
ON cob.rep_year = report_date.rep_year 
AND cob.rep_month = report_date.rep_month 
LEFT OUTER JOIN 
(
    SELECT year(z.s_Date) AS rep_year, Month(z.s_Date) AS rep_month, sum(z.gQty) as Ttgift, sum(z.gQty*z.gprice)as TtGiftSum 
    From gift z 
    WHERE z.gbooktype =2 
    and z.gdate <= curdate() 
    and z.gDate>'2010-10-31' 
    GROUP BY rep_year, rep_month 
)as dob 
ON dob.rep_year = report_date.rep_year 
AND dob.rep_month = report_date.rep_month 
WHERE CONCAT(month_year.rep_year, month_year.rep_month) > '201010' 
AND CONCAT(month_year.rep_year, month_year.rep_month) <= DATE_FORMAT(CURDATE(), '%Y%m') 

它可能會簡化這進一步取決於它包含什麼

+0

謝謝@Kickstart我試圖運行查詢,但它報告找不到一些字段。 report_date表包含從2010年1月1日至2016年12月安排的rep_year(2010年)和rep_month(月,例如2月2日,12月12日12月) – Diin

+0

您可以使用一些示例數據設置SQL提琴,以便我可以進行測試。 – Kickstart

+0

稍微調整了第二個工作。如果沒有任何樣本數據,您能夠提供幫助的速度令我印象深刻 – Diin