错误代码:1242。子查询返回超过1行(对于所有记录)

时间:2014-05-13 10:15:42

标签: mysql sql

这里我需要通过AgentID获取所有记录组,但它显示错误:错误代码:1242。子查询返回超过1行

表:

 customerID  amountreceived date_time area agentID paymentmode
   1            2000         5/13/2014 hyd   1       cash


enter code here

 DELIMITER $$
 CREATE DEFINER=`ntc`@`%` PROCEDURE `spforallAgents`()
 BEGIN
select 
(select 
        @DayAmount:=sum(AmountRecevied) as Totoalamountperday
    from
        collection_master
    where  day(Date_Time) = day(CURRENT_DATE())   group by AgentID),
(select 
        @MonthAmount:=sum(AmountRecevied) as Totoalamountperday
    from
        collection_master
    where  date_time between DATE_FORMAT(NOW(), '%Y-%m-01') and LAST_DAY(now() - interval 0 month) group by AgentID),
    (select 
        @YearAmount:=sum(AmountRecevied) as Totoalamountpermonth
    from
        collection_master where  year(Date_Time) = YEAR(CURRENT_DATE()) group by AgentID),
 (select 
        @Position:=@Position + 1 AS Rank
    from
        collection_master,
        (SELECT @Position:=0) r group by AgentID) as position;

END

1 个答案:

答案 0 :(得分:2)

使用连接,您可以这样做(未经测试): -

SELECT AgentID, 
        current_date_amount, 
        date_range_amount, 
        month_amount,
        @Position:=@Position + 1 AS `Rank`
FROM
(
    SELECT just_agent.AgentID, 
            total_current_date.Totoalamountperday AS current_date_amount, 
            total_date_range.Totoalamountperday AS date_range_amount, 
            total_month.Totoalamountpermonth AS month_amount
    FROM
    (
        SELECT DISTINCT AgentID
        FROM collection_master
    ) just_agent
    LEFT OUTER JOIN
    (
        select AgentID, SUM(AmountRecevied) as Totoalamountperday
        from collection_master
        where  day(Date_Time) = day(CURRENT_DATE())
        group by AgentID
    ) total_current_date
    ON just_agent.AgentID = total_current_date.AgentID
    LEFT OUTER JOIN
    (
        select AgentID, sum(AmountRecevied) as Totoalamountperday
        from collection_master
        where  date_time between DATE_FORMAT(NOW(), '%Y-%m-01') and LAST_DAY(now() - interval 0 month) 
        group by AgentID
    ) total_date_range
    ON just_agent.AgentID = total_date_range.AgentID
    LEFT OUTER JOIN
    (
        select AgentID, sum(AmountRecevied) as Totoalamountpermonth
        from collection_master 
        where  year(Date_Time) = YEAR(CURRENT_DATE()) 
        group by AgentID
    ) total_month
    ON just_agent.AgentID = total_month.AgentID
    ORDER BY total_month.Totoalamountpermonth DESC
) Sub1
CROSS JOIN (SELECT @Position:=0) Sub2

请注意,这是一些假设。例如,您的原始查询不清楚您要使用哪个顺序来分配排名(我假设降序为Totoalamountpermonth)。如果有另一个表给出代理ID,而不是使用额外的子查询来获取不同的AgentID,那么它会更简单

SQL小提琴: -

http://www.sqlfiddle.com/#!2/549232/2

修改

使用来自其他线程的代码(和M. Massias值得信任)将查询加入到自身中。

SELECT t2.*
FROM 
(
    SELECT AgentID, 
            current_date_amount, 
            date_range_amount, 
            month_amount,
            @Position1:=@Position1 + 1 AS `Rank`
    FROM
    (
        SELECT just_agent.AgentID, 
                total_current_date.Totoalamountperday AS current_date_amount, 
                total_date_range.Totoalamountperday AS date_range_amount, 
                total_month.Totoalamountpermonth AS month_amount
        FROM
        (
            SELECT DISTINCT AgentID
            FROM collection_master
        ) just_agent
        LEFT OUTER JOIN
        (
            select AgentID, SUM(AmountRecevied) as Totoalamountperday
            from collection_master
            where  day(Date_Time) = day(CURRENT_DATE())
            group by AgentID
        ) total_current_date
        ON just_agent.AgentID = total_current_date.AgentID
        LEFT OUTER JOIN
        (
            select AgentID, sum(AmountRecevied) as Totoalamountperday
            from collection_master
            where  date_time between DATE_FORMAT(NOW(), '%Y-%m-01') and LAST_DAY(now() - interval 0 month) 
            group by AgentID
        ) total_date_range
        ON just_agent.AgentID = total_date_range.AgentID
        LEFT OUTER JOIN
        (
            select AgentID, sum(AmountRecevied) as Totoalamountpermonth
            from collection_master 
            where  year(Date_Time) = YEAR(CURRENT_DATE()) 
            group by AgentID
        ) total_month
        ON just_agent.AgentID = total_month.AgentID
        ORDER BY total_month.Totoalamountpermonth DESC
    ) Sub1
    CROSS JOIN (SELECT @Position1:=0) Sub2
) t1
JOIN 
(
    SELECT AgentID, 
            current_date_amount, 
            date_range_amount, 
            month_amount,
            @Position2:=@Position2 + 1 AS `Rank`
    FROM
    (
        SELECT just_agent.AgentID, 
                total_current_date.Totoalamountperday AS current_date_amount, 
                total_date_range.Totoalamountperday AS date_range_amount, 
                total_month.Totoalamountpermonth AS month_amount
        FROM
        (
            SELECT DISTINCT AgentID
            FROM collection_master
        ) just_agent
        LEFT OUTER JOIN
        (
            select AgentID, SUM(AmountRecevied) as Totoalamountperday
            from collection_master
            where  day(Date_Time) = day(CURRENT_DATE())
            group by AgentID
        ) total_current_date
        ON just_agent.AgentID = total_current_date.AgentID
        LEFT OUTER JOIN
        (
            select AgentID, sum(AmountRecevied) as Totoalamountperday
            from collection_master
            where  date_time between DATE_FORMAT(NOW(), '%Y-%m-01') and LAST_DAY(now() - interval 0 month) 
            group by AgentID
        ) total_date_range
        ON just_agent.AgentID = total_date_range.AgentID
        LEFT OUTER JOIN
        (
            select AgentID, sum(AmountRecevied) as Totoalamountpermonth
            from collection_master 
            where  year(Date_Time) = YEAR(CURRENT_DATE()) 
            group by AgentID
        ) total_month
        ON just_agent.AgentID = total_month.AgentID
        ORDER BY total_month.Totoalamountpermonth DESC
    ) Sub1
    CROSS JOIN (SELECT @Position2:=0) Sub2
) t2
ON ABS(t1.`Rank` - t2.`Rank`) <= 1.5 
WHERE t1.AgentID = 2