每个created_by的distinct created_by和sum total_amount

时间:2013-07-18 07:50:21

标签: mysql

我坚持这个。

我的数据库中有两个表(mysql)1是(i_mst),2是(o_mst)

我在i_mst中有日期列,o_mst中的amount列和o_mst中的created_by列。

我需要的是

distinct created_by and sum each created_by amount By i_mst date 

    BETWEEN '2013-01-01'
    AND '2013-08-01'

并以单行返回。

这样做我正在尝试这个。

SELECT DISTINCT (
o_mst.created_by
), o_mst.amount
FROM o_mst
LEFT JOIN i_mst ON i_mst.i_date
WHERE i_date
BETWEEN '2013-01-01'
AND '2013-08-01'
ORDER BY `o_mst`.`amount` DESC
LIMIT 0 , 5

它为我提供了多个created_by id及其数量。

我希望为每个created_by行获取不同的created_by rows和sum total_amount。

如果可能,请帮助我。

由于

3 个答案:

答案 0 :(得分:0)

您可以尝试此查询

SELECT o_mst.created_by, SUM(o_mst.amount)
FROM o_mst
LEFT JOIN i_mst ON i_mst.i_date
WHERE i_date
BETWEEN '2013-01-01'
AND '2013-08-01'
GROUP BY o_mst.created_by
ORDER BY `o_mst`.`amount` DESC
LIMIT 0 , 5

答案 1 :(得分:0)

试试这个

SELECT o_mst.created_by, SUM(o_mst.amount)
FROM o_mst
INNER JOIN i_mst ON i_mst.inq_mst_id = o_mst.inq_mst_id 
WHERE i_date
BETWEEN '2013-01-01'
AND '2013-08-01'
GROUP BY o_mst.created_by DESC
LIMIT 0 , 5

答案 2 :(得分:0)

这个怎么样

SELECT DISTINCT_VALUES.created_by, 
       SUM(DISTINCT_VALUES.amount)
 FROM
    (

   SELECT * 
   FROM o_mst
   LEFT JOIN i_mst ON i_mst.i_date
   WHERE i_date
   BETWEEN '2013-01-01'
   AND '2013-08-01'
    GROUP BY o_mst.created_by,  //.. all the columns that you want to merge
   ORDER BY `o_mst`.`amount` DESC
    LIMIT 0 , 5
) DISTINCT_VALUES