MySQL子查询与日期之间

时间:2015-01-02 14:00:00

标签: mysql sql select group-by where

我的查询是用于计算销售利润。要计算成本价格,我需要采取特定日期的平均成本。如果我有一个日期,则以下查询效果很好,但对于 range of dates (replacing '=2014-12-30 ' with 'BETWEEN '2014-12-30' AND '2015-01-02' at both places),它不会生成所需的输出

SELECT sinvoiceno,sinvoicedate,pureweight,makingcharge,totalamount,rd.avgr ,
    (pureweight*rd.avgr)+makingcharge AS TotalCost ,
    totalamount-((pureweight*rd.avgr)+makingcharge) AS Profit 
FROM
    (SELECT DATE_FORMAT(gdate,'%Y-%m-%d') AS grDate,AVG(goldrate24) AS AvgR 
     FROM rate_detail r  
     GROUP BY grDate 
     HAVING grdate = '2014-12-30' ) AS rd,
    sales_master sm 
WHERE  sinvoicedate = '2014-12-30'

3 个答案:

答案 0 :(得分:0)

您需要加入条件,以便在同一日期将子查询的行与sales_master相关联。

SELECT sinvoiceno,sinvoicedate,pureweight,makingcharge,totalamount,rd.avgr ,
    (pureweight*rd.avgr)+makingcharge AS TotalCost ,
    totalamount-((pureweight*rd.avgr)+makingcharge) AS Profit 
FROM
    (SELECT DATE(gDate) AS grDate, AVG(goldrate24) AS AvgR 
     FROM rate_detail r 
     WHERE DATE(gDate) BETWEEN '2014-12-30' AND '2015-01-02' 
     GROUP BY grDate ) AS rd
JOIN sales_master sm ON sinvoicedate = grDate
WHERE sinvoicedate BETWEEN '2014-12-30' AND '2015-01-02' 

答案 1 :(得分:0)

试试这个:

SELECT sinvoiceno, sinvoicedate, pureweight, makingcharge, 
       totalamount, rd.avgr, (pureweight * rd.avgr) + makingcharge AS TotalCost ,
       totalamount -((pureweight * rd.avgr) + makingcharge) AS Profit 
FROM (SELECT STR_TO_DATE(gdate,'%Y-%m-%d') AS grDate,AVG(goldrate24) AS AvgR 
      FROM rate_detail r  
      WHERE STR_TO_DATE(gdate,'%Y-%m-%d') BETWEEN '2014-12-30' AND '2015-01-02'
      GROUP BY grDate
     ) AS rd,
INNER JOIN sales_master sm ON rd.grDate = sinvoicedate
WHERE sinvoicedate BETWEEN '2014-12-30' AND '2015-01-02';

答案 2 :(得分:0)

您似乎希望rd子查询返回一行,这是一段时间内的平均值。因此,请移除group by并使用where

输入相应的边界
SELECT sm.sinvoiceno, sm.sinvoicedate, sm.pureweight, sm.makingcharge, sm.totalamount,
       rd.avgr, (sm.pureweight*rd.avgr) + sm.makingcharge AS TotalCost ,
       sm.totalamount-((sm.pureweight*rd.avgr)+sm.makingcharge) AS Profit 
FROM sales_master sm CROSS JOIN
     (SELECT AVG(goldrate24) AS AvgR 
      FROM rate_detail r  
      WHERE grDate = '2014-12-30'
     ) rd
WHERE sm.sinvoicedate = '2014-12-30';

然后您可以输入一个范围来扩展平均值的时间段。例如:

SELECT sm.sinvoiceno, sm.sinvoicedate, sm.pureweight, sm.makingcharge, sm.totalamount,
       rd.avgr, (sm.pureweight*rd.avgr) + sm.makingcharge AS TotalCost ,
       sm.totalamount-((sm.pureweight*rd.avgr)+sm.makingcharge) AS Profit 
FROM sales_master sm CROSS JOIN
     (SELECT AVG(goldrate24) AS AvgR 
      FROM rate_detail r  
      WHERE grDate BETWEEN '2014-12-30' AND '2015-01-02'
     ) rd
WHERE s.invoiceDate BETWEEN '2014-12-30' AND '2015-01-02';

编辑:

我觉得你想要一个日期的平均值,即发票日期。在那种情况下:

SELECT sm.sinvoiceno, sm.sinvoicedate, sm.pureweight, sm.makingcharge, sm.totalamount,
       rd.avgr, (sm.pureweight*rd.avgr) + sm.makingcharge AS TotalCost,
       sm.totalamount-((sm.pureweight*rd.avgr)+sm.makingcharge) AS Profit 
FROM sales_master sm JOIN
     (SELECT date(grDate) as grDate, AVG(goldrate24) AS AvgR 
      FROM rate_detail r  
      WHERE BETWEEN '2014-12-30' AND '2015-01-02'
      GROUP BY date(grDate)
     ) rd
     ON rd.grDate = sm.s.invoicedate
WHERE s.invoiceDate BETWEEN '2014-12-30' AND '2015-01-02';

我不清楚“日期”列是否有时间成分。如果是这样,那么您希望在适当的地方使用date()